Rails 如何知道我的时区? [英] How does Rails know my timezone?
问题描述
Rails 如何知道我的时区?我没有在 application.rb
中设置它:
How does Rails know my time zone? I don't have it set in application.rb
:
# config.time_zone = 'Central Time (US & Canada)'
我在整个应用程序中搜索了 time_zone
,这是它的唯一实例.我也没有看到时区设置的任何环境变量.我使用的是 Windows.
I searched the whole app for time_zone
and that is the only instance of it. I don't see any environment variables for time zone set either. I'm on Windows.
C:\Users\Chloe\workspace\MyBestBody>rails runner "p Time.now"
DL is deprecated, please use Fiddle
2015-06-12 23:38:33 -0400
它在部署到 Heroku 时打印 UTC 时间.
It prints UTC time when deployed to Heroku.
C:\Users\Chloe\workspace\MyBestBody>heroku run rails console
Running `rails console` attached to terminal... up, run.1949
Loading production environment (Rails 4.2.1)
irb(main):001:0> Time.new
=> 2015-06-13 03:28:34 +0000
Rails 4.2.1
推荐答案
从 gettimeofday() 开始,大约 ruby MRI 来源 让我相信:
From gettimeofday(), or so the ruby MRI source would have me believe:
int __cdecl
gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
filetime_to_timeval(&ft, tv);
return 0;
}
根据注释,这是一个 C 函数,由幕后的 ruby 日期例程调用.您不会自己调用该方法,而是调用您正在调用的 ruby 方法.
Per comment, this is a C function, called by the ruby date routines under the hood. You do not call the method yourself, rather you call the ruby methods you are calling.
这篇关于Rails 如何知道我的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!