Class: TZInfo::DataSources::TransitionsDataTimezoneInfo
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
DataTimezoneInfo ,
TimezoneInfo
|
|
Instance Chain:
self,
DataTimezoneInfo ,
TimezoneInfo
|
|
Inherits: |
TZInfo::DataSources::DataTimezoneInfo
|
Defined in: | lib/tzinfo/data_sources/transitions_data_timezone_info.rb |
Overview
Represents a data time zone defined by a list of transitions that change the locally observed time.
Class Method Summary
-
.new(identifier, transitions) ⇒ TransitionsDataTimezoneInfo
constructor
Initializes a new
TransitionsDataTimezoneInfo
.
TimezoneInfo
- Inherited
.new | Initializes a new |
Instance Attribute Summary
Instance Method Summary
- #period_for(timestamp) ⇒ TimezonePeriod
-
#periods_for_local(local_timestamp) ⇒ Array<TimezonePeriod>
Returns an
Array
containing the TimezonePeriods that could be observed at the local time specified bylocal_timestamp
. -
#transitions_up_to(to_timestamp, from_timestamp = nil) ⇒ Array<TimezoneTransition>
Returns an
Array
of::TZInfo::TimezoneTransition
instances representing the times where the UTC offset of the time zone changes. -
#find_minimum_transition {|transition| ... } ⇒ Integer
private
Performs a binary search on #transitions to find the index of the earliest transition satisfying a condition.
-
#transition_on_or_after_timestamp?(transition, timestamp) ⇒ Boolean
private
Determines if a transition occurs at or after a given
::TZInfo::Timestamp
, taking the sub_second into consideration.
DataTimezoneInfo
- Inherited
#create_timezone, #period_for, | |
#periods_for_local | Returns an |
#transitions_up_to | Returns an |
#raise_not_implemented | Raises a |
TimezoneInfo
- Inherited
#create_timezone, #inspect, | |
#raise_not_implemented | Raises a |
Constructor Details
.new(identifier, transitions) ⇒ TransitionsDataTimezoneInfo
Initializes a new TransitionsDataTimezoneInfo
.
The passed in identifier
instance will be frozen. A reference to the
passed in Array
will be retained.
The #transitions Array
must be sorted in order of ascending
timestamp. Each transition must have a
timestamp_value that is greater
than the timestamp_value of the
prior transition.
# File 'lib/tzinfo/data_sources/transitions_data_timezone_info.rb', line 31
def initialize(identifier, transitions) super(identifier) raise ArgumentError, 'transitions must be specified' unless transitions raise ArgumentError, 'transitions must not be an empty Array' if transitions.empty? @transitions = transitions.freeze end
Instance Attribute Details
#transitions ⇒ Array
<TimezoneTransition> (readonly)
# File 'lib/tzinfo/data_sources/transitions_data_timezone_info.rb', line 11
attr_reader :transitions
Instance Method Details
#find_minimum_transition {|transition| ... } ⇒ Integer
(private)
Performs a binary search on #transitions to find the index of the earliest transition satisfying a condition.
:nocov_array_bsearch_index:
See additional method definition at line 159.
# File 'lib/tzinfo/data_sources/transitions_data_timezone_info.rb', line 179
def find_minimum_transition(&block) @transitions.bsearch_index(&block) end
#period_for(timestamp) ⇒ TimezonePeriod
# File 'lib/tzinfo/data_sources/transitions_data_timezone_info.rb', line 39
def period_for( ) raise ArgumentError, 'timestamp must be specified' unless raise ArgumentError, 'timestamp must have a specified utc_offset' unless .utc_offset = .value index = find_minimum_transition {|t| t. >= } if index transition = @transitions[index] if transition. == # timestamp occurs within the second of the found transition, so is # the transition that starts the period. start_transition = transition end_transition = @transitions[index + 1] else # timestamp occurs before the second of the found transition, so is # the transition that ends the period. start_transition = index == 0 ? nil : @transitions[index - 1] end_transition = transition end else start_transition = @transitions.last end_transition = nil end TransitionsTimezonePeriod.new(start_transition, end_transition) end
#periods_for_local(local_timestamp) ⇒ Array
<TimezonePeriod>
Returns an Array
containing the TimezonePeriods that
could be observed at the local time specified by local_timestamp
. The
results are are ordered by increasing UTC start date. An empty Array
is returned if no periods are found for the given local time.
# File 'lib/tzinfo/data_sources/transitions_data_timezone_info.rb', line 70
def periods_for_local( ) raise ArgumentError, 'local_timestamp must be specified' unless raise ArgumentError, 'local_timestamp must have an unspecified utc_offset' if .utc_offset = .value latest_possible_utc_value = + 86400 earliest_possible_utc_value = - 86400 # Find the index of the first transition that occurs after a latest # possible UTC representation of the local timestamp and then search # backwards until an earliest possible UTC representation. index = find_minimum_transition {|t| t. >= latest_possible_utc_value } # No transitions after latest_possible_utc_value, set to max index + 1 # to search backwards including the period after the last transition index = @transitions.length unless index result = [] index.downto(0) do |i| start_transition = i > 0 ? @transitions[i - 1] : nil end_transition = @transitions[i] offset = start_transition ? start_transition.offset : end_transition.previous_offset = - offset.observed_utc_offset # It is not necessary to compare the sub-seconds because a timestamp # is in the period if is >= the start transition (sub-seconds would # make == become >) and if it is < the end transition (which # sub-seconds cannot affect). if (!start_transition || >= start_transition. ) && (!end_transition || < end_transition. ) result << TransitionsTimezonePeriod.new(start_transition, end_transition) elsif end_transition && end_transition. < earliest_possible_utc_value break end end result.reverse! end
#transition_on_or_after_timestamp?(transition, timestamp) ⇒ Boolean
(private)
Determines if a transition occurs at or after a given ::TZInfo::Timestamp
,
taking the sub_second into consideration.
# File 'lib/tzinfo/data_sources/transitions_data_timezone_info.rb', line 207
def (transition, ) = transition. = .value > || == && .sub_second == 0 end
#transitions_up_to(to_timestamp, from_timestamp = nil) ⇒ Array
<TimezoneTransition>
Returns an Array
of ::TZInfo::TimezoneTransition
instances representing the
times where the UTC offset of the time zone changes.
Transitions are returned up to a given ::TZInfo::Timestamp
(to_timestamp
).
A from ::TZInfo::Timestamp
may also be supplied using the from_timestamp
parameter. If from_timestamp
is specified, only transitions from that
time onwards will be returned.
Comparisons with to_timestamp
are exclusive. Comparisons with
from_timestamp
are inclusive. If a transition falls precisely on
to_timestamp
, it will be excluded. If a transition falls on
from_timestamp
, it will be included.
Transitions returned are ordered by when they occur, from earliest to latest.
# File 'lib/tzinfo/data_sources/transitions_data_timezone_info.rb', line 111
def transitions_up_to(, = nil) raise ArgumentError, 'to_timestamp must be specified' unless raise ArgumentError, 'to_timestamp must have a specified utc_offset' unless .utc_offset if raise ArgumentError, 'from_timestamp must have a specified utc_offset' unless .utc_offset raise ArgumentError, 'to_timestamp must be greater than from_timestamp' if <= end if from_index = find_minimum_transition {|t| (t, ) } return [] unless from_index else from_index = 0 end to_index = find_minimum_transition {|t| (t, ) } if to_index return [] if to_index < 1 to_index -= 1 else to_index = -1 end @transitions[from_index..to_index] end