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?
You can use
Time#gmtime
. For exampleTime#utc
also works and is an alias forTime#gmtime
Depending on the use case, ActiveSupport offers a lot of TimeZone related goodness.
ActiveSupport may be a larger dependency than you want, but you shouldn't overlook it.
You can also set environment variables from within ruby by accessing the
ENV
hash:also see this answer: Set time zone offset in Ruby, It provides a way to write something like