How to get abbreviation of daylight saving and without daylight saving country? #17

Closed
user1045 opened this Issue Apr 16, 2014 · 2 comments

Comments

Projects
None yet
3 participants
@user1045

TZInfo::Timezone.get('America/New_York').current_period
When i run this i get this information

#<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionInfo: #<TZInfo::TimeOrDateTime: 1394348400>,#<TZInfo::TimezoneOffsetInfo: -18000,3600,EDT>>,#<TZInfo::TimezoneTransitionInfo: #<TZInfo::TimeOrDateTime: 1414908000>,#<TZInfo::TimezoneOffsetInfo: -18000,0,EST>>> 

i want abbreviation EDT and EST how i eill get this

@user1045 user1045 changed the title from How to get abbreviation of daylight saving and without daylight saving? to How to get abbreviation of daylight saving and without daylight saving country? Apr 16, 2014

@philr philr added the question label Apr 16, 2014

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Apr 16, 2014

Member

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 offsets_up_to method:

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 period_for_utc method:

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 ["EST", "EDT"].

Member

philr commented 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 offsets_up_to method:

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 period_for_utc method:

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 ["EST", "EDT"].

@philr philr closed this Apr 16, 2014

@davispuh

This comment has been minimized.

Show comment
Hide comment
@davispuh

davispuh Apr 16, 2014

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment