123456789_123456789_123456789_123456789_123456789_

Class: TZInfo::DateTimeWithOffset

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, DateTime
Instance Chain:
self, WithOffset, DateTime
Inherits: DateTime
  • Object
Defined in: lib/tzinfo/datetime_with_offset.rb

Overview

A subclass of DateTime used to represent local times. DateTimeWithOffset holds a reference to the related TimezoneOffset and overrides various methods to return results appropriate for the TimezoneOffset. Certain operations will clear the associated TimezoneOffset (if the TimezoneOffset would not necessarily be valid for the result). Once the TimezoneOffset has been cleared, DateTimeWithOffset behaves identically to DateTime.

Arithmetic performed on DateTimeWithOffset instances is not time zone-aware. Regardless of whether transitions in the time zone are crossed, results of arithmetic operations will always maintain the same offset from UTC (offset). The associated TimezoneOffset will aways be cleared.

Instance Attribute Summary

Instance Method Summary

WithOffset - Included

#strftime

Overrides the Time, DateTime or Timestamp version of strftime, replacing %Z with the abbreviation of the associated TimezoneOffset.

#if_timezone_offset

Performs a calculation if there is an associated TimezoneOffset.

Instance Attribute Details

#timezone_offsetTimezoneOffset (readonly)

Returns:

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 24

attr_reader :timezone_offset

Instance Method Details

#clear_timezone_offsetDateTimeWithOffset (protected)

Clears the associated TimezoneOffset.

Returns:

  • (DateTimeWithOffset)

    self.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 148

def clear_timezone_offset
  @timezone_offset = nil
  self
end

#downto(min)

An overridden version of DateTime#downto that clears the associated TimezoneOffset of the returned or yielded instances.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 61

def downto(min)
  if block_given?
    super {|dt| yield dt.clear_timezone_offset }
  else
    enum = super
    enum.each {|dt| dt.clear_timezone_offset }
    enum
  end
end

#englandDateTime

An overridden version of DateTime#england that preserves the associated TimezoneOffset.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 75

def england
  # super doesn't call #new_start on MRI, so each method has to be
  # individually overridden.
  if_timezone_offset(super) {|o,dt| dt.set_timezone_offset(o) }
end

#gregorianDateTime

An overridden version of DateTime#gregorian that preserves the associated TimezoneOffset.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 85

def gregorian
  # super doesn't call #new_start on MRI, so each method has to be
  # individually overridden.
  if_timezone_offset(super) {|o,dt| dt.set_timezone_offset(o) }
end

#italyDateTime

An overridden version of DateTime#italy that preserves the associated TimezoneOffset.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 95

def italy
  # super doesn't call #new_start on MRI, so each method has to be
  # individually overridden.
  if_timezone_offset(super) {|o,dt| dt.set_timezone_offset(o) }
end

#julianDateTime

An overridden version of DateTime#julian that preserves the associated TimezoneOffset.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 105

def julian
  # super doesn't call #new_start on MRI, so each method has to be
  # individually overridden.
  if_timezone_offset(super) {|o,dt| dt.set_timezone_offset(o) }
end

#new_start(start = Date::ITALY) ⇒ DateTime

An overridden version of DateTime#new_start that preserves the associated TimezoneOffset.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 115

def new_start(start = Date::ITALY)
  if_timezone_offset(super) {|o,dt| dt.set_timezone_offset(o) }
end

#set_timezone_offset(timezone_offset) ⇒ DateTimeWithOffset

Sets the associated TimezoneOffset.

Parameters:

Returns:

  • (DateTimeWithOffset)

    self.

Raises:

  • (ArgumentError)

    if timezone_offset is nil.

  • (ArgumentError)

    if timezone_offset.observed_utc_offset does not equal self.offset * 86400.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 34

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 offset * 86400 != timezone_offset.observed_utc_offset
  @timezone_offset = timezone_offset
  self
end

#step(limit, step = 1)

An overridden version of DateTime#step that clears the associated TimezoneOffset of the returned or yielded instances.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 121

def step(limit, step = 1)
  if block_given?
    super {|dt| yield dt.clear_timezone_offset }
  else
    enum = super
    enum.each {|dt| dt.clear_timezone_offset }
    enum
  end
end

#to_timeTime

An overridden version of DateTime#to_time that, if there is an associated TimezoneOffset, returns a DateTimeWithOffset with that offset.

Returns:

  • (Time)

    if there is an associated TimezoneOffset, a TimeWithOffset representation of this DateTimeWithOffset, otherwise a Time representation.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 48

def to_time
  if_timezone_offset(super) do |o,t|
    # Ruby 2.4.0 changed the behaviour of to_time so that it preserves the
    # offset instead of converting to the system local timezone.
    #
    # When self has an associated TimezonePeriod, this implementation will
    # preserve the offset on all versions of Ruby.
    TimeWithOffset.at(t.to_i, t.subsec * 1_000_000).set_timezone_offset(o)
  end
end

#upto(max)

An overridden version of DateTime#upto that clears the associated TimezoneOffset of the returned or yielded instances.

[ GitHub ]

  
# File 'lib/tzinfo/datetime_with_offset.rb', line 133

def upto(max)
  if block_given?
    super {|dt| yield dt.clear_timezone_offset }
  else
    enum = super
    enum.each {|dt| dt.clear_timezone_offset }
    enum
  end
end