Rails 时区问题

发布于 2024-10-31 18:13:52 字数 528 浏览 0 评论 0原文

这是我的控制台:

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

Here is my console:

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>

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

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2024-11-07 18:13:52
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

http://railscasts.com/episodes/106 -time-zones-in-rails-2-1

相关主题:

UPD

用于理解。如果设置 config.time_zone = 'Copenhagen' 是什么意思?

  • 时间仍将存储为 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 存储时间?由于用户可以选择任何本地时区,因此它会自动偏移到用户时区。

irb(main):048:0> Time.now
=> 2011-04-13 00:51:50 +0200
created_at: 2011-04-12 22:52:13 # +0000

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

Related topics:

UPD

For understanding. If you set config.time_zone = 'Copenhagen' what does it mean?

  • Time will still stored as UTC +0000
  • If you cal @object.created_at (or any other date field) it will offset your time and return your local time.

Example (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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文