【问题标题】:ActiveRecord mapping date to TimeWithZone instead of DateTime with .000000 suffixActiveRecord 将日期映射到 TimeWithZone 而不是带有 .000000 后缀的 DateTime
【发布时间】:2016-05-21 11:22:13
【问题描述】:

如标题所示,程序将 DateTime 映射到 TimeWithZone 本身。

create_table :db_values do |t|
    t.datetime :created_date
end

来自数据库的记录: 2016-02-01T01:00:00+0000.000000

如何删除该后缀? '.000000' 我用过:

Time::DATE_FORMATS[:db]= lambda { |time|  time.strftime("%Y-%m-%dT%H:%M:%S%z") }

【问题讨论】:

  • 如何从日期格式中删除“.000000”?

标签: ruby datetime activerecord rails-activerecord activesupport


【解决方案1】:

Rails 会将 UTC 时区的所有时间/时间戳保存到您的数据库中。这样,当您在其他时区给它一个时间时,更容易准确地知道它应该在什么时间进行比较。因此,不要尝试将 DB 值更改为分区时间值。顺其自然……

在显示本地化时间时,只需使用辅助方法I18n.l Time.current

这里有几个关于timezones in Railsgood articles

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多