如何更改时间戳的偏移,然后将更改存储在PostgreSQL中?
我在Rails应用程序上有一个表单,该表格允许您创建一个活动对象,用户可以在其中设置start_date
,end_date
和timezone
for日期将正式开始和结束。
我想做的是将所选时区的偏移应用于start_date
和end_date
。我尝试了几种组合,但我似乎无法使其起作用。对于初学者,当我在11:00 pm的开始和结束日期选择-0500
的偏移时,选择中心时区CDT时,PostgreSQL的最终结果是以下时间戳。
# No offset is applied
Wed, 08 Jun 2022 23:00:00.000000000 UTC +00:00
为了尝试应用偏移,我尝试了铁轨代码中的一些组合。以下是fore_validation
回调中的一些示例。它是伪代码,但这是它的要旨。
示例#1
date_tmp = send("start_at") # Wed, 08 Jun 2022 23:00:00.000000000 UTC +00:00
date_string = date_tmp.to_datetime.change(offset: '-0500').to_s
assign_attributes("start_at" => date_string) # "2022-06-08T22:00:00-05:00"
结果没有更改为1。
示例#2
date_tmp = send("start_at") # Wed, 08 Jun 2022 23:00:00.000000000 UTC +00:00
date_string = date_tmp.to_datetime.change(offset: '-0500').to_s
dt = Chronic.parse(date_string.to_time.to_s)
assign_attributes("start_at" => dt) # "2022-06-08 22:00:00 -0500"
这些变化都不可用。我还试图保存到看起来像这样的PostgreSQL时间戳:
2022-06-08T22:00:00-05:00
2022-06-08 22:00:00 -0500
Wed, 08 Jun 2022 22:00:00 -0500
我不明白为什么Rails和PostgreSQL无法使用我想要的调整后的UTC_OFFSET保存时间戳。我会做什么错?
I have a form on my rails app that allows you to create a campaign object where the user can set a start_date
, end_date
, and timezone
for which the dates will officially start and end.
What I would like to do is to apply the offset of the selected timezone to both the start_date
and end_date
. I've tried a few combinations but I can't seem to make it work. For starters, when I select my start and end dates for 11:00 PM and select Central Time Zone CDT with offset of -0500
, the end result in postgresql is the following timestamp.
# No offset is applied
Wed, 08 Jun 2022 23:00:00.000000000 UTC +00:00
In order to try to apply the offset, I've tried a few combinations in my rails code. Here are some examples in a before_validation
callback. It's pseudocode but this is the gist of it.
Example #1
date_tmp = send("start_at") # Wed, 08 Jun 2022 23:00:00.000000000 UTC +00:00
date_string = date_tmp.to_datetime.change(offset: '-0500').to_s
assign_attributes("start_at" => date_string) # "2022-06-08T22:00:00-05:00"
The result is no change for example 1.
Example #2
date_tmp = send("start_at") # Wed, 08 Jun 2022 23:00:00.000000000 UTC +00:00
date_string = date_tmp.to_datetime.change(offset: '-0500').to_s
dt = Chronic.parse(date_string.to_time.to_s)
assign_attributes("start_at" => dt) # "2022-06-08 22:00:00 -0500"
None of these variations work. I've also tried to save to postgresql timestamps that look like this:
2022-06-08T22:00:00-05:00
2022-06-08 22:00:00 -0500
Wed, 08 Jun 2022 22:00:00 -0500
I don't understand why rails and postgresql can't save the timestamp with the adjusted utc_offset that I want. What can I be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
new_offset
在dateTime
实例上的方法。它重复
dateTime对象
并重置其偏移量。一些例子:
Try
new_offset
method on yourDateTime
instance.It duplicates
datetime object
and resets its offset.Some examples: