Rails 时区问题 [英] Rails Time zone issue
问题描述
这是我的控制台:
irb(main):048:0> Time.now
=> 2011-04-13 00:51:50 +0200
<c => (@stats[5] == '-' ? 0 : @stats[3]), :earn => @stats[6])
=> #<Reklamer id: 75, virksomhed: "Orville", dato: "2011-04-13 00:00:00", unik_k
lik: 0, klik: 0, unik_vis: 0, vis: 0, leads: 0, ordre: 0, earn: 0, created_at: "
2011-04-12 22:52:13", updated_at: "2011-04-12 22:52:13", cpc: 0>
irb(main):050:0>
正如你在我创建一个新项目时所看到的 updated_at 和 created_at 是2011-04-12 22:52:13"而不是正确的时间:2011-04-13 00:51:50
As you can see when I create a new item updated_at and created_at is "2011-04-12 22:52:13" instead of the correct time: 2011-04-13 00:51:50
推荐答案
irb(main):048:0> Time.now
=> 2011-04-13 00:51:50 +0200
created_at: 2011-04-12 22:52:13 # +0000
Rails 默认存储在 +0000 时区,而您当前的时区是 +0200
Rails stores at +0000 Time zone by default, while your current time zone is +0200
http://railscasts.com/episodes/106-time-zones-in-rails-2-1
相关主题:
UPD
为了理解.如果你设置 config.time_zone = 'Copenhagen'
是什么意思?
For understanding. If you set config.time_zone = 'Copenhagen'
what does it mean?
- 时间仍将存储为
UTC +0000
- 如果您调用 @object.created_at(或任何其他日期字段),它将偏移您的时间并返回您的当地时间.
示例(config.time_zone = 'Moscow' # +0400
)
object = Object.new
object.save
#=> #<Object id: 1, created_at: "2011-04-13 07:46:36", updated_at: "2011-04-13 07:46:36">
object.created_at
#=> Wed, 13 Apr 2011 11:46:36 MSD +04:00
为什么它以 +0000 UTC 存储时间?因为用户可以选择任何本地时区,所以它会自动偏移到用户时区.
Why does it store Time in +0000 UTC? Because user can choose any local time zone, so it will automaticaly offseted to users time zone.
这篇关于Rails 时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!