ActiveRecord不尊重夏令时(DST)?

时间:2013-04-10 08:34:56

标签: ruby-on-rails datetime activerecord timezone dst

我们位于时区Bern,即+0100。但由于我们现在处于夏季(我们有夏令时),因此当前偏移为+0200。在我的rails应用程序中,我使用应用程序控制器中的包装器设置时区,因为我需要基于用户的时区:

around_filter :user_timezone

def user_timezone(&block)
  Time.use_zone(current_timezone, &block)
end

现在奇怪的部分:

Time.zone.now                # 2013-04-10 10:32:56 +0200
                             # (correct offset)

SomeArModel.first.created_at # 2013-03-28 17:49:59 +0100
                             # (incorrect offset, no DST)

对此有任何解释吗?

1 个答案:

答案 0 :(得分:1)

这是正常的行为,DST的变化发生在Sun Mar 31 01:00:00 UTC 2013

t = Time.mktime(2013, 03, 31, 1, 15, 0)

6.times do
  t += 900
  u = Time.at(t.to_i).utc
  puts t.to_s  + " " + u.to_s
end

输出:

Sun Mar 31 01:30:00 +0100 2013 Sun Mar 31 00:30:00 UTC 2013
Sun Mar 31 01:45:00 +0100 2013 Sun Mar 31 00:45:00 UTC 2013
Sun Mar 31 03:00:00 +0200 2013 Sun Mar 31 01:00:00 UTC 2013
Sun Mar 31 03:15:00 +0200 2013 Sun Mar 31 01:15:00 UTC 2013
Sun Mar 31 03:30:00 +0200 2013 Sun Mar 31 01:30:00 UTC 2013
Sun Mar 31 03:45:00 +0200 2013 Sun Mar 31 01:45:00 UTC 2013