Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I override the system timezone in Ruby?

I'm here on Ubuntu 12.04, and I can see:

$ cat /etc/timezone 
America/Phoenix

Accordingly Time will return a time with a non-UTC zone:

$ irb
> Time.now
=> 2013-03-27 13:44:49 -0700
> Time.at 0
=> 1969-12-31 17:00:00 -0700

I can override the system time zone using the TZ environment variable:

$ TZ=UTC irb
> Time.now
=> 2013-03-27 20:47:19 +0000
> Time.at 0
=> 1970-01-01 00:00:00 +0000

Is there anyway I can make this change programmatically, within a Ruby process?

like image 243
davetapley Avatar asked Mar 27 '13 20:03

davetapley


People also ask

Where are Ruby environment variables stored?

You store separate environment variables in config/development. rb , config/testing. rb and config/production. rb respectively.


1 Answers

You can also set environment variables from within ruby by accessing the ENV hash:

ENV['TZ'] = 'UTC'
Time.at 0
#=> 1970-01-01 00:00:00 +0000

also see this answer: Set time zone offset in Ruby, It provides a way to write something like

with_time_zone 'UTC' do
  # do stuff
end

# now TZ is reset to system standard
like image 106
Patrick Oscity Avatar answered Nov 03 '22 00:11

Patrick Oscity