Class: TZInfo::TimestampWithOffset
- 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
-
#timezone_offset ⇒ TimezoneOffset
readonly
The TimezoneOffset associated with this instance.
Attributes inherited from Timestamp
#sub_second, #utc_offset, #value
Class Method Summary collapse
-
.set_timezone_offset(timestamp, timezone_offset) ⇒ TimestampWithOffset
Creates a new TimestampWithOffset from a given Timestamp and TimezoneOffset.
Instance Method Summary collapse
-
#set_timezone_offset(timezone_offset) ⇒ TimestampWithOffset
Sets the associated TimezoneOffset of this TimestampWithOffset.
-
#to_datetime ⇒ DateTime
An overridden version of TZInfo::Timestamp#to_datetime, if there is an associated TimezoneOffset, returns a DateTimeWithOffset with that offset.
-
#to_time ⇒ Time
An overridden version of TZInfo::Timestamp#to_time that, if there is an associated TimezoneOffset, returns a TimeWithOffset with that offset.
Methods included from WithOffset
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_offset ⇒ TimezoneOffset (readonly)
Returns the TZInfo::TimezoneOffset associated with this instance.
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
.
32 33 34 35 36 |
# File 'lib/tzinfo/timestamp_with_offset.rb', line 32 def self.set_timezone_offset(, timezone_offset) raise ArgumentError, 'timestamp must be specified' unless raise ArgumentError, 'timezone_offset must be specified' unless timezone_offset new!(.value, .sub_second, timezone_offset.observed_utc_offset).set_timezone_offset(timezone_offset) end |
Instance Method Details
#set_timezone_offset(timezone_offset) ⇒ TimestampWithOffset
Sets the associated TZInfo::TimezoneOffset of this TZInfo::TimestampWithOffset.
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_datetime ⇒ DateTime
An overridden version of TZInfo::Timestamp#to_datetime, if there is an associated TZInfo::TimezoneOffset, returns a DateTimeWithOffset with that offset.
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_time ⇒ Time
An overridden version of TZInfo::Timestamp#to_time that, if there is an associated TZInfo::TimezoneOffset, returns a TZInfo::TimeWithOffset with that offset.
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 |