如何更改时间戳的偏移,然后将更改存储在PostgreSQL中?

发布于 2025-02-08 04:31:18 字数 1298 浏览 1 评论 0原文

我在Rails应用程序上有一个表单,该表格允许您创建一个活动对象,用户可以在其中设置start_dateend_datetimezone for日期将正式开始和结束。

我想做的是将所选时区的偏移应用于start_dateend_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2025-02-15 04:31:18

尝试new_offsetdateTime实例上的方法。
它重复dateTime对象并重置其偏移量。

一些例子:

date = DateTime.now 
date # => Thu, 16 Jun 2022 20:14:58 +0000
date.new_offset('+03:00') # => Thu, 16 Jun 2022 23:14:58 +0300
date.new_offset('-03:00') # => Thu, 16 Jun 2022 17:14:58 -0300

Try new_offset method on your DateTime instance.
It duplicates datetime object and resets its offset.

Some examples:

date = DateTime.now 
date # => Thu, 16 Jun 2022 20:14:58 +0000
date.new_offset('+03:00') # => Thu, 16 Jun 2022 23:14:58 +0300
date.new_offset('-03:00') # => Thu, 16 Jun 2022 17:14:58 -0300
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文