简体   繁体   English

为什么`config.time_zone`似乎没有做任何事情?

[英]Why doesn't `config.time_zone` seem to do anything?

In application.rb , it says: application.rb ,它说:

Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 将Time.zone默认设置为指定区域,并将Active Record自动转换为此区域。 Run "rake -D time" for a list of tasks for finding time zone names. 运行“rake -D time”以获取用于查找时区名称的任务列表。 Default is UTC. 默认为UTC。

But setting config.time_zone = 'Central Time (US & Canada)' or config.time_zone = 'Eastern Time (US & Canada)' has no effect - the created_at field in a model is stil being saved in UTC. 但是设置config.time_zone = 'Central Time (US & Canada)'config.time_zone = 'Eastern Time (US & Canada)'没有效果 - 模型中的created_at字段是以UTC格式保存的。

According to this railsforum answer : 根据这个railsforum回答

config.time_zone just lets rails know that your server is set to this timezone so when it writes dates to the database it can properly convert it to UTC. config.time_zone只是让rails知道您的服务器设置为此时区,因此当它将日期写入数据库时​​,它可以正确地将其转换为UTC。

If that is true, then why is it that when my system time is Pacific Time (US & Canada) and config.time_zone = 'Central Time (US & Canada)' or config.time_zone = 'Eastern Time (US & Canada)' , that the created_at time is the correct UTC? 如果这是真的,那么为什么我的系统时间是Pacific Time (US & Canada)config.time_zone = 'Central Time (US & Canada)'config.time_zone = 'Eastern Time (US & Canada)'created_at时间是正确的UTC? Should it not be incorrect?! 它应该不正确吗?!

Because, if the PST time is 8 PM, then EST is 11 PM and UTC is 4 AM. 因为,如果PST时间是晚上8点,则EST是晚上11点,UTC是凌晨4点。 Presuming that Rails does Time.now , that would be 8 PM. 假设Rails确实是Time.now ,那将是晚上8点。 And we told Rails that the server is in EST. 我们告诉Rails服务器在EST中。 So, 8 PM would be EST time as far as Rails is concerned and the UTC would then be 5 AM UTC, which would be incorrect (because the actual time is 8 PM PST/11 PM EST, which is 4 AM UTC) 因此,就Rails而言,晚上8点将是EST时间,然后UTC将是UTC时间上午5点,这将是不正确的(因为实际时间是太平洋标准时间晚上8点/美国东部时间晚上11点,即UTC时间上午4点)

What's going on here? 这里发生了什么?

Here's a list of concepts and things that may help you: 以下是可能对您有所帮助的概念和事项列表:

config.time_zone doesn't set "Server Time", that is controlled by your operating system usually. config.time_zone不设置“服务器时间”,通常由操作系统控制。

Rails always stores your dates in UTC in the database (unless you change a different setting). Rails始终将您的日期以UTC格式存储在数据库中(除非您更改其他设置)。

Time.now returns the localtime for your computer in your timezone and it also includes the local timezone offset from your operating system, which means Ruby and therefore Rails knows how to convert localtime into UTC. Time.now在您的时区中返回计算机的本地时间,它还包括操作系统的本地时区偏移量,这意味着Ruby和Rails知道如何将localtime转换为UTC。 You can try this by using irb directly, so no Rails libraries are loaded: 你可以直接使用irb来试试这个,所以没有加载Rails库:

ctcherry$ irb
>> Time.now
=> Mon Feb 21 20:53:14 -0800 2011
>> 

If config.time_zone , or Time.zone is set, to lets say EST, Rails expects that if you set a datetime attribute that you mean for that time and date to be in specified timezone, in this case EST. 如果设置了config.time_zoneTime.zone ,让我们说EST,Rails希望如果你设置一个datetime属性,你指的是该时间和日期在指定的时区,在这种情况下是EST。 This is why you set Time.zone equal to your end users timezone, so they can use their local time and dates, and you can pass them directly into your ActiveRecord models and Rails can convert it to UTC for storage in the database. 这就是为什么你将Time.zone设置为等于你的最终用户时区,这样他们就可以使用他们的本地时间和日期,你可以将它们直接传递给你的ActiveRecord模型,Rails可以将它转换成UTC存储在数据库中。

Does that help at all? 这些帮助有用?

您需要使用in_time_zone (即Time.now.in_time_zone )来获取除UTC之外的其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 'config.time_zone' 不适用于加载到 text_field 中的日期时间数据。 使固定? - Rails 'config.time_zone' doesn't apply to datetime data loaded into a text_field. Fix? 由于config.time_zone,Rake无法正常工作 - Rake not working because of config.time_zone 如何在生产环境中配置Rails以使用config.time_zone而不是服务器时区? - How do I configure Rails in production to use config.time_zone instead of the server time zone? Rails:将config.time_zone设置为服务器的时区吗? - Rails: Set config.time_zone to the server's time zone? 如何使用与config.time_zone中设置的时区不同的时区保存日期时间 - How do I save a datetime with a different timezone than what is set in config.time_zone 在实时Rails应用程序中更改config.time_zone有什么含义? - What are the implications of changing config.time_zone in a live Rails app? Rails 4通过表单设置config.time_zone - Rails 4 set config.time_zone through form 如何将Rails config.time_zone设置为GMT +6? - How to set Rails config.time_zone to GMT +6? Rails config.time_zone和datetime属性更新 - Rails config.time_zone and datetime attribute update 在Heroku上使用config.time_zone的Rails,Rake计划的任务和DateTime - Rails, Rake scheduled tasks and DateTime with config.time_zone on Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM