Ruby 1.8.6 中 DateTime 的时区转换

发布于 2024-12-20 11:35:06 字数 819 浏览 8 评论 0原文

(卡在 Ruby 1.8.6 的土地上)

直到最近我才使用 Time 来解析 UTC 日期并在本地时区中打印它们

dt = Time.parse("20111225080000Z")
puts dt.localtime
Sun Dec 25 21:00:00 +1300 2011
puts dt.zone
"New Zealand Daylight Time"

不幸的是 1901 和 2034 的限制最终阻碍了我,所以我尝试使用 DateTime

dt = DateTime.parse(date)

但我找不到与 .localtime 等效的内容,

dt.timezone is of course +00:00
DateTime.now().zone is +13:00

我去使用它,但意识到要转换的日期是否不在白天的话答案是错误的。请注意,Time 类获取正确的时间,并将时间转换为本地时间(不是特定的时区或偏移量) - midwinter =>标准时间。

dt = Time.parse("20110725080000Z")
puts dt.localtime
Sun Jul 25 21:00:00 +1200 2011
puts dt.zone
"New Zealand Standard Time"

对于 DateTime 类,哪里相当于询问我的本地时间而不指定确切的(可能是不正确的时区)?

(Stuck in Ruby 1.8.6 land)

Until recently I was using Time to parse UTC dates and print them in a local timezone

dt = Time.parse("20111225080000Z")
puts dt.localtime
Sun Dec 25 21:00:00 +1300 2011
puts dt.zone
"New Zealand Daylight Time"

Unfortunately the limits 1901 and 2034 finally got in the way and so I tried to use DateTime

dt = DateTime.parse(date)

But I cant find an equivalent to .localtime

dt.timezone is of course +00:00
DateTime.now().zone is +13:00

I went to use that but realized if the date being converted was NOT in Daylight Time the answer would be wrong. Note the Time class gets it correct, and converts the time to the localtime (not a specific timezone or offset) - midwinter => Standard time.

dt = Time.parse("20110725080000Z")
puts dt.localtime
Sun Jul 25 21:00:00 +1200 2011
puts dt.zone
"New Zealand Standard Time"

For DateTime class, where is the equivalent of asking for my local time without specifying an exact (and possibly incorrect timezone)?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文