在 Sequel 中使用 UTC?

发布于 2024-07-26 12:23:35 字数 141 浏览 6 评论 0原文

我不想将时间存储在我当地的时区,但续集让我变得非常困难。 我可以在将它们放在那里之前将它们设置为 UTC(有点痛苦),但是当我将它们取出时,它会假设它们是本地日期,然后它们都是未来的 8 小时。 这是还没有实施的事情吗? 如果是这样,有什么解决方法吗? 谢谢!

I'd like to not store times in my local timezone, but Sequel is making it really tough on me. I can set them to UTC before I put them in there (a bit of a pain), but then when I take them back out it assumes that they are local dates and then they are all 8 hours in the future. Is this something that hasn't been implemented yet? And if so, are there any workarounds? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

嘿咻 2024-08-02 12:23:35

这在这一点上有点过时,但我相信自从发布原始答案以来,这里的最佳解决方案已经改变。 如果您设置

Sequel.default_timezone = :utc

sequel 会将所有时间视为UTC,并且不会表现出问题中描述的行为。

欲了解更多信息,请访问 http://sequel.jeremyevans.net/rdoc/classes/Sequel /时区.html

This is a bit outdated at this point but I believe the best solution here has changed since the original answer was posted. If you set

Sequel.default_timezone = :utc

sequel will treat all times as UTC and wont exhibit the behavior described in the question.

Find more info at http://sequel.jeremyevans.net/rdoc/classes/Sequel/Timezones.html

药祭#氼 2024-08-02 12:23:35

我自己也有一个非常相似的问题。

此信息取自 Sequel RDoc

Sequel 可以使用 Time 或 DateTime
从数据库返回的时间。
它默认为时间。 将其更改为
日期时间,使用:

Sequel.datetime_class = DateTime

还要确保您没有将时区信息存储在数据库中。
我使用的是 Postgres,列类型是无时区的时间戳

这应该会导致显示的日期/时间采用 UTC。 当传入 2009-07-13T03:22:53Z 的日期/时间时,它对我有用,结果显示为 2009-07-13T03:22:53+00:00

Just had a very similar issue myself.

This information has been taken from the Sequel RDoc

Sequel can use either Time or DateTime
for times returned from the database.
It defaults to Time. To change it to
DateTime, use:

Sequel.datetime_class = DateTime

Also make sure your not storing the timezone information in your database.
I'm using Postgres and the column type is timestamp without time zone.

This should result in the displayed Date/Time being in UTC. It has worked for me when passing in a Date/Time of 2009-07-13T03:22:53Z the result is displayed as 2009-07-13T03:22:53+00:00

双马尾 2024-08-02 12:23:35

尝试将它们放入 UTC 的最简单方法是覆盖用于返回 UTC 时间文字字符串的数据集类的literal_datetime 和/或literal_time。

以 UTC 格式获取它们取决于您使用的适配器。 例如,postgres适配器调用Sequel.string_to_datetime,它只调用Sequel.datetime_class(默认为Time)上的parse。 如果日期时间列包含时区信息,则一切应该正常。 如果它不包含时区信息,Time.parse 将假定给定的是本地时间。 在这种情况下,您可能需要重写 Sequel.string_to_datetime 以确保它始终返回具有 UTC 偏移量的时间(也许通过调用 Time.parse(s).gmtime )。

The easiest way to try to put them in as UTC is to override literal_datetime and/or literal_time for the dataset class you are using to return a literal string of the UTC time.

Getting them out in UTC depends on the adapter you are using. For example, the postgres adapter calls Sequel.string_to_datetime, which just calls parse on the Sequel.datetime_class (Time by default). If the datetime column includes the timezone information, things should work fine. If it doesn't include timezone information, Time.parse is going to assume it is given a local time. In that case, you may want to override Sequel.string_to_datetime to make sure it always returns the time with the UTC offset (maybe by calling Time.parse(s).gmtime).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文