Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

  require 'tzinfo'
  tz = TZInfo::Timezone.get('America/New_York')
  tz.strftime("%Z")
  => "EST"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top