rubytimezonetzinfo

How do I retrieve timezone code from a Ruby Timezone object?


I'm using Ruby 1.9.3 with gems timezone 0.3.1 and tzinfo 0.3.38. I'm looking to retrieve the current timezone code for a timezone, e.g. 'EST' for 'America/New_York' right now, or 'EDT' in the summer. I've found nothing in the documentation, yet when I dump the object with the following code:

ptz = Timezone::Zone.new :zone => 'America/New_York'
hash = {}
ptz.instance_variables.each do |var|
    hash[var.to_s.tr('@','')] = ptz.instance_variable_get var
end
puts hash.to_json

there is an array of 267 "rules", each a hash where "name" contains the value I am looking. Does anyone know how I should determine which rule is current, so I can retrieve the name value?


Solution

  • TZInfo::Timezone.get('America/New_York').period_for_utc(Time.now).abbreviation gives you the abbr. for the time provided.