我在 Ubuntu 12.04 上,我可以看到:
$ cat /etc/timezone
America/Phoenix
相应地,Time
将返回一个非 UTC 时区的时间:
$ irb
> Time.now
=> 2013-03-27 13:44:49 -0700
> Time.at 0
=> 1969-12-31 17:00:00 -0700
我可以使用 TZ
环境变量覆盖系统时区:
$ TZ=UTC irb
> Time.now
=> 2013-03-27 20:47:19 +0000
> Time.at 0
=> 1970-01-01 00:00:00 +0000
无论如何,我可以在 Ruby 进程中以编程方式进行此更改吗?
最佳答案
您还可以通过访问 ENV
哈希从 ruby 中设置环境变量:
ENV['TZ'] = 'UTC'
Time.at 0
#=> 1970-01-01 00:00:00 +0000
另请参阅此答案:Set time zone offset in Ruby , 它提供了一种写类似
with_time_zone 'UTC' do
# do stuff
end
# now TZ is reset to system standard
关于ruby - 我可以覆盖 Ruby 中的系统时区吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15669166/