铁路时区
我了解 Rails 的默认行为是将数据库记录和任何日期/日期时间相关列保存为 UTC 时间。我们可以通过environment.rb中的config.time_zone来控制向用户显示日期/日期时间。所以基本上 config.time_zone 设置是控制显示,rails 通过它将记录从 UTC 转换为我们在 config.time_zone 中指定的记录。
有没有什么方法或设置可以强制 Rails 以 EST 而不是 UTC 格式保存记录,并且仍然根据 config.time_zone 设置进行转换以显示?
-萨蒂诺斯
I understand that default behavior of rails is to save the database records and any date/datetime related columns to UTC time. And we can control the display of date/datetime to the users with config.time_zone in environment.rb. So basically config.time_zone setting is to control the display, by which rails converts records from UTC to the one we specify in config.time_zone.
Is there any way or setting to force rails to save the records in EST rather than UTC and still do the conversion to display based on the config.time_zone setting?
-Satynos
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该考虑问题背后的原因。
请记住:Rails 实现约定优于配置 - 并且您最好有充分的理由想要更改约定!特别是在像日期时间如何保存在数据库中这样随意的事情上,并且考虑到 Rails 使我们可以轻松地处理日期。
You should consider the reasoning behind your question.
Remember: Rails implements convention over configuration - and you'd better have a very good reason to want to change the convention! Especially on something as arbitrary as how datetime is saved in the database, and given how easy Rails makes it for us to work with dates.
我不知道如何强制将记录保存在 EST 中,但我想分享有关在数据库中使用本地时区的讨论: http://www.ruby-forum.com/topic/890711
从讨论中,我最终使用 skip_time_zone_conversion_for_attributes 来解决类似的问题(关于方法: http://api.rubyonrails.org/classes/ActiveRecord/Timestamp.html)。
您可能会感兴趣 - 也许将时区设置为 EST,然后尝试此方法。
祝你好运!我知道已经有一段时间了,但也许需要一些时间让人们知道你最终做了什么。
I don't know about forcing records to be saved in EST, but I wanted to share this discussion about using a local time zone in the db: http://www.ruby-forum.com/topic/890711
From the discussion, I ended up using skip_time_zone_conversion_for_attributes for a similar problem (about that method: http://api.rubyonrails.org/classes/ActiveRecord/Timestamp.html).
It may be of interest to you - maybe, set the Time zone to EST, and then try this method.
Good luck! I know it's been a while, but maybe take some time to let people know what you ended up doing.