Class: TZInfo::TZDataParser
- Inherits:
-
Object
- Object
- TZInfo::TZDataParser
- Defined in:
- lib/tzinfo/tzdataparser.rb
Overview
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.
Constant Summary collapse
- MIN_YEAR =
Minimum year that will be considered.
1800
- MAX_YEAR =
Maximum year that will be considered.
2050
Instance Attribute Summary collapse
-
#exclude_zones ⇒ Object
Zones to exclude from generation when not using only_zones (set to an array containing zone identifiers).
-
#generate_countries ⇒ Object
Whether to generate country definitions (set to false to stop countries being generated).
-
#generate_zones ⇒ Object
Whether to generate zone definitions (set to false to stop zones being generated).
-
#only_zones ⇒ Object
Limit the set of zones to generate (set to an array containing zone identifiers).
Class Method Summary collapse
-
.parse_month(month) ⇒ Object
Parses a month specified in the tz data and converts it to a number between 1 and 12 representing January to December.
-
.parse_offset(offset) ⇒ Object
Parses an offset string [-]h:m:s (minutes and seconds are optional).
-
.quote_str(str) ⇒ Object
Encloses the string in single quotes and escapes any single quotes in the content.
Instance Method Summary collapse
-
#execute ⇒ Object
Reads the tzdata source and generates the classes.
-
#initialize(input_dir, output_dir) ⇒ TZDataParser
constructor
Initializes a new TZDataParser.
Constructor Details
#initialize(input_dir, output_dir) ⇒ TZDataParser
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).
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tzinfo/tzdataparser.rb', line 86 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 |
Instance Attribute Details
#exclude_zones ⇒ Object
Zones to exclude from generation when not using only_zones (set to an array containing zone identifiers).
81 82 83 |
# File 'lib/tzinfo/tzdataparser.rb', line 81 def exclude_zones @exclude_zones end |
#generate_countries ⇒ Object
Whether to generate country definitions (set to false to stop countries being generated).
73 74 75 |
# File 'lib/tzinfo/tzdataparser.rb', line 73 def generate_countries @generate_countries end |
#generate_zones ⇒ Object
Whether to generate zone definitions (set to false to stop zones being generated).
69 70 71 |
# File 'lib/tzinfo/tzdataparser.rb', line 69 def generate_zones @generate_zones end |
#only_zones ⇒ Object
Limit the set of zones to generate (set to an array containing zone identifiers).
77 78 79 |
# File 'lib/tzinfo/tzdataparser.rb', line 77 def only_zones @only_zones end |
Class Method Details
.parse_month(month) ⇒ Object
Parses a month specified in the tz data and converts it to a number between 1 and 12 representing January to December.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/tzinfo/tzdataparser.rb', line 144 def self.parse_month(month) lower = month.downcase if lower =~ /^jan/ @month = 1 elsif lower =~ /^feb/ @month = 2 elsif lower =~ /^mar/ @month = 3 elsif lower =~ /^apr/ @month = 4 elsif lower =~ /^may/ @month = 5 elsif lower =~ /^jun/ @month = 6 elsif lower =~ /^jul/ @month = 7 elsif lower =~ /^aug/ @month = 8 elsif lower =~ /^sep/ @month = 9 elsif lower =~ /^oct/ @month = 10 elsif lower =~ /^nov/ @month = 11 elsif lower =~ /^dec/ @month = 12 else raise "Invalid month: #{month}" end end |
.parse_offset(offset) ⇒ Object
Parses an offset string [-]h:m:s (minutes and seconds are optional). Returns the offset in seconds.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/tzinfo/tzdataparser.rb', line 177 def self.parse_offset(offset) raise "Invalid time: #{offset}" if offset !~ /^(-)?(?:([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 |
.quote_str(str) ⇒ Object
Encloses the string in single quotes and escapes any single quotes in the content.
196 197 198 |
# File 'lib/tzinfo/tzdataparser.rb', line 196 def self.quote_str(str) "'#{str.gsub('\'', '\\\\\'')}'" end |
Instance Method Details
#execute ⇒ Object
Reads the tzdata source and generates the classes. Takes a long time to run. Currently outputs debugging information to standard out.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/tzinfo/tzdataparser.rb', line 102 def execute # Note that the backzone file is ignored. backzone contains alternative # definitions of certain zones, primarily for pre-1970 data. It is not # recommended for ordinary use and the tzdata Makefile does not # install its entries by default. files = Dir.entries(@input_dir).select do |file| file =~ /\A[^\.]+\z/ && !%w(backzone calendars leapseconds version CONTRIBUTING LICENSE Makefile NEWS README SOURCE Theory version).include?(file) && File.file?(File.join(@input_dir, file)) end files.each {|file| load_rules(file) } files.each {|file| load_zones(file) } files.each {|file| load_links(file) } 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 |