Join GitHub today
How to get abbreviation of daylight saving and without daylight saving country? #17
i want abbreviation EDT and EST how i eill get this
changed the title from
How to get abbreviation of daylight saving and without daylight saving?
How to get abbreviation of daylight saving and without daylight saving country?
Apr 16, 2014
I assume that you are looking to get all the abbreviations in use over a year for a particular time zone?
If you are using TZInfo v1.1.0, you can use the
tz = TZInfo::Timezone.get('America/New_York') year = 2014 tz.offsets_up_to(Time.utc(year + 1, 1, 1), Time.utc(year, 1, 1)).collect do |o| o.abbreviation.to_s end
With earlier versions of TZInfo, you could check the abbreviation at a time in winter and a time in summer using the
tz = TZInfo::Timezone.get('America/New_York') year = 2014 [Time.utc(year, 1, 1), Time.utc(year, 7, 1)].collect do |t| tz.period_for_utc(t).abbreviation.to_s end
Both approaches will give the result
Alternatively you may use my TimezoneParser gem.
require 'timezone_parser' require 'date' abbrs =  # Will contain an hash of abbreviations and their offset list all_abbreviations = TimezoneParser::Data::Storage.Abbreviations # filter abbreviations by your requirements all_abbreviations.each do |abbr, offset_list| offset_list.each do |offset_info| # just ignore those, but if you want all abbreviations # even those which aren't in TZ db (and thus neither in TZInfo) # then there's Metazones instead next unless offset_info.has_key?('Timezones') # select abbreviations from 'America/New_York' which are still in effect if offset_info['Timezones'].include?('America/New_York') && offset_info['To'] >= DateTime.now abbrs << abbr break end end end # abbrs will be ["EDT", "EST"]
For structure you can look at data/abbreviations.yml