Class: TZInfo::DataTimezoneInfo
- Inherits:
-
TimezoneInfo
- Object
- TimezoneInfo
- TZInfo::DataTimezoneInfo
- Defined in:
- lib/tzinfo/data_timezone_info.rb
Overview
Represents a (non-linked) timezone defined in a data module.
Instance Attribute Summary
Attributes inherited from TimezoneInfo
Instance Method Summary collapse
-
#initialize(identifier) ⇒ DataTimezoneInfo
constructor
Constructs a new TimezoneInfo with its identifier.
-
#offset(id, utc_offset, std_offset, abbreviation) ⇒ Object
Defines a offset.
-
#period_for_utc(utc) ⇒ Object
Returns the TimezonePeriod for the given UTC time.
-
#periods_for_local(local) ⇒ Object
Returns the set of TimezonePeriods for the given local time as an array.
-
#transition(year, month, offset_id, numerator_or_time, denominator = nil) ⇒ Object
Defines a transition.
Methods inherited from TimezoneInfo
Constructor Details
#initialize(identifier) ⇒ DataTimezoneInfo
Constructs a new TimezoneInfo with its identifier.
39 40 41 42 43 44 45 |
# File 'lib/tzinfo/data_timezone_info.rb', line 39 def initialize(identifier) super(identifier) @offsets = {} @transitions = [] @previous_offset = nil @transitions_index = nil end |
Instance Method Details
#offset(id, utc_offset, std_offset, abbreviation) ⇒ Object
Defines a offset. The id uniquely identifies this offset within the timezone. utc_offset and std_offset define the offset in seconds of standard time from UTC and daylight savings from standard time respectively. abbreviation describes the timezone offset (e.g. GMT, BST, EST or EDT).
The first offset to be defined is treated as the offset that applies until the first transition. This will usually be in Local Mean Time (LMT).
ArgumentError will be raised if the id is already defined.
57 58 59 60 61 62 63 |
# File 'lib/tzinfo/data_timezone_info.rb', line 57 def offset(id, utc_offset, std_offset, abbreviation) raise ArgumentError, 'Offset already defined' if @offsets.has_key?(id) offset = TimezoneOffsetInfo.new(utc_offset, std_offset, abbreviation) @offsets[id] = offset @previous_offset = offset unless @previous_offset end |
#period_for_utc(utc) ⇒ Object
Returns the TimezonePeriod for the given UTC time. Raises NoOffsetsDefined if no offsets have been defined.
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 141 142 |
# File 'lib/tzinfo/data_timezone_info.rb', line 104 def period_for_utc(utc) unless @transitions.empty? utc = TimeOrDateTime.wrap(utc) index = transition_index(utc.year, utc.mon) start_transition = nil start = transition_before_end(index) if start start.downto(0) do |i| if @transitions[i].at <= utc start_transition = @transitions[i] break end end end end_transition = nil start = transition_after_start(index) if start start.upto(@transitions.length - 1) do |i| if @transitions[i].at > utc end_transition = @transitions[i] break end end end if start_transition || end_transition TimezonePeriod.new(start_transition, end_transition) else # Won't happen since there are transitions. Must always find one # transition that is either >= or < the specified time. raise 'No transitions found in search' end else raise NoOffsetsDefined, 'No offsets have been defined' unless @previous_offset TimezonePeriod.new(nil, nil, @previous_offset) end end |
#periods_for_local(local) ⇒ Object
Returns the set of TimezonePeriods for the given local time as an array.
Results returned are ordered by increasing UTC start date. Returns an empty array if no periods are found for the given time. Raises NoOffsetsDefined if no offsets have been defined.
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/tzinfo/data_timezone_info.rb', line 148 def periods_for_local(local) unless @transitions.empty? local = TimeOrDateTime.wrap(local) index = transition_index(local.year, local.mon) result = [] start_index = transition_after_start(index - 1) if start_index && @transitions[start_index].local_end > local if start_index > 0 if @transitions[start_index - 1].local_start <= local result << TimezonePeriod.new(@transitions[start_index - 1], @transitions[start_index]) end else result << TimezonePeriod.new(nil, @transitions[start_index]) end end end_index = transition_before_end(index + 1) if end_index start_index = end_index unless start_index start_index.upto(transition_before_end(index + 1)) do |i| if @transitions[i].local_start <= local if i + 1 < @transitions.length if @transitions[i + 1].local_end > local result << TimezonePeriod.new(@transitions[i], @transitions[i + 1]) end else result << TimezonePeriod.new(@transitions[i], nil) end end end end result else raise NoOffsetsDefined, 'No offsets have been defined' unless @previous_offset [TimezonePeriod.new(nil, nil, @previous_offset)] end end |
#transition(year, month, offset_id, numerator_or_time, denominator = nil) ⇒ Object
Defines a transition. Transitions must be defined in chronological order. ArgumentError will be raised if a transition is added out of order. offset_id refers to an id defined with offset. ArgumentError will be raised if the offset_id cannot be found. numerator_or_time and denomiator specify the time the transition occurs as. See TimezoneTransitionInfo for more detail about specifying times.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/tzinfo/data_timezone_info.rb', line 71 def transition(year, month, offset_id, numerator_or_time, denominator = nil) offset = @offsets[offset_id] raise ArgumentError, 'Offset not found' unless offset if @transitions_index if year < @last_year || (year == @last_year && month < @last_month) raise ArgumentError, 'Transitions must be increasing date order' end # Record the position of the first transition with this index. index = transition_index(year, month) @transitions_index[index] ||= @transitions.length # Fill in any gaps (index - 1).downto(0) do |i| break if @transitions_index[i] @transitions_index[i] = @transitions.length end else @transitions_index = [@transitions.length] @start_year = year @start_month = month end @transitions << TimezoneTransitionInfo.new(offset, @previous_offset, numerator_or_time, denominator) @last_year = year @last_month = month @previous_offset = offset end |