Parses tzdata from elsie.nci.nih.gov/pub/ and transforms it into a set of Ruby modules that can be used through Timezone and Country.
Normally, this class wouldn't be used. It is only run to update the timezone data and index modules.
Maximum year that will be considered.
Minimum year that will be considered.
Zones to exclude from generation when not using #only_zones (set to an array containing zone identifiers).
Whether to generate country definitions (set to false to stop countries being generated).
Whether to generate zone definitions (set to false to stop zones being generated).
Limit the set of zones to generate (set to an array containing zone identifiers).
Initializes a new TZDataParser. input_dir must contain the extracted tzdata tarball. output_dir is the location to output the modules (in definitions and indexes directories).
# File lib/tzinfo/tzdataparser.rb, line 59 def initialize(input_dir, output_dir) super() @input_dir = input_dir @output_dir = output_dir @rule_sets = {} @zones = {} @countries = {} @no_rules = TZDataNoRules.new @generate_zones = true @generate_countries = true @only_zones = [] @exclude_zones = [] end
Parses a month specified in the tz data and converts it to a number between 1 and 12 representing January to December.
# File lib/tzinfo/tzdataparser.rb, line 114 def self.parse_month(month) lower = month.downcase if lower =~ %r^jan/ @month = 1 elsif lower =~ %r^feb/ @month = 2 elsif lower =~ %r^mar/ @month = 3 elsif lower =~ %r^apr/ @month = 4 elsif lower =~ %r^may/ @month = 5 elsif lower =~ %r^jun/ @month = 6 elsif lower =~ %r^jul/ @month = 7 elsif lower =~ %r^aug/ @month = 8 elsif lower =~ %r^sep/ @month = 9 elsif lower =~ %r^oct/ @month = 10 elsif lower =~ %r^nov/ @month = 11 elsif lower =~ %r^dec/ @month = 12 else raise "Invalid month: #{month}" end end
Parses an offset string [-]h:m:s (minutes and seconds are optional). Returns the offset in seconds.
# File lib/tzinfo/tzdataparser.rb, line 147 def self.parse_offset(offset) raise "Invalid time: #{offset}" if offset !~ %r^(-)?(?:([0-9]+)(?::([0-9]+)(?::([0-9]+))?)?)?$/ negative = !$1.nil? hour = $2.nil? ? 0 : $2.to_i minute = $3.nil? ? 0 : $3.to_i second = $4.nil? ? 0 : $4.to_i seconds = hour seconds = seconds * 60 seconds = seconds + minute seconds = seconds * 60 seconds = seconds + second seconds = -seconds if negative seconds end
Encloses the string in single quotes and escapes any single quotes in the content.
# File lib/tzinfo/tzdataparser.rb, line 166 def self.quote_str(str) "'#{str.gsub('\'', '\\\\\'')}'" end
Reads the tzdata source and generates the classes. Takes a long time to run. Currently outputs debugging information to standard out.
# File lib/tzinfo/tzdataparser.rb, line 75 def execute Dir.foreach(@input_dir) {|file| load_rules(file) if file =~ %r^[^\.]+$/ } Dir.foreach(@input_dir) {|file| load_zones(file) if file =~ %r^[^\.]+$/ } Dir.foreach(@input_dir) {|file| load_links(file) if file =~ %r^[^\.]+$/ } load_countries if @generate_zones modules = [] if @only_zones.nil? || @only_zones.empty? @zones.each_value {|zone| zone.write_module(@output_dir) unless @exclude_zones.include?(zone.name) } else @only_zones.each {|id| zone = @zones[id] zone.write_module(@output_dir) } end write_timezones_index end if @generate_countries write_countries_index end end