Class: TZInfo::TimestampWithOffset

Inherits:
Timestamp
  • Object
show all
Includes:
WithOffset
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.

Instance Attribute Summary collapse

Attributes inherited from Timestamp

#sub_second, #utc_offset, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithOffset

#strftime

Methods inherited from Timestamp

#<=>, #add_and_set_utc_offset, create, for, #hash, #initialize, #inspect, #strftime, #to_i, #to_s, #utc, utc, #utc?

Constructor Details

This class inherits a constructor from TZInfo::Timestamp

Instance Attribute Details

#timezone_offsetTimezoneOffset (readonly)

Returns the TZInfo::TimezoneOffset associated with this instance.

Returns:



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

def timezone_offset
  @timezone_offset
end

Class Method Details

.set_timezone_offset(timestamp, timezone_offset) ⇒ TimestampWithOffset

Creates a new TZInfo::TimestampWithOffset from a given TZInfo::Timestamp and TZInfo::TimezoneOffset.

time of timestamp.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if timestamp or timezone_offset is nil.



32
33
34
35
36
# 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 Method Details

#set_timezone_offset(timezone_offset) ⇒ TimestampWithOffset

Parameters:

Returns:

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.



47
48
49
50
51
52
# 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 TZInfo::Timestamp#to_datetime, if there is an associated TZInfo::TimezoneOffset, returns a DateTimeWithOffset with that offset.

Returns:



76
77
78
79
80
81
82
83
# 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 TZInfo::Timestamp#to_time that, if there is an associated TZInfo::TimezoneOffset, returns a TZInfo::TimeWithOffset with that offset.

Returns:



60
61
62
63
64
65
66
67
# 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