Class: Timezone::Zone
- Inherits:
-
Object
- Object
- Timezone::Zone
- Includes:
- Comparable
- Defined in:
- lib/timezone/zone.rb
Constant Summary collapse
- ZONE_FILE_PATH =
File.(File.dirname(__FILE__)+'/../../data')
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#zone ⇒ Object
readonly
Returns the value of attribute zone.
Class Method Summary collapse
-
.get_zone_data(zone) ⇒ Object
Retrieve the data from a particular time zone.
-
.list(*args) ⇒ Object
Get a list of specified timezones and the basic information accompanying that zone.
-
.names ⇒ Object
Instantly grab all possible time zone names.
Instance Method Summary collapse
-
#<=>(zone) ⇒ Object
:nodoc:.
- #active_support_time_zone ⇒ Object
-
#dst?(reference) ⇒ Boolean
Whether or not the time in the timezone is in DST.
-
#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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/timezone/zone.rb', line 27 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? data = Zone.get_zone_data([:zone]) @rules = data['zone'] @zone = data['_zone'] || [:zone] end |
Instance Attribute Details
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
12 13 14 |
# File 'lib/timezone/zone.rb', line 12 def rules @rules end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
12 13 14 |
# File 'lib/timezone/zone.rb', line 12 def zone @zone end |
Class Method Details
.get_zone_data(zone) ⇒ Object
Retrieve the data from a particular time zone
78 79 80 81 82 83 84 85 |
# File 'lib/timezone/zone.rb', line 78 def get_zone_data(zone) file = File.join(ZONE_FILE_PATH, "#{zone}.json") begin return JSON.parse(open(file).read) rescue raise Timezone::Error::InvalidZone, "'#{zone}' is not a valid zone." end end |
.list(*args) ⇒ Object
Get a list of specified timezones and the basic information accompanying that zone
zones = Timezone::Zone.list(*zones)
zones - An array of timezone names. (i.e. Timezone::Zones.list(“America/Chicago”, “Australia/Sydney”))
The result is a Hash of timezones with their title, offset in seconds, UTC offset, and if it uses DST.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/timezone/zone.rb', line 102 def list(*args) args = nil if args.empty? # set to nil if no args are provided zones = args || Configure.default_for_list || self.names # get default list list = self.names.select { |name| zones.include? name } # only select zones if they exist @zones = [] now = Time.now list.each do |zone| item = Zone.new(zone: zone) @zones << { :zone => item.zone, :title => Configure.replacements[item.zone] || item.zone, :offset => item.utc_offset, :utc_offset => (item.utc_offset/(60*60)), :dst => item.dst?(now) } end @zones.sort_by! { |zone| zone[Configure.order_list_by] } end |
.names ⇒ Object
Instantly grab all possible time zone names.
88 89 90 91 92 |
# File 'lib/timezone/zone.rb', line 88 def names @@names ||= Dir[File.join(ZONE_FILE_PATH, "**/**/*.json")].collect do |file| file.gsub("#{ZONE_FILE_PATH}/", '').gsub(".json", '') end end |
Instance Method Details
#<=>(zone) ⇒ Object
:nodoc:
71 72 73 |
# File 'lib/timezone/zone.rb', line 71 def <=> zone #:nodoc: utc_offset <=> zone.utc_offset end |
#active_support_time_zone ⇒ Object
42 43 44 |
# File 'lib/timezone/zone.rb', line 42 def active_support_time_zone @active_support_time_zone ||= Timezone::ActiveSupport.format(@zone) end |
#dst?(reference) ⇒ Boolean
Whether or not the time in the timezone is in DST.
60 61 62 |
# File 'lib/timezone/zone.rb', line 60 def dst?(reference) rule_for_reference(reference)['dst'] 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.
55 56 57 |
# File 'lib/timezone/zone.rb', line 55 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)
67 68 69 |
# File 'lib/timezone/zone.rb', line 67 def utc_offset reference=Time.now rule_for_reference(reference)['offset'] end |