Ruby 1.8.6 中 DateTime 的时区转换
(卡在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论