Method: Chronos::Zone.load

Defined in:
lib/chronos/zone.rb

.load(tabfile, marshalfile = nil, marshal = true) ⇒ Object

load locations from a tabfile



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chronos/zone.rb', line 167

def self.load(tabfile, marshalfile=nil, marshal=true)
	if marshalfile && File.readable?(marshalfile) && File.mtime(tabfile) <= File.mtime(marshalfile) then
		@by_name.update(Marshal.load(File.read(marshalfile)))
	else
		data = {}
		File.foreach(tabfile) { |line|
			next if line[0] == ?#
			tmp                    = line.chomp.split("\t")
			tmp[3,0]               = 0
			location               = new(*tmp)
			location.utc           = location.utc.to_sym
			location.offset        = Offset[location.utc]
			location.numeric       = Integer(location.numeric) rescue nil
			location.latitude      = Float(location.latitude) rescue nil
			location.longitude     = Float(location.longitude) rescue nil
			location.latitude_iso  = Integer(location.latitude_iso) rescue nil
			location.longitude_iso = Integer(location.longitude_iso) rescue nil
			data[location.timezone_id.downcase] = location
		}
		@by_name.update(data)
		if marshalfile && marshal then
			File.open(marshalfile, "wb") { |fh|
				fh.write(Marshal.dump(data))
			}
		end
	end
end