Class: Timezone::Zone
- Inherits:
-
Object
- Object
- Timezone::Zone
- Includes:
- Comparable
- Defined in:
- lib/timezone/zone.rb
Instance Attribute Summary collapse
-
#rules ⇒ Object
Returns the value of attribute rules.
-
#zone ⇒ Object
Returns the value of attribute zone.
Instance Method Summary collapse
-
#<=>(zone) ⇒ Object
:nodoc:.
-
#initialize(options) ⇒ Zone
constructor
Create a new Timezone object.
-
#time(reference) ⇒ Object
Determine the time in the timezone.
-
#utc_offset(reference = Time.now) ⇒ Object
Get the current UTC offset in seconds for this timezone.
Constructor Details
#initialize(options) ⇒ Zone
Create a new Timezone object.
Timezone.new()
:zone - The actual name of the zone. For example, Australia/Sydney or Americas/Los_Angeles. :lat, :lon - The latitude and longitude of the location. :latlon - The array of latitude and longitude of the location.
If a latitude and longitude is passed in, the Timezone object will do a lookup for the actual zone name and then use that as a reference. It will then load the appropriate json timezone information for that zone, and compile a list of the timezone rules.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/timezone/zone.rb', line 24 def initialize if .has_key?(:lat) && .has_key?(:lon) [:zone] = timezone_id [:lat], [:lon] elsif .has_key?(:latlon) [:zone] = timezone_id *[:latlon] end raise Timezone::Error::NilZone, 'No zone was found. Please specify a zone.' if [:zone].nil? file = File.join File.(File.dirname(__FILE__)+'/../../data'), "#{[:zone]}.json" raise Timezone::Error::InvalidZone, "'#{[:zone]}' is not a valid zone." unless File.exists?(file) data = JSON.parse(open(file).read) @rules = data['zone'] @zone = data['_zone'] || [:zone] end |
Instance Attribute Details
#rules ⇒ Object
Returns the value of attribute rules.
11 12 13 |
# File 'lib/timezone/zone.rb', line 11 def rules @rules end |
#zone ⇒ Object
Returns the value of attribute zone.
11 12 13 |
# File 'lib/timezone/zone.rb', line 11 def zone @zone end |
Instance Method Details
#<=>(zone) ⇒ Object
:nodoc:
61 62 63 |
# File 'lib/timezone/zone.rb', line 61 def <=> zone #:nodoc: utc_offset <=> zone.utc_offset end |
#time(reference) ⇒ Object
Determine the time in the timezone.
timezone.time(reference)
reference - The Time you want to convert.
The reference is converted to a UTC equivalent. That UTC equivalent is then used to lookup the appropriate offset in the timezone rules. Once the offset has been found that offset is added to the reference UTC time to calculate the reference time in the timezone.
50 51 52 |
# File 'lib/timezone/zone.rb', line 50 def time reference reference.utc + rule_for_reference(reference)['offset'] end |
#utc_offset(reference = Time.now) ⇒ Object
Get the current UTC offset in seconds for this timezone.
timezone.utc_offset(reference)
57 58 59 |
# File 'lib/timezone/zone.rb', line 57 def utc_offset reference=Time.now rule_for_reference(reference)['offset'] end |