I writing a Ruby program. I need a way to get something like America/New_York as ActiveSupport::TimeZone and require "tzinfo" can't get my current timezone. (Note that Time.now.zone can get things like EST but I need the tz database format, e.g. America/New_York.) Though the eazy way to do this is to just have the user set $TZ, TZ='America/New_York'; export TZ or tzselect. I'm looking for a way to do this without having the user set TZ. Is there some system file I can load, or a network location I can query?

I need the timezone for things like.

require "active_support/core_ext/time/zones"

Time.zone = ENV['TZ']
p Time.zone.now # Returns current time in the local time zone using ActiveSupport.
  • 1
    Why do you think you need America/New_York? Also, what distribution are you on? – Random832 Oct 30 '12 at 15:16
  • I'm on Xubuntu 12.04 and I need the user's local time zone, not necessarily America/New_York. – Christopher Oct 30 '12 at 16:25
  • Why? How is it going to be useful to you? That's just the name of a file that may contain anything. Wouldn't strftime("%z") be more useful? – Stéphane Chazelas Oct 30 '12 at 21:12
  • I wish it was, but active support requires the Olsen/"tz database" format the $TZ environment variable is required for my app now but I may switch to geolocation. – Christopher Oct 31 '12 at 1:05

On glibc, the default timezone is kept in /etc/localtime. This may be a symlink to a file in the tz database (in which case you could simply readlink), but there is no guarantee - it may be a hardlink or even a copy. In that case, you would have to read the file and then compare it to all the files in the tz data directory.

I am not sure what purpose you think getting the value America/New_York will serve.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.