Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.
share|improve this question
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
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
This is what I was looking for: <stackoverflow.com/a/12523283/1058897>; this could be translated to ruby but I'll just require that $TZ is set. –  Christopher Oct 30 '12 at 16:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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