Class: Chronos::Zone

Inherits:
Struct
  • Object
show all
Defined in:
lib/chronos/zone.rb,
lib/chronos/zone.rb

Constant Summary collapse

Inspect =
"#<%s:%s>"
Offset =

the UTC timezones, offset in seconds FIXME: as soon as rdoc stops sucking, use :‘<name>’ instead of ‘<name>’.to_sym (:stopdoc: proved useless, see earlier version in the git repo)

{
	"UTC-12".to_sym    => -43200, # I would love to use :'UTC-12', but rdoc fails with that
	"UTC-11".to_sym    => -39600,
	"UTC-10".to_sym    => -36000,
	"UTC-9:30".to_sym  => -34200,
	"UTC-9".to_sym     => -32400,
	"UTC-8".to_sym     => -28800,
	"UTC-7".to_sym     => -25200,
	"UTC-6".to_sym     => -21600,
	"UTC-5".to_sym     => -18000,
	"UTC-4:30".to_sym  => -16200,
	"UTC-4".to_sym     => -14400,
	"UTC-3:30".to_sym  => -12600,
	"UTC-3".to_sym     => -10800,
	"UTC-2".to_sym     =>  -7200,
	"UTC-1".to_sym     =>  -3600,
	"UTC".to_sym       =>      0,
	"UTC+1".to_sym     =>   3600,
	"UTC+2".to_sym     =>   7200,
	"UTC+3".to_sym     =>  10800,
	"UTC+3:07".to_sym  =>  11224,
	"UTC+3:30".to_sym  =>  12600,
	"UTC+4".to_sym     =>  14400,
	"UTC+4:30".to_sym  =>  16200,
	"UTC+5".to_sym     =>  18000,
	"UTC+5:30".to_sym  =>  19800,
	"UTC+5:45".to_sym  =>  20700,
	"UTC+6".to_sym     =>  21600,
	"UTC+6:30".to_sym  =>  23400,
	"UTC+7".to_sym     =>  25200,
	"UTC+8".to_sym     =>  28800,
	"UTC+8:45".to_sym  =>  31500,
	"UTC+9".to_sym     =>  32400,
	"UTC+9:30".to_sym  =>  34200,
	"UTC+10".to_sym    =>  36000,
	"UTC+10:30".to_sym =>  37800,
	"UTC+11".to_sym    =>  39600,
	"UTC+11:30".to_sym =>  41400,
	"UTC+12".to_sym    =>  43200,
	"UTC+12:45".to_sym =>  45900,
	"UTC+13".to_sym    =>  46800,
	"UTC+14".to_sym    =>  50400,
}
Map =

map old/military timezone names to UTC corresponding utc, isDST

Hash.new { |hash, key| key }.merge!({
	# for ::DateTime
	'-12:00'   => ['utc-12',   false],
	'-11:00'   => ['utc-11',   false],
	'-10:00'   => ['utc-10',   false],
	'-09:00'   => ['utc-9',    false],
	'-08:00'   => ['utc-8',    false],
	'-07:00'   => ['utc-7',    false],
	'-06:00'   => ['utc-6',    false],
	'-05:00'   => ['utc-5',    false],
	'-04:30'   => ['utc-4:30', false],
	'-04:00'   => ['utc-4',    false],
	'-03:00'   => ['utc-3',    false],
	'-03:30'   => ['utc-3:30', false],
	'-02:00'   => ['utc-2',    false],
	'-01:00'   => ['utc-1',    false],
	'+00:00'   => ['utc',      false],
	'+01:00'   => ['utc+1',    false],
	'+02:00'   => ['utc+2',    false],
	'+03:00'   => ['utc+3',    false],
	'+03:30'   => ['utc+3:30', false],
	'+04:00'   => ['utc+4',    false],
	'+05:00'   => ['utc+5',    false],
	'+05:30'   => ['utc+5:30', false],
	'+06:00'   => ['utc+6',    false],
	'+07:00'   => ['utc+7',    false],
	'+08:00'   => ['utc+8',    false],
	'+09:00'   => ['utc+9',    false],
	'+09:30'   => ['utc+9:30', false],
	'+10:00'   => ['utc+10',   false],
	'+11:00'   => ['utc+11',   false],
	'+12:00'   => ['utc+12',   false],
	# military & old
	'yankee'   => ['utc-12',   false],
	'xray'     => ['utc-11',   false],
	'hst'      => ['utc-10',   false],
	'whisky'   => ['utc-10',   false],
	'akst'     => ['utc-9',    false],
	'akdt'     => ['utc-9',    true],
	'ydt'      => ['utc-9',    true],
	'victor'   => ['utc-9',    false],
	'pst'      => ['utc-8',    false],
	'pdt'      => ['utc-8',    true],
	'uniform'  => ['utc-8',    false],
	'mst'      => ['utc-7',    false],
	'mdt'      => ['utc-7',    true],
	'tango'    => ['utc-7',    false],
	'cst'      => ['utc-6',    false],
	'cdt'      => ['utc-6',    true],
	'sierra'   => ['utc-6',    false],
	'est'      => ['utc-5',    false],
	'edt'      => ['utc-5',    true],
	'romeo'    => ['utc-5',    false],
	'vst'      => ['utc-4:30', false],
	'ast'      => ['utc-4',    false],
	'adt'      => ['utc-4',    true],
	'quebec'   => ['utc-4',    false],
	'nst'      => ['utc-3:30', false],
	'ndt'      => ['utc-3:30', true],
	'papa'     => ['utc-3',    false],
	'oscar'    => ['utc-2',    false],
	'november' => ['utc-1',    false],
	'gmt'      => ['utc',      false],
	'wet'      => ['utc',      false],
	'zulu'     => ['utc',      false],
	'cet'      => ['utc+1',    false],
	'cest'     => ['utc+1',    true],
	'alpha'    => ['utc+1',    false],
	'bravo'    => ['utc+2',    false],
	'msk'      => ['utc+3',    false],
	'charlie'  => ['utc+3',    false],
	'delta'    => ['utc+4',    false],
	'echo'     => ['utc+5',    false],
	'ist'      => ['utc+5:30', false],
	'foxtrot'  => ['utc+6',    false],
	'golf'     => ['utc+7',    false],
	'awst'     => ['utc+8',    false],
	'hotel'    => ['utc+8',    false],
	'india'    => ['utc+9',    false],
	'acst'     => ['utc+9:30', false],
	'aest'     => ['utc+10',   false],
	'kilo'     => ['utc+10',   false],
	'lima'     => ['utc+11',   false],
	'mike'     => ['utc+12',   false]
})

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alpha2Object

Returns the value of attribute alpha2

Returns:

  • (Object)

    the current value of alpha2



10
11
12
# File 'lib/chronos/zone.rb', line 10

def alpha2
  @alpha2
end

#alpha3Object

Returns the value of attribute alpha3

Returns:

  • (Object)

    the current value of alpha3



10
11
12
# File 'lib/chronos/zone.rb', line 10

def alpha3
  @alpha3
end

#countryObject

Returns the value of attribute country

Returns:

  • (Object)

    the current value of country



10
11
12
# File 'lib/chronos/zone.rb', line 10

def country
  @country
end

#dstruleObject

Returns the value of attribute dstrule

Returns:

  • (Object)

    the current value of dstrule



10
11
12
# File 'lib/chronos/zone.rb', line 10

def dstrule
  @dstrule
end

#languageObject

Returns the value of attribute language

Returns:

  • (Object)

    the current value of language



10
11
12
# File 'lib/chronos/zone.rb', line 10

def language
  @language
end

#latitudeObject

Returns the value of attribute latitude

Returns:

  • (Object)

    the current value of latitude



10
11
12
# File 'lib/chronos/zone.rb', line 10

def latitude
  @latitude
end

#latitude_isoObject

Returns the value of attribute latitude_iso

Returns:

  • (Object)

    the current value of latitude_iso



10
11
12
# File 'lib/chronos/zone.rb', line 10

def latitude_iso
  @latitude_iso
end

#longitudeObject

Returns the value of attribute longitude

Returns:

  • (Object)

    the current value of longitude



10
11
12
# File 'lib/chronos/zone.rb', line 10

def longitude
  @longitude
end

#longitude_isoObject

Returns the value of attribute longitude_iso

Returns:

  • (Object)

    the current value of longitude_iso



10
11
12
# File 'lib/chronos/zone.rb', line 10

def longitude_iso
  @longitude_iso
end

#numericObject

Returns the value of attribute numeric

Returns:

  • (Object)

    the current value of numeric



10
11
12
# File 'lib/chronos/zone.rb', line 10

def numeric
  @numeric
end

#offsetObject

Returns the value of attribute offset

Returns:

  • (Object)

    the current value of offset



10
11
12
# File 'lib/chronos/zone.rb', line 10

def offset
  @offset
end

#timezone_idObject

Returns the value of attribute timezone_id

Returns:

  • (Object)

    the current value of timezone_id



10
11
12
# File 'lib/chronos/zone.rb', line 10

def timezone_id
  @timezone_id
end

#utcObject

Returns the value of attribute utc

Returns:

  • (Object)

    the current value of utc



10
11
12
# File 'lib/chronos/zone.rb', line 10

def utc
  @utc
end

Class Method Details

.[](name) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/chronos/zone.rb', line 195

def self.[](name)
	name = name.to_s.downcase
	if zone = @by_name[name] then
		zone
	elsif mapped = Map[name]
		@by_name[mapped.first]
	end
end

.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

Instance Method Details

#inspectObject



208
209
210
# File 'lib/chronos/zone.rb', line 208

def inspect
	sprintf Inspect, self.class, timezone_id
end

#zone_namesObject



204
205
206
# File 'lib/chronos/zone.rb', line 204

def zone_names
	@by_name.keys
end