code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def is_time_like?(value) value.respond_to?(:to_i) && value.respond_to?(:subsec) end
Determines if an object is like a `Time` (for the purposes of converting to a {Timestamp} with {for}), responding to `to_i` and `subsec`. @param value [Object] an object to test. @return [Boolean] `true` if the object is `Time`-like, otherwise `false`.
is_time_like?
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def for_time_like(time_like, ignore_offset, target_utc_offset) value = time_like.to_i sub_second = time_like.subsec.to_r if ignore_offset utc_offset = target_utc_offset value += time_like.utc_offset.to_i if time_like.respond_to?(:utc_offset) elsif time_like.respond_to?(:utc_offset) utc_offset = time_like.utc_offset.to_i else utc_offset = 0 end new(value, sub_second, utc_offset) end
Creates a {Timestamp} that represents a given `Time`-like object, optionally ignoring the offset (if the `time_like` responds to `utc_offset`). @param time_like [Object] a `Time`-like object. @param ignore_offset [Boolean] whether to ignore the offset of `time`. @param target_utc_offset [Object] if `ignore_offset` is `true`, the UTC offset of the result (`:utc`, `nil` or an `Integer`). @return [Timestamp] the {Timestamp} representation of `time_like`.
for_time_like
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def initialize(value, sub_second = 0, utc_offset = nil) raise ArgumentError, 'value must be an Integer' unless value.kind_of?(Integer) raise ArgumentError, 'sub_second must be a Rational or the Integer 0' unless (sub_second.kind_of?(Integer) && sub_second == 0) || sub_second.kind_of?(Rational) raise RangeError, 'sub_second must be >= 0 and < 1' if sub_second < 0 || sub_second >= 1 raise ArgumentError, 'utc_offset must be an Integer, :utc or nil' if utc_offset && utc_offset != :utc && !utc_offset.kind_of?(Integer) initialize!(value, sub_second, utc_offset) end
Initializes a new {Timestamp}. @param value [Integer] the number of seconds since 1970-01-01 00:00:00 UTC ignoring leap seconds. @param sub_second [Numeric] the fractional part of the second as either a `Rational` that is greater than or equal to 0 and less than 1, or the `Integer` 0. @param utc_offset [Object] either `nil` for a {Timestamp} without a specified offset, an offset from UTC specified as an `Integer` number of seconds or the `Symbol` `:utc`). @raise [ArgumentError] if `value` is not an `Integer`. @raise [ArgumentError] if `sub_second` is not a `Rational`, or the `Integer` 0. @raise [RangeError] if `sub_second` is a `Rational` but that is less than 0 or greater than or equal to 1. @raise [ArgumentError] if `utc_offset` is not `nil`, not an `Integer` and not the `Symbol` `:utc`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def add_and_set_utc_offset(seconds, utc_offset) raise ArgumentError, 'seconds must be an Integer' unless seconds.kind_of?(Integer) raise ArgumentError, 'utc_offset must be an Integer, :utc or nil' if utc_offset && utc_offset != :utc && !utc_offset.kind_of?(Integer) return self if seconds == 0 && utc_offset == (@utc ? :utc : @utc_offset) Timestamp.send(:new!, @value + seconds, @sub_second, utc_offset) end
Adds a number of seconds to the {Timestamp} value, setting the UTC offset of the result. @param seconds [Integer] the number of seconds to be added. @param utc_offset [Object] either `nil` for a {Timestamp} without a specified offset, an offset from UTC specified as an `Integer` number of seconds or the `Symbol` `:utc`). @return [Timestamp] the result of adding `seconds` to the {Timestamp} value as a new {Timestamp} instance with the chosen `utc_offset`. @raise [ArgumentError] if `seconds` is not an `Integer`. @raise [ArgumentError] if `utc_offset` is not `nil`, not an `Integer` and not the `Symbol` `:utc`.
add_and_set_utc_offset
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def utc return self if @utc Timestamp.send(:new!, @value, @sub_second, :utc) end
@return [Timestamp] a UTC {Timestamp} equivalent to this instance. Returns `self` if {#utc? self.utc?} is `true`.
utc
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def to_time time = new_time if @utc_offset && !@utc time.localtime(@utc_offset) else time.utc end end
Converts this {Timestamp} to a `Time`. @return [Time] a `Time` representation of this {Timestamp}. If the UTC offset of this {Timestamp} is not specified, a UTC `Time` will be returned.
to_time
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def strftime(format) raise ArgumentError, 'format must be specified' unless format to_time.strftime(format) end
Formats this {Timestamp} according to the directives in the given format string. @param format [String] the format string. Please refer to `Time#strftime` for a list of supported format directives. @return [String] the formatted {Timestamp}. @raise [ArgumentError] if `format` is not specified.
strftime
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def to_s return value_and_sub_second_to_s unless @utc_offset return "#{value_and_sub_second_to_s} UTC" if @utc sign = @utc_offset >= 0 ? '+' : '-' min, sec = @utc_offset.abs.divmod(60) hour, min = min.divmod(60) "#{value_and_sub_second_to_s(@utc_offset)} #{sign}#{'%02d' % hour}:#{'%02d' % min}#{sec > 0 ? ':%02d' % sec : nil}#{@utc_offset != 0 ? " (#{value_and_sub_second_to_s} UTC)" : nil}" end
@return [String] a `String` representation of this {Timestamp}.
to_s
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def hash [@value, @sub_second, !!@utc_offset].hash end
@return [Integer] a hash based on the value, sub-second and whether there is a defined UTC offset.
hash
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def inspect "#<#{self.class}: @value=#{@value}, @sub_second=#{@sub_second}, @utc_offset=#{@utc_offset.inspect}, @utc=#{@utc.inspect}>" end
@return [String] the internal object state as a programmer-readable `String`.
inspect
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def new_time(klass = Time) klass.at(@value, @sub_second * 1_000_000) end
Creates a new instance of a `Time` or `Time`-like class matching the {value} and {sub_second} of this {Timestamp}, but not setting the offset. @param klass [Class] the class to instantiate. @private
new_time
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def new_datetime(klass = DateTime) # Can't specify the start parameter unless the jd parameter is an exact number of days. # Use #gregorian instead. datetime = klass.jd(JD_EPOCH + ((@value.to_r + @sub_second) / 86400)).gregorian @utc_offset && @utc_offset != 0 ? datetime.new_offset(Rational(@utc_offset, 86400)) : datetime end
Constructs a new instance of a `DateTime` or `DateTime`-like class with the same {value}, {sub_second} and {utc_offset} as this {Timestamp}. @param klass [Class] the class to instantiate. @private
new_datetime
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def value_and_sub_second_to_s(offset = 0) "#{@value + offset}#{sub_second_to_s}" end
Converts the value and sub-seconds to a `String`, adding on the given offset. @param offset [Integer] the offset to add to the value. @return [String] the value and sub-seconds.
value_and_sub_second_to_s
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def sub_second_to_s if @sub_second == 0 '' else " #{@sub_second.numerator}/#{@sub_second.denominator}" end end
Converts the {sub_second} value to a `String` suitable for appending to the `String` representation of a {Timestamp}. @return [String] a `String` representation of {sub_second}.
sub_second_to_s
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def initialize!(value, sub_second = 0, utc_offset = nil) @value = value # Convert Rational(0,1) to 0. @sub_second = sub_second == 0 ? 0 : sub_second if utc_offset @utc = utc_offset == :utc @utc_offset = @utc ? 0 : utc_offset else @utc = @utc_offset = nil end end
Initializes a new {Timestamp} without validating the parameters. This method is used internally within {Timestamp} to avoid the overhead of checking parameters. @param value [Integer] the number of seconds since 1970-01-01 00:00:00 UTC ignoring leap seconds. @param sub_second [Numeric] the fractional part of the second as either a `Rational` that is greater than or equal to 0 and less than 1, or the `Integer` 0. @param utc_offset [Object] either `nil` for a {Timestamp} without a specified offset, an offset from UTC specified as an `Integer` number of seconds or the `Symbol` `:utc`).
initialize!
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp.rb
MIT
def set_timezone_offset(timezone_offset) raise ArgumentError, 'timezone_offset must be specified' unless timezone_offset raise ArgumentError, 'timezone_offset.observed_utc_offset does not match self.utc_offset' if utc? || utc_offset != timezone_offset.observed_utc_offset @timezone_offset = timezone_offset self end
Sets the associated {TimezoneOffset} of this {TimestampWithOffset}. @param timezone_offset [TimezoneOffset] a {TimezoneOffset} valid at the time and for the offset of this {TimestampWithOffset}. @return [TimestampWithOffset] `self`. @raise [ArgumentError] if `timezone_offset` is `nil`. @raise [ArgumentError] if {utc? self.utc?} is `true`. @raise [ArgumentError] if `timezone_offset.observed_utc_offset` does not equal `self.utc_offset`.
set_timezone_offset
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp_with_offset.rb
MIT
def to_time to = timezone_offset if to new_time(TimeWithOffset).set_timezone_offset(to) else super end end
An overridden version of {Timestamp#to_time} that, if there is an associated {TimezoneOffset}, returns a {TimeWithOffset} with that offset. @return [Time] if there is an associated {TimezoneOffset}, a {TimeWithOffset} representation of this {TimestampWithOffset}, otherwise a `Time` representation.
to_time
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp_with_offset.rb
MIT
def to_datetime to = timezone_offset if to new_datetime(DateTimeWithOffset).set_timezone_offset(to) else super end end
An overridden version of {Timestamp#to_datetime}, if there is an associated {TimezoneOffset}, returns a {DateTimeWithOffset} with that offset. @return [DateTime] if there is an associated {TimezoneOffset}, a {DateTimeWithOffset} representation of this {TimestampWithOffset}, otherwise a `DateTime` representation.
to_datetime
ruby
tzinfo/tzinfo
lib/tzinfo/timestamp_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timestamp_with_offset.rb
MIT
def get_proxies(identifiers) identifiers.collect {|identifier| get_proxy(identifier)} end
@param [Enumerable<String>] identifiers an `Enumerable` of time zone identifiers. @return [Array<TimezoneProxy>] an `Array` of {TimezoneProxy} instances corresponding to the given identifiers.
get_proxies
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def name # Don't use alias, as identifier gets overridden. identifier end
@return [String] the identifier of the time zone, for example, `"Europe/Paris"`.
name
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def inspect "#<#{self.class}: #{identifier}>" end
@return [String] the internal object state as a programmer-readable `String`.
inspect
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def period_for_utc(utc_time) raise ArgumentError, 'utc_time must be specified' unless utc_time period_for(Timestamp.for(utc_time, :treat_as_utc)) end
Returns the {TimezonePeriod} that is valid at a given time. The UTC offset of the `utc_time` parameter is ignored (it is treated as a UTC time). Use the {period_for} method instead if the UTC offset of the time needs to be taken into consideration. @param utc_time [Object] a `Time`, `DateTime` or {Timestamp}. @return [TimezonePeriod] the {TimezonePeriod} that is valid at `utc_time`. @raise [ArgumentError] if `utc_time` is `nil`.
period_for_utc
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def period_for_local(local_time, dst = Timezone.default_dst) raise ArgumentError, 'local_time must be specified' unless local_time local_time = Timestamp.for(local_time, :ignore) results = periods_for_local(local_time) if results.empty? raise PeriodNotFound, "#{local_time.strftime('%Y-%m-%d %H:%M:%S')} is an invalid local time." elsif results.size < 2 results.first else # ambiguous result try to resolve if !dst.nil? matches = results.find_all {|period| period.dst? == dst} results = matches if !matches.empty? end if results.size < 2 results.first else # still ambiguous, try the block if block_given? results = yield results end if results.is_a?(TimezonePeriod) results elsif results && results.size == 1 results.first else raise AmbiguousTime, "#{local_time.strftime('%Y-%m-%d %H:%M:%S')} is an ambiguous local time." end end end end
Returns the {TimezonePeriod} that is valid at the given local time. The UTC offset of the `local_time` parameter is ignored (it is treated as a time in the time zone represented by `self`). Use the {period_for} method instead if the the UTC offset of the time needs to be taken into consideration. _Warning:_ There are local times that have no equivalent UTC times (for example, during the transition from standard time to daylight savings time). There are also local times that have more than one UTC equivalent (for example, during the transition from daylight savings time to standard time). In the first case (no equivalent UTC time), a {PeriodNotFound} exception will be raised. In the second case (more than one equivalent UTC time), an {AmbiguousTime} exception will be raised unless the optional `dst` parameter or block handles the ambiguity. If the ambiguity is due to a transition from daylight savings time to standard time, the `dst` parameter can be used to select whether the daylight savings time or local time is used. For example, the following code would raise an {AmbiguousTime} exception: tz = TZInfo::Timezone.get('America/New_York') tz.period_for_local(Time.new(2004,10,31,1,30,0)) Specifying `dst = true` would select the daylight savings period from April to October 2004. Specifying `dst = false` would return the standard time period from October 2004 to April 2005. The `dst` parameter will not be able to resolve an ambiguity resulting from the clocks being set back without changing from daylight savings time to standard time. In this case, if a block is specified, it will be called to resolve the ambiguity. The block must take a single parameter - an `Array` of {TimezonePeriod}s that need to be resolved. The block can select and return a single {TimezonePeriod} or return `nil` or an empty `Array` to cause an {AmbiguousTime} exception to be raised. The default value of the `dst` parameter can be specified using {Timezone.default_dst=}. @param local_time [Object] a `Time`, `DateTime` or {Timestamp}. @param dst [Boolean] whether to resolve ambiguous local times by always selecting the period observing daylight savings time (`true`), always selecting the period observing standard time (`false`), or leaving the ambiguity unresolved (`nil`). @yield [periods] if the `dst` parameter did not resolve an ambiguity, an optional block is yielded to. @yieldparam periods [Array<TimezonePeriod>] an `Array` containing all the {TimezonePeriod}s that still match `local_time` after applying the `dst` parameter. @yieldreturn [Object] to resolve the ambiguity: a chosen {TimezonePeriod} or an `Array` containing a chosen {TimezonePeriod}; to leave the ambiguity unresolved: an empty `Array`, an `Array` containing more than one {TimezonePeriod}, or `nil`. @return [TimezonePeriod] the {TimezonePeriod} that is valid at `local_time`. @raise [ArgumentError] if `local_time` is `nil`. @raise [PeriodNotFound] if `local_time` is not valid for the time zone (there is no equivalent UTC time). @raise [AmbiguousTime] if `local_time` was ambiguous for the time zone and the `dst` parameter or block did not resolve the ambiguity.
period_for_local
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def to_local(time) raise ArgumentError, 'time must be specified' unless time Timestamp.for(time) do |ts| TimestampWithOffset.set_timezone_offset(ts, period_for(ts).offset) end end
Converts a time to the local time for the time zone. The result will be of type {TimeWithOffset} (if passed a `Time`), {DateTimeWithOffset} (if passed a `DateTime`) or {TimestampWithOffset} (if passed a {Timestamp}). {TimeWithOffset}, {DateTimeWithOffset} and {TimestampWithOffset} are subclasses of `Time`, `DateTime` and {Timestamp} that provide additional information about the local result. Unlike {utc_to_local}, {to_local} takes the UTC offset of the given time into consideration. @param time [Object] a `Time`, `DateTime` or {Timestamp}. @return [Object] the local equivalent of `time` as a {TimeWithOffset}, {DateTimeWithOffset} or {TimestampWithOffset}. @raise [ArgumentError] if `time` is `nil`. @raise [ArgumentError] if `time` is a {Timestamp} that does not have a specified UTC offset.
to_local
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def utc_to_local(utc_time) raise ArgumentError, 'utc_time must be specified' unless utc_time Timestamp.for(utc_time, :treat_as_utc) do |ts| to_local(ts) end end
Converts a time in UTC to the local time for the time zone. The result will be of type {TimeWithOffset} (if passed a `Time`), {DateTimeWithOffset} (if passed a `DateTime`) or {TimestampWithOffset} (if passed a {Timestamp}). {TimeWithOffset}, {DateTimeWithOffset} and {TimestampWithOffset} are subclasses of `Time`, `DateTime` and {Timestamp} that provide additional information about the local result. The UTC offset of the `utc_time` parameter is ignored (it is treated as a UTC time). Use the {to_local} method instead if the the UTC offset of the time needs to be taken into consideration. @param utc_time [Object] a `Time`, `DateTime` or {Timestamp}. @return [Object] the local equivalent of `utc_time` as a {TimeWithOffset}, {DateTimeWithOffset} or {TimestampWithOffset}. @raise [ArgumentError] if `utc_time` is `nil`.
utc_to_local
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def local_to_utc(local_time, dst = Timezone.default_dst) raise ArgumentError, 'local_time must be specified' unless local_time Timestamp.for(local_time, :ignore) do |ts| period = if block_given? period_for_local(ts, dst) {|periods| yield periods } else period_for_local(ts, dst) end ts.add_and_set_utc_offset(-period.observed_utc_offset, :utc) end end
Converts a local time for the time zone to UTC. The result will either be a `Time`, `DateTime` or {Timestamp} according to the type of the `local_time` parameter. The UTC offset of the `local_time` parameter is ignored (it is treated as a time in the time zone represented by `self`). _Warning:_ There are local times that have no equivalent UTC times (for example, during the transition from standard time to daylight savings time). There are also local times that have more than one UTC equivalent (for example, during the transition from daylight savings time to standard time). In the first case (no equivalent UTC time), a {PeriodNotFound} exception will be raised. In the second case (more than one equivalent UTC time), an {AmbiguousTime} exception will be raised unless the optional `dst` parameter or block handles the ambiguity. If the ambiguity is due to a transition from daylight savings time to standard time, the `dst` parameter can be used to select whether the daylight savings time or local time is used. For example, the following code would raise an {AmbiguousTime} exception: tz = TZInfo::Timezone.get('America/New_York') tz.period_for_local(Time.new(2004,10,31,1,30,0)) Specifying `dst = true` would select the daylight savings period from April to October 2004. Specifying `dst = false` would return the standard time period from October 2004 to April 2005. The `dst` parameter will not be able to resolve an ambiguity resulting from the clocks being set back without changing from daylight savings time to standard time. In this case, if a block is specified, it will be called to resolve the ambiguity. The block must take a single parameter - an `Array` of {TimezonePeriod}s that need to be resolved. The block can select and return a single {TimezonePeriod} or return `nil` or an empty `Array` to cause an {AmbiguousTime} exception to be raised. The default value of the `dst` parameter can be specified using {Timezone.default_dst=}. @param local_time [Object] a `Time`, `DateTime` or {Timestamp}. @param dst [Boolean] whether to resolve ambiguous local times by always selecting the period observing daylight savings time (`true`), always selecting the period observing standard time (`false`), or leaving the ambiguity unresolved (`nil`). @yield [periods] if the `dst` parameter did not resolve an ambiguity, an optional block is yielded to. @yieldparam periods [Array<TimezonePeriod>] an `Array` containing all the {TimezonePeriod}s that still match `local_time` after applying the `dst` parameter. @yieldreturn [Object] to resolve the ambiguity: a chosen {TimezonePeriod} or an `Array` containing a chosen {TimezonePeriod}; to leave the ambiguity unresolved: an empty `Array`, an `Array` containing more than one {TimezonePeriod}, or `nil`. @return [Object] the UTC equivalent of `local_time` as a `Time`, `DateTime` or {Timestamp}. @raise [ArgumentError] if `local_time` is `nil`. @raise [PeriodNotFound] if `local_time` is not valid for the time zone (there is no equivalent UTC time). @raise [AmbiguousTime] if `local_time` was ambiguous for the time zone and the `dst` parameter or block did not resolve the ambiguity.
local_to_utc
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def local_time(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, sub_second = 0, dst = Timezone.default_dst, &block) local_timestamp(year, month, day, hour, minute, second, sub_second, dst, &block).to_time end
Creates a `Time` object based on the given (Gregorian calendar) date and time parameters. The parameters are interpreted as a local time in the time zone. The result has the appropriate `utc_offset`, `zone` and {TimeWithOffset#timezone_offset timezone_offset}. _Warning:_ There are time values that are not valid as local times in a time zone (for example, during the transition from standard time to daylight savings time). There are also time values that are ambiguous, occurring more than once with different offsets to UTC (for example, during the transition from daylight savings time to standard time). In the first case (an invalid local time), a {PeriodNotFound} exception will be raised. In the second case (more than one occurrence), an {AmbiguousTime} exception will be raised unless the optional `dst` parameter or block handles the ambiguity. If the ambiguity is due to a transition from daylight savings time to standard time, the `dst` parameter can be used to select whether the daylight savings time or local time is used. For example, the following code would raise an {AmbiguousTime} exception: tz = TZInfo::Timezone.get('America/New_York') tz.local_time(2004,10,31,1,30,0,0) Specifying `dst = true` would return a `Time` with a UTC offset of -4 hours and abbreviation EDT (Eastern Daylight Time). Specifying `dst = false` would return a `Time` with a UTC offset of -5 hours and abbreviation EST (Eastern Standard Time). The `dst` parameter will not be able to resolve an ambiguity resulting from the clocks being set back without changing from daylight savings time to standard time. In this case, if a block is specified, it will be called to resolve the ambiguity. The block must take a single parameter - an `Array` of {TimezonePeriod}s that need to be resolved. The block can select and return a single {TimezonePeriod} or return `nil` or an empty `Array` to cause an {AmbiguousTime} exception to be raised. The default value of the `dst` parameter can be specified using {Timezone.default_dst=}. @param year [Integer] the year. @param month [Integer] the month (1-12). @param day [Integer] the day of the month (1-31). @param hour [Integer] the hour (0-23). @param minute [Integer] the minute (0-59). @param second [Integer] the second (0-59). @param sub_second [Numeric] the fractional part of the second as either a `Rational` that is greater than or equal to 0 and less than 1, or the `Integer` 0. @param dst [Boolean] whether to resolve ambiguous local times by always selecting the period observing daylight savings time (`true`), always selecting the period observing standard time (`false`), or leaving the ambiguity unresolved (`nil`). @yield [periods] if the `dst` parameter did not resolve an ambiguity, an optional block is yielded to. @yieldparam periods [Array<TimezonePeriod>] an `Array` containing all the {TimezonePeriod}s that still match `local_time` after applying the `dst` parameter. @yieldreturn [Object] to resolve the ambiguity: a chosen {TimezonePeriod} or an `Array` containing a chosen {TimezonePeriod}; to leave the ambiguity unresolved: an empty `Array`, an `Array` containing more than one {TimezonePeriod}, or `nil`. @return [TimeWithOffset] a new `Time` object based on the given values, interpreted as a local time in the time zone. @raise [ArgumentError] if either of `year`, `month`, `day`, `hour`, `minute`, or `second` is not an `Integer`. @raise [ArgumentError] if `sub_second` is not a `Rational`, or the `Integer` 0. @raise [ArgumentError] if `utc_offset` is not `nil`, not an `Integer` and not the `Symbol` `:utc`. @raise [RangeError] if `month` is not between 1 and 12. @raise [RangeError] if `day` is not between 1 and 31. @raise [RangeError] if `hour` is not between 0 and 23. @raise [RangeError] if `minute` is not between 0 and 59. @raise [RangeError] if `second` is not between 0 and 59. @raise [RangeError] if `sub_second` is a `Rational` but that is less than 0 or greater than or equal to 1. @raise [PeriodNotFound] if the date and time parameters do not specify a valid local time in the time zone. @raise [AmbiguousTime] if the date and time parameters are ambiguous for the time zone and the `dst` parameter or block did not resolve the ambiguity.
local_time
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def local_datetime(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, sub_second = 0, dst = Timezone.default_dst, &block) local_timestamp(year, month, day, hour, minute, second, sub_second, dst, &block).to_datetime end
Creates a `DateTime` object based on the given (Gregorian calendar) date and time parameters. The parameters are interpreted as a local time in the time zone. The result has the appropriate `offset` and {DateTimeWithOffset#timezone_offset timezone_offset}. _Warning:_ There are time values that are not valid as local times in a time zone (for example, during the transition from standard time to daylight savings time). There are also time values that are ambiguous, occurring more than once with different offsets to UTC (for example, during the transition from daylight savings time to standard time). In the first case (an invalid local time), a {PeriodNotFound} exception will be raised. In the second case (more than one occurrence), an {AmbiguousTime} exception will be raised unless the optional `dst` parameter or block handles the ambiguity. If the ambiguity is due to a transition from daylight savings time to standard time, the `dst` parameter can be used to select whether the daylight savings time or local time is used. For example, the following code would raise an {AmbiguousTime} exception: tz = TZInfo::Timezone.get('America/New_York') tz.local_datetime(2004,10,31,1,30,0,0) Specifying `dst = true` would return a `Time` with a UTC offset of -4 hours and abbreviation EDT (Eastern Daylight Time). Specifying `dst = false` would return a `Time` with a UTC offset of -5 hours and abbreviation EST (Eastern Standard Time). The `dst` parameter will not be able to resolve an ambiguity resulting from the clocks being set back without changing from daylight savings time to standard time. In this case, if a block is specified, it will be called to resolve the ambiguity. The block must take a single parameter - an `Array` of {TimezonePeriod}s that need to be resolved. The block can select and return a single {TimezonePeriod} or return `nil` or an empty `Array` to cause an {AmbiguousTime} exception to be raised. The default value of the `dst` parameter can be specified using {Timezone.default_dst=}. @param year [Integer] the year. @param month [Integer] the month (1-12). @param day [Integer] the day of the month (1-31). @param hour [Integer] the hour (0-23). @param minute [Integer] the minute (0-59). @param second [Integer] the second (0-59). @param sub_second [Numeric] the fractional part of the second as either a `Rational` that is greater than or equal to 0 and less than 1, or the `Integer` 0. @param dst [Boolean] whether to resolve ambiguous local times by always selecting the period observing daylight savings time (`true`), always selecting the period observing standard time (`false`), or leaving the ambiguity unresolved (`nil`). @yield [periods] if the `dst` parameter did not resolve an ambiguity, an optional block is yielded to. @yieldparam periods [Array<TimezonePeriod>] an `Array` containing all the {TimezonePeriod}s that still match `local_time` after applying the `dst` parameter. @yieldreturn [Object] to resolve the ambiguity: a chosen {TimezonePeriod} or an `Array` containing a chosen {TimezonePeriod}; to leave the ambiguity unresolved: an empty `Array`, an `Array` containing more than one {TimezonePeriod}, or `nil`. @return [DateTimeWithOffset] a new `DateTime` object based on the given values, interpreted as a local time in the time zone. @raise [ArgumentError] if either of `year`, `month`, `day`, `hour`, `minute`, or `second` is not an `Integer`. @raise [ArgumentError] if `sub_second` is not a `Rational`, or the `Integer` 0. @raise [ArgumentError] if `utc_offset` is not `nil`, not an `Integer` and not the `Symbol` `:utc`. @raise [RangeError] if `month` is not between 1 and 12. @raise [RangeError] if `day` is not between 1 and 31. @raise [RangeError] if `hour` is not between 0 and 23. @raise [RangeError] if `minute` is not between 0 and 59. @raise [RangeError] if `second` is not between 0 and 59. @raise [RangeError] if `sub_second` is a `Rational` but that is less than 0 or greater than or equal to 1. @raise [PeriodNotFound] if the date and time parameters do not specify a valid local time in the time zone. @raise [AmbiguousTime] if the date and time parameters are ambiguous for the time zone and the `dst` parameter or block did not resolve the ambiguity.
local_datetime
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def local_timestamp(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, sub_second = 0, dst = Timezone.default_dst, &block) ts = Timestamp.create(year, month, day, hour, minute, second, sub_second) timezone_offset = period_for_local(ts, dst, &block).offset utc_offset = timezone_offset.observed_utc_offset TimestampWithOffset.new(ts.value - utc_offset, sub_second, utc_offset).set_timezone_offset(timezone_offset) end
Creates a {Timestamp} object based on the given (Gregorian calendar) date and time parameters. The parameters are interpreted as a local time in the time zone. The result has the appropriate {Timestamp#utc_offset utc_offset} and {TimestampWithOffset#timezone_offset timezone_offset}. _Warning:_ There are time values that are not valid as local times in a time zone (for example, during the transition from standard time to daylight savings time). There are also time values that are ambiguous, occurring more than once with different offsets to UTC (for example, during the transition from daylight savings time to standard time). In the first case (an invalid local time), a {PeriodNotFound} exception will be raised. In the second case (more than one occurrence), an {AmbiguousTime} exception will be raised unless the optional `dst` parameter or block handles the ambiguity. If the ambiguity is due to a transition from daylight savings time to standard time, the `dst` parameter can be used to select whether the daylight savings time or local time is used. For example, the following code would raise an {AmbiguousTime} exception: tz = TZInfo::Timezone.get('America/New_York') tz.local_timestamp(2004,10,31,1,30,0,0) Specifying `dst = true` would return a `Time` with a UTC offset of -4 hours and abbreviation EDT (Eastern Daylight Time). Specifying `dst = false` would return a `Time` with a UTC offset of -5 hours and abbreviation EST (Eastern Standard Time). The `dst` parameter will not be able to resolve an ambiguity resulting from the clocks being set back without changing from daylight savings time to standard time. In this case, if a block is specified, it will be called to resolve the ambiguity. The block must take a single parameter - an `Array` of {TimezonePeriod}s that need to be resolved. The block can select and return a single {TimezonePeriod} or return `nil` or an empty `Array` to cause an {AmbiguousTime} exception to be raised. The default value of the `dst` parameter can be specified using {Timezone.default_dst=}. @param year [Integer] the year. @param month [Integer] the month (1-12). @param day [Integer] the day of the month (1-31). @param hour [Integer] the hour (0-23). @param minute [Integer] the minute (0-59). @param second [Integer] the second (0-59). @param sub_second [Numeric] the fractional part of the second as either a `Rational` that is greater than or equal to 0 and less than 1, or the `Integer` 0. @param dst [Boolean] whether to resolve ambiguous local times by always selecting the period observing daylight savings time (`true`), always selecting the period observing standard time (`false`), or leaving the ambiguity unresolved (`nil`). @yield [periods] if the `dst` parameter did not resolve an ambiguity, an optional block is yielded to. @yieldparam periods [Array<TimezonePeriod>] an `Array` containing all the {TimezonePeriod}s that still match `local_time` after applying the `dst` parameter. @yieldreturn [Object] to resolve the ambiguity: a chosen {TimezonePeriod} or an `Array` containing a chosen {TimezonePeriod}; to leave the ambiguity unresolved: an empty `Array`, an `Array` containing more than one {TimezonePeriod}, or `nil`. @return [TimestampWithOffset] a new {Timestamp} object based on the given values, interpreted as a local time in the time zone. @raise [ArgumentError] if either of `year`, `month`, `day`, `hour`, `minute`, or `second` is not an `Integer`. @raise [ArgumentError] if `sub_second` is not a `Rational`, or the `Integer` 0. @raise [ArgumentError] if `utc_offset` is not `nil`, not an `Integer` and not the `Symbol` `:utc`. @raise [RangeError] if `month` is not between 1 and 12. @raise [RangeError] if `day` is not between 1 and 31. @raise [RangeError] if `hour` is not between 0 and 23. @raise [RangeError] if `minute` is not between 0 and 59. @raise [RangeError] if `second` is not between 0 and 59. @raise [RangeError] if `sub_second` is a `Rational` but that is less than 0 or greater than or equal to 1. @raise [PeriodNotFound] if the date and time parameters do not specify a valid local time in the time zone. @raise [AmbiguousTime] if the date and time parameters are ambiguous for the time zone and the `dst` parameter or block did not resolve the ambiguity.
local_timestamp
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def offsets_up_to(to, from = nil) raise ArgumentError, 'to must be specified' unless to to_timestamp = Timestamp.for(to) from_timestamp = from && Timestamp.for(from) transitions = transitions_up_to(to_timestamp, from_timestamp) if transitions.empty? # No transitions in the range, find the period that covers it. if from_timestamp # Use the from date as it is inclusive. period = period_for(from_timestamp) else # to is exclusive, so this can't be used with period_for. However, any # time earlier than to can be used. Subtract 1 hour. period = period_for(to_timestamp.add_and_set_utc_offset(-3600, :utc)) end [period.offset] else result = Set.new first = transitions.first result << first.previous_offset unless from_timestamp && first.at == from_timestamp transitions.each do |t| result << t.offset end result.to_a end end
Returns the unique offsets used by the time zone up to a given time (`to`) as an `Array` of {TimezoneOffset} instances. A from time may also be supplied using the `from` parameter. If from is not `nil`, only offsets used from that time onwards will be returned. Comparisons with `to` are exclusive. Comparisons with `from` are inclusive. @param to [Object] a `Time`, `DateTime` or {Timestamp} specifying the latest (exclusive) offset to return. @param from [Object] an optional `Time`, `DateTime` or {Timestamp} specifying the earliest (inclusive) offset to return. @return [Array<TimezoneOffsets>] the offsets that are used earlier than `to` and, if specified, at or later than `from`. Offsets may be returned in any order. @raise [ArgumentError] if `from` is specified and `to` is not greater than `from`. @raise [ArgumentError] is raised if `to` is `nil`. @raise [ArgumentError] if either `to` or `from` is a {Timestamp} with an unspecified offset.
offsets_up_to
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def current_time_and_period period = nil local_time = Timestamp.for(Time.now) do |ts| period = period_for(ts) TimestampWithOffset.set_timezone_offset(ts, period.offset) end [local_time, period] end
Returns the current local time and {TimezonePeriod} for the time zone as an `Array`. The first element is the time as a {TimeWithOffset}. The second element is the period. @return [Array] an `Array` containing the current {TimeWithOffset} for the time zone as the first element and the current {TimezonePeriod} for the time zone as the second element.
current_time_and_period
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def eql?(tz) self == tz end
@param tz [Object] an `Object` to compare this {Timezone} with. @return [Boolean] `true` if `tz` is an instance of {Timezone} and has the same {identifier} as `self`, otherwise `false`.
eql?
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def raise_unknown_timezone raise UnknownTimezone, 'TZInfo::Timezone should not be constructed directly (use TZInfo::Timezone.get instead)' end
Raises an {UnknownTimezone} exception. @raise [UnknownTimezone] always.
raise_unknown_timezone
ruby
tzinfo/tzinfo
lib/tzinfo/timezone.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
MIT
def initialize(base_utc_offset, std_offset, abbreviation) @base_utc_offset = base_utc_offset @std_offset = std_offset @abbreviation = abbreviation.freeze @observed_utc_offset = @base_utc_offset + @std_offset end
Initializes a new {TimezoneOffset}. {TimezoneOffset} instances should not normally be constructed manually. The passed in `abbreviation` instance will be frozen. @param base_utc_offset [Integer] the base offset from UTC in seconds. @param std_offset [Integer] the offset from standard time in seconds. @param abbreviation [String] the abbreviation identifying the offset.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_offset.rb
MIT
def dst? @std_offset != 0 end
Determines if daylight savings is in effect (i.e. if {std_offset} is non-zero). @return [Boolean] `true` if {std_offset} is non-zero, otherwise `false`.
dst?
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_offset.rb
MIT
def eql?(toi) self == toi end
Determines if this {TimezoneOffset} is equal to another instance. @param toi [Object] the instance to test for equality. @return [Boolean] `true` if `toi` is a {TimezoneOffset} with the same {utc_offset}, {std_offset} and {abbreviation} as this {TimezoneOffset}, otherwise `false`.
eql?
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_offset.rb
MIT
def hash [@base_utc_offset, @std_offset, @abbreviation].hash end
@return [Integer] a hash based on {utc_offset}, {std_offset} and {abbreviation}.
hash
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_offset.rb
MIT
def inspect "#<#{self.class}: @base_utc_offset=#{@base_utc_offset}, @std_offset=#{@std_offset}, @abbreviation=#{@abbreviation}>" end
@return [String] the internal object state as a programmer-readable `String`.
inspect
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_offset.rb
MIT
def initialize(offset) raise ArgumentError, 'offset must be specified' unless offset @offset = offset end
Initializes a {TimezonePeriod}. @param offset [TimezoneOffset] the offset that is observed for the period of time. @raise [ArgumentError] if `offset` is `nil`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_period.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_period.rb
MIT
def raise_not_implemented(method_name) raise NotImplementedError, "Subclasses must override #{method_name}" end
Raises a {NotImplementedError} to indicate that subclasses should override a method. @raise [NotImplementedError] always.
raise_not_implemented
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_period.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_period.rb
MIT
def timestamp(transition) transition ? transition.at : nil end
@param transition [TimezoneTransition] a transition or `nil`. @return [Timestamp] the {Timestamp} representing when a transition occurs, or `nil` if `transition` is `nil`.
timestamp
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_period.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_period.rb
MIT
def timestamp_with_offset(transition) transition ? TimestampWithOffset.set_timezone_offset(transition.at, offset) : nil end
@param transition [TimezoneTransition] a transition or `nil`. @return [TimestampWithOffset] a {Timestamp} representing when a transition occurs with offset set to {#offset}, or `nil` if `transition` is `nil`.
timestamp_with_offset
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_period.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_period.rb
MIT
def initialize(identifier) super() @identifier = identifier @real_timezone = nil end
Initializes a new {TimezoneProxy}. The `identifier` parameter is not checked when initializing the proxy. It will be validated when the real {Timezone} instance is loaded. @param identifier [String] an IANA Time Zone Database time zone identifier.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_proxy.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_proxy.rb
MIT
def real_timezone # Thread-safety: It is possible that the value of @real_timezone may be # calculated multiple times in concurrently executing threads. It is not # worth the overhead of locking to ensure that @real_timezone is only # calculated once. unless @real_timezone result = Timezone.get(@identifier) return result if frozen? @real_timezone = result end @real_timezone end
Returns the real {Timezone} instance being proxied. The real {Timezone} is loaded using {Timezone.get} on the first access. @return [Timezone] the real {Timezone} instance being proxied.
real_timezone
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_proxy.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_proxy.rb
MIT
def initialize(offset, previous_offset, timestamp_value) @offset = offset @previous_offset = previous_offset @timestamp_value = timestamp_value end
Initializes a new {TimezoneTransition}. {TimezoneTransition} instances should not normally be constructed manually. @param offset [TimezoneOffset] the offset the transition changes to. @param previous_offset [TimezoneOffset] the offset the transition changes from. @param timestamp_value [Integer] when the transition occurs as a number of seconds since 1970-01-01 00:00:00 UTC ignoring leap seconds (i.e. each day is treated as if it were 86,400 seconds long).
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_transition.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_transition.rb
MIT
def local_end_at TimestampWithOffset.new(@timestamp_value, 0, @previous_offset.observed_utc_offset).set_timezone_offset(@previous_offset) end
Returns a {TimestampWithOffset} instance representing the local time when this transition causes the previous observance to end (calculated from {at} using {previous_offset}). To obtain the result as a `Time` or `DateTime`, call either {TimestampWithOffset#to_time to_time} or {TimestampWithOffset#to_datetime to_datetime} on the {TimestampWithOffset} instance that is returned. @return [TimestampWithOffset] the local time when this transition causes the previous observance to end.
local_end_at
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_transition.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_transition.rb
MIT
def local_start_at TimestampWithOffset.new(@timestamp_value, 0, @offset.observed_utc_offset).set_timezone_offset(@offset) end
Returns a {TimestampWithOffset} instance representing the local time when this transition causes the next observance to start (calculated from {at} using {offset}). To obtain the result as a `Time` or `DateTime`, call either {TimestampWithOffset#to_time to_time} or {TimestampWithOffset#to_datetime to_datetime} on the {TimestampWithOffset} instance that is returned. @return [TimestampWithOffset] the local time when this transition causes the next observance to start.
local_start_at
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_transition.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_transition.rb
MIT
def hash [@offset, @previous_offset, @timestamp_value].hash end
@return [Integer] a hash based on {offset}, {previous_offset} and {timestamp_value}.
hash
ruby
tzinfo/tzinfo
lib/tzinfo/timezone_transition.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone_transition.rb
MIT
def set_timezone_offset(timezone_offset) raise ArgumentError, 'timezone_offset must be specified' unless timezone_offset localtime(timezone_offset.observed_utc_offset) @timezone_offset = timezone_offset self end
Marks this {TimeWithOffset} as a local time with the UTC offset of a given {TimezoneOffset} and sets the associated {TimezoneOffset}. @param timezone_offset [TimezoneOffset] the {TimezoneOffset} to use to set the offset of this {TimeWithOffset}. @return [TimeWithOffset] `self`. @raise [ArgumentError] if `timezone_offset` is `nil`.
set_timezone_offset
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def dst? to = timezone_offset to ? to.dst? : super end
An overridden version of `Time#dst?` that, if there is an associated {TimezoneOffset}, returns the result of calling {TimezoneOffset#dst? dst?} on that offset. @return [Boolean] `true` if daylight savings time is being observed, otherwise `false`.
dst?
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def getlocal(*args) # JRuby < 9.3 returns a Time in all cases. # JRuby >= 9.3 returns a Time when called with no arguments and a # TimeWithOffset with a timezone_offset assigned when called with an # offset argument. result = super result.clear_timezone_offset if result.kind_of?(TimeWithOffset) result end
An overridden version of `Time#getlocal` that clears the associated {TimezoneOffset} if the base implementation of `getlocal` returns a {TimeWithOffset}. @return [Time] a representation of the {TimeWithOffset} using either the local time zone or the given offset.
getlocal
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def gmtime super @timezone_offset = nil self end
An overridden version of `Time#gmtime` that clears the associated {TimezoneOffset}. @return [TimeWithOffset] `self`.
gmtime
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def localtime(*args) super @timezone_offset = nil self end
An overridden version of `Time#localtime` that clears the associated {TimezoneOffset}. @return [TimeWithOffset] `self`.
localtime
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def round(ndigits = 0) if_timezone_offset(super) {|o,t| self.class.at(t.to_i, t.subsec * 1_000_000).set_timezone_offset(o) } end
An overridden version of `Time#round` that, if there is an associated {TimezoneOffset}, returns a {TimeWithOffset} preserving that offset. @return [Time] the rounded time.
round
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def to_a if_timezone_offset(super) do |o,a| a[8] = o.dst? a[9] = o.abbreviation a end end
An overridden version of `Time#to_a`. The `isdst` (index 8) and `zone` (index 9) elements of the array are set according to the associated {TimezoneOffset}. @return [Array] an `Array` representation of the {TimeWithOffset}.
to_a
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def utc super @timezone_offset = nil self end
An overridden version of `Time#utc` that clears the associated {TimezoneOffset}. @return [TimeWithOffset] `self`.
utc
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def zone to = timezone_offset to ? to.abbreviation : super end
An overridden version of `Time#zone` that, if there is an associated {TimezoneOffset}, returns the {TimezoneOffset#abbreviation abbreviation} of that offset. @return [String] the {TimezoneOffset#abbreviation abbreviation} of the associated {TimezoneOffset}, or the result from `Time#zone` if there is no such offset.
zone
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def to_datetime if_timezone_offset(super) do |o,dt| offset = dt.offset result = DateTimeWithOffset.jd(dt.jd + dt.day_fraction - offset) result = result.new_offset(offset) unless offset == 0 result.set_timezone_offset(o) end end
An overridden version of `Time#to_datetime` that, if there is an associated {TimezoneOffset}, returns a {DateTimeWithOffset} with that offset. @return [DateTime] if there is an associated {TimezoneOffset}, a {DateTimeWithOffset} representation of this {TimeWithOffset}, otherwise a `Time` representation.
to_datetime
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def clear_timezone_offset @timezone_offset = nil self end
Clears the associated {TimezoneOffset}. @return [TimeWithOffset] `self`.
clear_timezone_offset
ruby
tzinfo/tzinfo
lib/tzinfo/time_with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/time_with_offset.rb
MIT
def initialize(start_transition, end_transition) if start_transition super(start_transition.offset) elsif end_transition super(end_transition.previous_offset) else raise ArgumentError, 'At least one of start_transition and end_transition must be specified' end @start_transition = start_transition @end_transition = end_transition end
Initializes a {TransitionsTimezonePeriod}. At least one of `start_transition` and `end_transition` must be specified. @param start_transition [TimezoneTransition] the transition that defines the start of the period, or `nil` if the start is unbounded. @param end_transition [TimezoneTransition] the transition that defines the end of the period, or `nil` if the end is unbounded. @raise [ArgumentError] if both `start_transition` and `end_transition` are `nil`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/transitions_timezone_period.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transitions_timezone_period.rb
MIT
def hash [@start_transition, @end_transition].hash end
@return [Integer] a hash based on {start_transition} and {end_transition}.
hash
ruby
tzinfo/tzinfo
lib/tzinfo/transitions_timezone_period.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transitions_timezone_period.rb
MIT
def inspect "#<#{self.class}: @start_transition=#{@start_transition.inspect}, @end_transition=#{@end_transition.inspect}>" end
@return [String] the internal object state as a programmer-readable `String`.
inspect
ruby
tzinfo/tzinfo
lib/tzinfo/transitions_timezone_period.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transitions_timezone_period.rb
MIT
def initialize(transition_at) raise ArgumentError, 'Invalid transition_at' unless transition_at.kind_of?(Integer) @transition_at = transition_at end
Initializes a new {TransitionRule}. @param transition_at [Integer] the time in seconds after midnight local time at which the transition occurs. @raise [ArgumentError] if `transition_at` is not an `Integer`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def at(offset, year) day = get_day(offset, year) TimestampWithOffset.set_timezone_offset(Timestamp.for(day + @transition_at), offset) end
Calculates the time of the transition from a given offset on a given year. @param offset [TimezoneOffset] the current offset at the time the rule will transition. @param year [Integer] the year in which the transition occurs (local time). @return [TimestampWithOffset] the time at which the transition occurs.
at
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def initialize(day, transition_at) super(transition_at) raise ArgumentError, 'Invalid day' unless day.kind_of?(Integer) @seconds = day * 86400 end
Initializes a new {DayOfYearTransitionRule}. @param day [Integer] the day of the year on which the transition occurs. The precise meaning is defined by subclasses. @param transition_at [Integer] the time in seconds after midnight local time at which the transition occurs. @raise [ArgumentError] if `transition_at` is not an `Integer`. @raise [ArgumentError] if `day` is not an `Integer`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def initialize(day, transition_at = 0) super(day, transition_at) raise ArgumentError, 'Invalid day' unless day >= 0 && day <= 365 end
Initializes a new {AbsoluteDayOfYearTransitionRule}. @param day [Integer] the zero-based day of the year on which the transition occurs (0 to 365 inclusive). @param transition_at [Integer] the time in seconds after midnight local time at which the transition occurs. @raise [ArgumentError] if `transition_at` is not an `Integer`. @raise [ArgumentError] if `day` is not an `Integer`. @raise [ArgumentError] if `day` is less than 0 or greater than 365.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def is_always_first_day_of_year? seconds == 0 end
@return [Boolean] `true` if the day specified by this transition is the first in the year (a day number of 0), otherwise `false`.
is_always_first_day_of_year?
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def get_day(offset, year) Time.new(year, 1, 1, 0, 0, 0, offset.observed_utc_offset) + seconds end
Returns a `Time` representing midnight local time on the day specified by the rule for the given offset and year. @param offset [TimezoneOffset] the current offset at the time of the transition. @param year [Integer] the year in which the transition occurs. @return [Time] midnight local time on the day specified by the rule for the given offset and year.
get_day
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def initialize(day, transition_at = 0) super(day, transition_at) raise ArgumentError, 'Invalid day' unless day >= 1 && day <= 365 end
Initializes a new {JulianDayOfYearTransitionRule}. @param day [Integer] the one-based Julian day of the year on which the transition occurs (1 to 365 inclusive). @param transition_at [Integer] the time in seconds after midnight local time at which the transition occurs. @raise [ArgumentError] if `transition_at` is not an `Integer`. @raise [ArgumentError] if `day` is not an `Integer`. @raise [ArgumentError] if `day` is less than 1 or greater than 365.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def is_always_first_day_of_year? seconds == 86400 end
@return [Boolean] `true` if the day specified by this transition is the first in the year (a day number of 1), otherwise `false`.
is_always_first_day_of_year?
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def is_always_last_day_of_year? seconds == YEAR end
@return [Boolean] `true` if the day specified by this transition is the last in the year (a day number of 365), otherwise `false`.
is_always_last_day_of_year?
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def get_day(offset, year) # Returns 1 March on non-leap years. leap = Time.new(year, 2, 29, 0, 0, 0, offset.observed_utc_offset) diff = seconds - LEAP diff += 86400 if diff >= 0 && leap.mday == 29 leap + diff end
Returns a `Time` representing midnight local time on the day specified by the rule for the given offset and year. @param offset [TimezoneOffset] the current offset at the time of the transition. @param year [Integer] the year in which the transition occurs. @return [Time] midnight local time on the day specified by the rule for the given offset and year.
get_day
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def initialize(month, day_of_week, transition_at) super(transition_at) raise ArgumentError, 'Invalid month' unless month.kind_of?(Integer) && month >= 1 && month <= 12 raise ArgumentError, 'Invalid day_of_week' unless day_of_week.kind_of?(Integer) && day_of_week >= 0 && day_of_week <= 6 @month = month @day_of_week = day_of_week end
Initializes a new {DayOfWeekTransitionRule}. @param month [Integer] the month of the year when the transition occurs. @param day_of_week [Integer] the day of the week when the transition occurs. 0 is Sunday, 6 is Saturday. @param transition_at [Integer] the time in seconds after midnight local time at which the transition occurs. @raise [ArgumentError] if `transition_at` is not an `Integer`. @raise [ArgumentError] if `month` is not an `Integer`. @raise [ArgumentError] if `month` is less than 1 or greater than 12. @raise [ArgumentError] if `day_of_week` is not an `Integer`. @raise [ArgumentError] if `day_of_week` is less than 0 or greater than 6.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def initialize(month, week, day_of_week, transition_at = 0) super(month, day_of_week, transition_at) raise ArgumentError, 'Invalid week' unless week.kind_of?(Integer) && week >= 1 && week <= 4 @offset_start = (week - 1) * 7 + 1 end
Initializes a new {DayOfMonthTransitionRule}. @param month [Integer] the month of the year when the transition occurs. @param week [Integer] the week of the month when the transition occurs (1 to 4). @param day_of_week [Integer] the day of the week when the transition occurs. 0 is Sunday, 6 is Saturday. @param transition_at [Integer] the time in seconds after midnight local time at which the transition occurs. @raise [ArgumentError] if `transition_at` is not an `Integer`. @raise [ArgumentError] if `month` is not an `Integer`. @raise [ArgumentError] if `month` is less than 1 or greater than 12. @raise [ArgumentError] if `week` is not an `Integer`. @raise [ArgumentError] if `week` is less than 1 or greater than 4. @raise [ArgumentError] if `day_of_week` is not an `Integer`. @raise [ArgumentError] if `day_of_week` is less than 0 or greater than 6.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def get_day(offset, year) candidate = Time.new(year, month, @offset_start, 0, 0, 0, offset.observed_utc_offset) diff = day_of_week - candidate.wday if diff < 0 candidate + (7 + diff) * 86400 elsif diff > 0 candidate + diff * 86400 else candidate end end
Returns a `Time` representing midnight local time on the day specified by the rule for the given offset and year. @param offset [TimezoneOffset] the current offset at the time of the transition. @param year [Integer] the year in which the transition occurs. @return [Time] midnight local time on the day specified by the rule for the given offset and year.
get_day
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def initialize(month, day_of_week, transition_at = 0) super(month, day_of_week, transition_at) end
Initializes a new {LastDayOfMonthTransitionRule}. @param month [Integer] the month of the year when the transition occurs. @param day_of_week [Integer] the day of the week when the transition occurs. 0 is Sunday, 6 is Saturday. @param transition_at [Integer] the time in seconds after midnight local time at which the transition occurs. @raise [ArgumentError] if `transition_at` is not an `Integer`. @raise [ArgumentError] if `month` is not an `Integer`. @raise [ArgumentError] if `month` is less than 1 or greater than 12. @raise [ArgumentError] if `day_of_week` is not an `Integer`. @raise [ArgumentError] if `day_of_week` is less than 0 or greater than 6.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def get_day(offset, year) next_month = month + 1 if next_month == 13 year += 1 next_month = 1 end candidate = Time.new(year, next_month, 1, 0, 0, 0, offset.observed_utc_offset) - 86400 diff = candidate.wday - day_of_week if diff < 0 candidate - (diff + 7) * 86400 elsif diff > 0 candidate - diff * 86400 else candidate end end
Returns a `Time` representing midnight local time on the day specified by the rule for the given offset and year. @param offset [TimezoneOffset] the current offset at the time of the transition. @param year [Integer] the year in which the transition occurs. @return [Time] midnight local time on the day specified by the rule for the given offset and year.
get_day
ruby
tzinfo/tzinfo
lib/tzinfo/transition_rule.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/transition_rule.rb
MIT
def strftime(format) raise ArgumentError, 'format must be specified' unless format if_timezone_offset do |o| abbreviation = nil format = format.gsub(/%(%*)Z/) do if $1.length.odd? # Return %%Z so the real strftime treats it as a literal %Z too. "#$1%Z" else "#$1#{abbreviation ||= o.abbreviation.gsub(/%/, '%%')}" end end end super end
Overrides the `Time`, `DateTime` or {Timestamp} version of `strftime`, replacing `%Z` with the {TimezoneOffset#abbreviation abbreviation} of the associated {TimezoneOffset}. If there is no associated offset, `%Z` is expanded by the base class instead. All the format directives handled by the base class are supported. @param format [String] the format string. @return [String] the formatted time. @raise [ArgumentError] if `format` is `nil`.
strftime
ruby
tzinfo/tzinfo
lib/tzinfo/with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/with_offset.rb
MIT
def if_timezone_offset(result = nil) #:nodoc: to = timezone_offset to ? yield(to, result) : result end
Performs a calculation if there is an associated {TimezoneOffset}. @param result [Object] a result value that can be manipulated by the block if there is an associated {TimezoneOffset}. @yield [period, result] if there is an associated {TimezoneOffset}, the block is yielded to in order to calculate the method result. @yieldparam period [TimezoneOffset] the associated {TimezoneOffset}. @yieldparam result [Object] the `result` parameter. @yieldreturn [Object] the result of the calculation performed if there is an associated {TimezoneOffset}. @return [Object] the result of the block if there is an associated {TimezoneOffset}, otherwise the `result` parameter. @private
if_timezone_offset
ruby
tzinfo/tzinfo
lib/tzinfo/with_offset.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/with_offset.rb
MIT
def initialize(identifier, constant_offset) super(identifier) raise ArgumentError, 'constant_offset must be specified' unless constant_offset @constant_offset = constant_offset end
Initializes a new {ConstantOffsetDataTimezoneInfo}. The passed in `identifier` instance will be frozen. A reference to the passed in {TimezoneOffset} will be retained. @param identifier [String] the identifier of the time zone. @param constant_offset [TimezoneOffset] the constantly observed offset. @raise [ArgumentError] if `identifier` or `constant_offset` is `nil`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/constant_offset_data_timezone_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/constant_offset_data_timezone_info.rb
MIT
def initialize(code, name, zones) raise ArgumentError, 'code must be specified' unless code raise ArgumentError, 'name must be specified' unless name raise ArgumentError, 'zones must be specified' unless zones @code = code.freeze @name = name.freeze @zones = zones.freeze end
Initializes a new {CountryInfo}. The passed in `code`, `name` and `zones` instances will be frozen. @param code [String] an ISO 3166-1 alpha-2 country code. @param name [String] the name of the country. @param zones [Array<CountryTimezone>] the time zones observed in the country. @raise [ArgumentError] if `code`, `name` or `zones` is `nil`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/country_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/country_info.rb
MIT
def inspect "#<#{self.class}: #@code>" end
@return [String] the internal object state as a programmer-readable `String`.
inspect
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/country_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/country_info.rb
MIT
def raise_not_implemented(method_name) raise NotImplementedError, "Subclasses must override #{method_name}" end
Raises a {NotImplementedError} to indicate that the base class is incorrectly being used directly. raise [NotImplementedError] always.
raise_not_implemented
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/data_timezone_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/data_timezone_info.rb
MIT
def initialize(identifier, link_to_identifier) super(identifier) raise ArgumentError, 'link_to_identifier must be specified' unless link_to_identifier @link_to_identifier = link_to_identifier.freeze end
Initializes a new {LinkedTimezoneInfo}. The passed in `identifier` and `link_to_identifier` instances will be frozen. @param identifier [String] the identifier of the time zone. @param link_to_identifier [String] the identifier of the time zone that this zone link to. @raise [ArgumentError] if `identifier` or `link_to_identifier` are `nil`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/linked_timezone_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/linked_timezone_info.rb
MIT
def initialize(string_deduper) @string_deduper = string_deduper end
Initializes a new {PosixTimeZoneParser}. @param string_deduper [StringDeduper] a {StringDeduper} instance to use to dedupe abbreviations.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/posix_time_zone_parser.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/posix_time_zone_parser.rb
MIT
def parse(tz_string) raise InvalidPosixTimeZone unless tz_string.kind_of?(String) return nil if tz_string.empty? s = StringScanner.new(tz_string) check_scan(s, /([^-+,\d<][^-+,\d]*) | <([^>]+)>/x) std_abbrev = @string_deduper.dedupe(RubyCoreSupport.untaint(s[1] || s[2])) check_scan(s, /([-+]?\d+)(?::(\d+)(?::(\d+))?)?/) std_offset = get_offset_from_hms(s[1], s[2], s[3]) if s.scan(/([^-+,\d<][^-+,\d]*) | <([^>]+)>/x) dst_abbrev = @string_deduper.dedupe(RubyCoreSupport.untaint(s[1] || s[2])) if s.scan(/([-+]?\d+)(?::(\d+)(?::(\d+))?)?/) dst_offset = get_offset_from_hms(s[1], s[2], s[3]) else # POSIX is negative for ahead of UTC. dst_offset = std_offset - 3600 end dst_difference = std_offset - dst_offset start_rule = parse_rule(s, 'start') end_rule = parse_rule(s, 'end') raise InvalidPosixTimeZone, "Expected the end of a POSIX-style time zone string but found '#{s.rest}'." if s.rest? if start_rule.is_always_first_day_of_year? && start_rule.transition_at == 0 && end_rule.is_always_last_day_of_year? && end_rule.transition_at == 86400 + dst_difference # Constant daylight savings time. # POSIX is negative for ahead of UTC. TimezoneOffset.new(-std_offset, dst_difference, dst_abbrev) else AnnualRules.new( TimezoneOffset.new(-std_offset, 0, std_abbrev), TimezoneOffset.new(-std_offset, dst_difference, dst_abbrev), start_rule, end_rule) end elsif !s.rest? # Constant standard time. # POSIX is negative for ahead of UTC. TimezoneOffset.new(-std_offset, 0, std_abbrev) else raise InvalidPosixTimeZone, "Expected the end of a POSIX-style time zone string but found '#{s.rest}'." end end
Parses a POSIX-style TZ string. @param tz_string [String] the string to parse. @return [Object] either a {TimezoneOffset} for a constantly applied offset or an {AnnualRules} instance representing the rules. @raise [InvalidPosixTimeZone] if `tz_string` is not a `String`. @raise [InvalidPosixTimeZone] if `tz_string` is is not valid.
parse
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/posix_time_zone_parser.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/posix_time_zone_parser.rb
MIT
def parse_rule(s, type) check_scan(s, /,(?: (?: J(\d+) ) | (\d+) | (?: M(\d+)\.(\d)\.(\d) ) )/x) julian_day_of_year = s[1] absolute_day_of_year = s[2] month = s[3] week = s[4] day_of_week = s[5] if s.scan(/\//) check_scan(s, /([-+]?\d+)(?::(\d+)(?::(\d+))?)?/) transition_at = get_seconds_after_midnight_from_hms(s[1], s[2], s[3]) else transition_at = 7200 end begin if julian_day_of_year JulianDayOfYearTransitionRule.new(julian_day_of_year.to_i, transition_at) elsif absolute_day_of_year AbsoluteDayOfYearTransitionRule.new(absolute_day_of_year.to_i, transition_at) elsif week == '5' LastDayOfMonthTransitionRule.new(month.to_i, day_of_week.to_i, transition_at) else DayOfMonthTransitionRule.new(month.to_i, week.to_i, day_of_week.to_i, transition_at) end rescue ArgumentError => e raise InvalidPosixTimeZone, "Invalid #{type} rule in POSIX-style time zone string: #{e}" end end
Parses a rule. @param s [StringScanner] the `StringScanner` to read the rule from. @param type [String] the type of rule (either `'start'` or `'end'`). @raise [InvalidPosixTimeZone] if the rule is not valid. @return [TransitionRule] the parsed rule.
parse_rule
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/posix_time_zone_parser.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/posix_time_zone_parser.rb
MIT
def get_offset_from_hms(h, m, s) h = h.to_i m = m.to_i s = s.to_i raise InvalidPosixTimeZone, "Invalid minute #{m} in offset for POSIX-style time zone string." if m > 59 raise InvalidPosixTimeZone, "Invalid second #{s} in offset for POSIX-style time zone string." if s > 59 magnitude = (h.abs * 60 + m) * 60 + s h < 0 ? -magnitude : magnitude end
Returns an offset in seconds from hh:mm:ss values. The value can be negative. -02:33:12 would represent 2 hours, 33 minutes and 12 seconds ahead of UTC. @param h [String] the hours. @param m [String] the minutes. @param s [String] the seconds. @return [Integer] the offset. @raise [InvalidPosixTimeZone] if the mm and ss values are greater than 59.
get_offset_from_hms
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/posix_time_zone_parser.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/posix_time_zone_parser.rb
MIT
def get_seconds_after_midnight_from_hms(h, m, s) h = h.to_i m = m.to_i s = s.to_i raise InvalidPosixTimeZone, "Invalid minute #{m} in time for POSIX-style time zone string." if m > 59 raise InvalidPosixTimeZone, "Invalid second #{s} in time for POSIX-style time zone string." if s > 59 (h * 3600) + m * 60 + s end
Returns the seconds from midnight from hh:mm:ss values. Hours can exceed 24 for a time on the following day. Hours can be negative to subtract hours from midnight on the given day. -02:33:12 represents 22:33:12 on the prior day. @param h [String] the hour. @param m [String] the minutes past the hour. @param s [String] the seconds past the minute. @return [Integer] the number of seconds after midnight. @raise [InvalidPosixTimeZone] if the mm and ss values are greater than 59.
get_seconds_after_midnight_from_hms
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/posix_time_zone_parser.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/posix_time_zone_parser.rb
MIT
def check_scan(s, pattern) result = s.scan(pattern) raise InvalidPosixTimeZone, "Expected '#{s.rest}' to match #{pattern} in POSIX-style time zone string." unless result result end
Scans for a pattern and raises an exception if the pattern does not match the input. @param s [StringScanner] the `StringScanner` to scan. @param pattern [Regexp] the pattern to match. @return [String] the result of the scan. @raise [InvalidPosixTimeZone] if the pattern does not match the input.
check_scan
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/posix_time_zone_parser.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/posix_time_zone_parser.rb
MIT
def initialize super begin require('tzinfo/data') rescue LoadError raise TZInfoDataNotFound, "The tzinfo-data gem could not be found (require 'tzinfo/data' failed)." end if TZInfo::Data.const_defined?(:LOCATION) # Format 2 @base_path = File.join(TZInfo::Data::LOCATION, 'tzinfo', 'data') else # Format 1 data_file = File.join('', 'tzinfo', 'data.rb') path = $".reverse_each.detect {|p| p.end_with?(data_file) } if path @base_path = RubyCoreSupport.untaint(File.join(File.dirname(path), 'data')) else @base_path = 'tzinfo/data' end end require_index('timezones') require_index('countries') @data_timezone_identifiers = Data::Indexes::Timezones.data_timezones @linked_timezone_identifiers = Data::Indexes::Timezones.linked_timezones @countries = Data::Indexes::Countries.countries @country_codes = @countries.keys.sort!.freeze end
Initializes a new {RubyDataSource} instance. @raise [TZInfoDataNotFound] if the tzinfo-data gem could not be found (i.e. `require 'tzinfo/data'` failed).
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/ruby_data_source.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/ruby_data_source.rb
MIT
def load_timezone_info(identifier) valid_identifier = validate_timezone_identifier(identifier) split_identifier = valid_identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__').split('/') begin require_definition(split_identifier) m = Data::Definitions split_identifier.each {|part| m = m.const_get(part) } m.get rescue LoadError, NameError => e raise InvalidTimezoneIdentifier, "#{e.message.encode(Encoding::UTF_8)} (loading #{valid_identifier})" end end
Returns a {TimezoneInfo} instance for the given time zone identifier. The result will either be a {ConstantOffsetDataTimezoneInfo}, a {TransitionsDataTimezoneInfo} or a {LinkedTimezoneInfo} depending on the type of time zone. @param identifier [String] A time zone identifier. @return [TimezoneInfo] a {TimezoneInfo} instance for the given time zone identifier. @raise [InvalidTimezoneIdentifier] if the time zone is not found or the identifier is invalid.
load_timezone_info
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/ruby_data_source.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/ruby_data_source.rb
MIT
def require_definition(identifier) require_data('definitions', *identifier) end
Requires a zone definition by its identifier (split on /). @param identifier [Array<string>] the component parts of a time zone identifier (split on /). This must have already been validated.
require_definition
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/ruby_data_source.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/ruby_data_source.rb
MIT
def require_index(name) require_data('indexes', name) end
Requires an index by its name. @param name [String] an index name.
require_index
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/ruby_data_source.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/ruby_data_source.rb
MIT
def require_data(*file) require(File.join(@base_path, *file)) end
Requires a file from tzinfo/data. @param file [Array<String>] a relative path to a file to be required.
require_data
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/ruby_data_source.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/ruby_data_source.rb
MIT
def version_info # The TZInfo::Data::VERSION constant is only available from v1.2014.8 # onwards. "tzdb v#{TZInfo::Data::Version::TZDATA}#{TZInfo::Data.const_defined?(:VERSION) ? ", tzinfo-data v#{TZInfo::Data::VERSION}" : ''}" end
@return [String] a `String` containing TZInfo::Data version infomation for inclusion in the #to_s and #inspect output.
version_info
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/ruby_data_source.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/ruby_data_source.rb
MIT
def initialize(identifier) raise ArgumentError, 'identifier must be specified' unless identifier @identifier = identifier.freeze end
Initializes a new TimezoneInfo. The passed in `identifier` instance will be frozen. @param identifier [String] the identifier of the time zone. @raise [ArgumentError] if `identifier` is `nil`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/timezone_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/timezone_info.rb
MIT
def inspect "#<#{self.class}: #@identifier>" end
@return [String] the internal object state as a programmer-readable `String`.
inspect
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/timezone_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/timezone_info.rb
MIT
def raise_not_implemented(method_name) raise NotImplementedError, "Subclasses must override #{method_name}" end
Raises a {NotImplementedError}. @param method_name [String] the name of the method that must be overridden. @raise NotImplementedError always.
raise_not_implemented
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/timezone_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/timezone_info.rb
MIT
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
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 {TimezoneTransition#timestamp_value timestamp_value} that is greater than the {TimezoneTransition#timestamp_value timestamp_value} of the prior transition. @param identifier [String] the identifier of the time zone. @param transitions [Array<TimezoneTransitions>] an `Array` of transitions that each indicate when a change occurs in the locally observed time. @raise [ArgumentError] if `identifier` is `nil`. @raise [ArgumentError] if `transitions` is `nil`. @raise [ArgumentError] if `transitions` is an empty `Array`.
initialize
ruby
tzinfo/tzinfo
lib/tzinfo/data_sources/transitions_data_timezone_info.rb
https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/data_sources/transitions_data_timezone_info.rb
MIT