【问题标题】:rails' utc_to_local and daylight savings timerails 的 utc_to_local 和夏令时
【发布时间】:2011-04-29 12:18:14
【问题描述】:
> e = Event.first
> e.registration_start_utc  #registration_start_utc is a datetime column
 => Sat, 23 Oct 2010 06:38:00 UTC +00:00 
> e.registration_start_utc.utc?
 => true 
> ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(e.registration_start_utc)
 => Sat, 23 Oct 2010 02:38:00 UTC +00:00

关于此的 2 个问题:

1) 为什么最后一个输出显示“UTC” - 小时已转换 (6 => 2) 但它仍然显示 UTC。为什么不是 EST/EDT?

2) 夏令时切换并且纽约的偏移量从 -4 变为 -5 后会发生什么?数据库中的值没有改变,所以我唯一的结论是我的应用程序将开始在各处显示“1:38”而不是正确的 2:38?

我最关心的是这里的#2。 #1 更多的是一种好奇心。

谢谢!

【问题讨论】:

  • Phil 的推理请参见here

标签: ruby-on-rails timezone dst


【解决方案1】:

2) utc_to_local 使用日期来确定哪个偏移量是正确的,因此对于给定日期,输出将始终相同。

你可以这样测试:

t = Time.utc(2011,3, 14, 12)
# => 2011-03-14 12:00:00 UTC
t2 = Time.utc(2011,3, 11, 12)
# => 2011-03-11 12:00:00 UTC
ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(t)
# => 2011-03-14 08:00:00 UTC
ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(t2)
# => 2011-03-14 07:00:00 UTC

1) 我也觉得不合适。我的猜测是他们只对小时、分钟等的实际值感兴趣……而忽略时区。

无论如何,你最好使用:

e.registration_start_utc.in_time_zone("Eastern Time (US & Canada)")

另见this question我刚问过...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2016-09-19
    • 2012-10-23
    • 2016-02-02
    • 2010-09-19
    相关资源
    最近更新 更多