문제

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?

도움이 되었습니까?

해결책 2

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

다른 팁

  require 'tzinfo'
  tz = TZInfo::Timezone.get('America/New_York')
  tz.strftime("%Z")
  => "EST"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top