Skip to content

Instantly share code, notes, and snippets.

@wombleton
Created September 28, 2010 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wombleton/600245 to your computer and use it in GitHub Desktop.
Save wombleton/600245 to your computer and use it in GitHub Desktop.
Create timezone transition data from the list of timezones in the "zones" variable. Depends on the installed ruby's Olson data being accurate.
require 'rubygems'
require 'tzinfo'
require 'json'
# Run to generate Olson => goog.i18n.TimeZone data.
zones = %w(UTC Pacific/Auckland)
FIFTY_YEARS_FROM_NOW = DateTime.now + 365 * 50
MINUTES_IN_HOUR = 60
SECONDS_IN_HOUR = 60 * 60
def populate(zone, data)
limit = FIFTY_YEARS_FROM_NOW
names = data["names"] = []
transitions = data["transitions"] = []
d = 0
while true
period = zone.period_for_local(d)
period_start = period.utc_start
period_end = period.utc_end
offset = (period.utc_total_offset - period.utc_offset) / MINUTES_IN_HOUR
if names.empty? && offset == 0
names << period.abbreviation.to_s
names << period.abbreviation.to_s
end
if names.length == 2 && offset != 0
names << period.abbreviation.to_s
names << period.abbreviation.to_s
end
break if period_end.nil?
transitions << period_start.strftime("%s").to_i / SECONDS_IN_HOUR
transitions << offset
if period_end > limit
break
else
d = (period_end + 1).strftime("%s").to_i
end
end
end
puts zones.map {|z|
zone = TZInfo::Timezone.get(z)
output = {
"id" => zone.name,
"std_offset" => zone.period_for_utc(0).utc_offset / MINUTES_IN_HOUR
}
populate(zone, output)
output
}.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment