123456789_123456789_123456789_123456789_123456789_

Class: TZInfo::TimestampWithOffset

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Timestamp
Instance Chain:
self, WithOffset, Timestamp, Comparable
Inherits: TZInfo::Timestamp
Defined in: lib/tzinfo/timestamp_with_offset.rb

Overview

A subclass of Timestamp used to represent local times. TimestampWithOffset 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, TimestampWithOffset behaves identically to Timestamp.

Constant Summary

Timestamp - Inherited

JD_EPOCH

Class Method Summary

Timestamp - Inherited

.create

Returns a new Timestamp representing the (proleptic Gregorian calendar) date and time specified by the supplied parameters.

.for

When used without a block, returns a Timestamp representation of a given Time, DateTime or Timestamp.

.new

Initializes a new Timestamp.

.utc

Creates a new UTC Timestamp.

.for_datetime

Creates a Timestamp that represents a given DateTime, optionally ignoring the offset.

.for_time

Creates a Timestamp that represents a given Time, optionally ignoring the offset.

.for_time_like

Creates a Timestamp that represents a given Time-like object, optionally ignoring the offset (if the time_like responds to utc_offset).

.for_timestamp

Returns a Timestamp that represents another Timestamp, optionally ignoring the offset.

.is_time_like?

Determines if an object is like a Time (for the purposes of converting to a Timestamp with for), responding to to_i and subsec.

.new!

Constructs a new instance of self (i.e. Timestamp or a subclass of Timestamp) without validating the parameters.

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.

Timestamp - Inherited

#<=>

Compares this Timestamp with another.

#add_and_set_utc_offset

Adds a number of seconds to the Timestamp value, setting the UTC offset of the result.

#eql?, #hash, #inspect,
#strftime

Formats this Timestamp according to the directives in the given format string.

#to_datetime

Converts this Timestamp to a Gregorian DateTime.

#to_i

Converts this Timestamp to an Integer number of seconds since 1970-01-01 00:00:00 UTC (ignoring leap seconds).

#to_s,
#to_time

Converts this Timestamp to a Time.

#utc,
#new_datetime

Constructs a new instance of a DateTime or DateTime-like class with the same value, sub_second and utc_offset as this Timestamp.

#new_time

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.

#initialize!

Initializes a new Timestamp without validating the parameters.

#sub_second_to_s

Converts the sub_second value to a String suitable for appending to the String representation of a Timestamp.

#value_and_sub_second_to_s

Converts the value and sub-seconds to a String, adding on the given offset.

Constructor Details

This class inherits a constructor from TZInfo::Timestamp

Class Method Details

.set_timezone_offset(timestamp, timezone_offset) ⇒ TimestampWithOffset

Creates a new TimestampWithOffset from a given Timestamp and TimezoneOffset.

time of timestamp.

Parameters:

Returns:

  • (TimestampWithOffset)

    a TimestampWithOffset that has the same value and sub_second as the timestamp parameter, a utc_offset equal to the observed_utc_offset of the timezone_offset parameter and timezone_offset set to the timezone_offset parameter.

Raises:

  • (ArgumentError)

    if timestamp or timezone_offset is nil.

[ GitHub ]

  
# File 'lib/tzinfo/timestamp_with_offset.rb', line 32

def self.set_timezone_offset(timestamp, timezone_offset)
  raise ArgumentError, 'timestamp must be specified' unless timestamp
  raise ArgumentError, 'timezone_offset must be specified' unless timezone_offset
  new!(timestamp.value, timestamp.sub_second, timezone_offset.observed_utc_offset).set_timezone_offset(timezone_offset)
end

Instance Attribute Details

#timezone_offsetTimezoneOffset (readonly)

Returns:

[ GitHub ]

  
# File 'lib/tzinfo/timestamp_with_offset.rb', line 17

attr_reader :timezone_offset

Instance Method Details

#set_timezone_offset(timezone_offset) ⇒ TimestampWithOffset

Sets the associated TimezoneOffset of this TimestampWithOffset.

Parameters:

Returns:

  • (TimestampWithOffset)

    self.

Raises:

  • (ArgumentError)

    if timezone_offset is nil.

  • (ArgumentError)

    if self.utc? is true.

  • (ArgumentError)

    if timezone_offset.observed_utc_offset does not equal self.utc_offset.

[ GitHub ]

  
# File 'lib/tzinfo/timestamp_with_offset.rb', line 47

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

#to_datetimeDateTime

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

Returns:

  • (DateTime)

    if there is an associated TimezoneOffset, a DateTimeWithOffset representation of this TimestampWithOffset, otherwise a DateTime representation.

[ GitHub ]

  
# File 'lib/tzinfo/timestamp_with_offset.rb', line 76

def to_datetime
  to = timezone_offset
  if to
    new_datetime(DateTimeWithOffset).set_timezone_offset(to)
  else
    super
  end
end

#to_timeTime

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

Returns:

  • (Time)

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

[ GitHub ]

  
# File 'lib/tzinfo/timestamp_with_offset.rb', line 60

def to_time
  to = timezone_offset
  if to
    new_time(TimeWithOffset).set_timezone_offset(to)
  else
    super
  end
end