Class: TZInfo::TimeWithOffset
- Inherits:
-
Time
- Object
- Time
- TZInfo::TimeWithOffset
- Includes:
- WithOffset
- Defined in:
- lib/tzinfo/time_with_offset.rb
Overview
A subclass of Time
used to represent local times. TimeWithOffset 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, TimeWithOffset behaves identically to Time
.
Arithmetic performed on TimeWithOffset 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
(utc_offset
). The associated TimezoneOffset will aways be cleared.
Instance Attribute Summary collapse
-
#timezone_offset ⇒ TimezoneOffset
readonly
The TimezoneOffset associated with this instance.
Instance Method Summary collapse
-
#dst? ⇒ Boolean
(also: #isdst)
An overridden version of
Time#dst?
that, if there is an associated TimezoneOffset, returns the result of calling dst? on that offset. -
#gmtime ⇒ TimeWithOffset
An overridden version of
Time#gmtime
that clears the associated TimezoneOffset. -
#localtime(*args) ⇒ TimeWithOffset
An overridden version of
Time#localtime
that clears the associated TimezoneOffset. -
#round(ndigits = 0) ⇒ Time
An overridden version of
Time#round
that, if there is an associated TimezoneOffset, returns a TimeWithOffset preserving that offset. -
#set_timezone_offset(timezone_offset) ⇒ TimeWithOffset
Marks this TimeWithOffset as a local time with the UTC offset of a given TimezoneOffset and sets the associated TimezoneOffset.
-
#to_a ⇒ Array
An overridden version of
Time#to_a
. -
#to_datetime ⇒ DateTime
An overridden version of
Time#to_datetime
that, if there is an associated TimezoneOffset, returns a DateTimeWithOffset with that offset. -
#utc ⇒ TimeWithOffset
An overridden version of
Time#utc
that clears the associated TimezoneOffset. -
#zone ⇒ String
An overridden version of
Time#zone
that, if there is an associated TimezoneOffset, returns the abbreviation of that offset.
Methods included from WithOffset
Instance Attribute Details
#timezone_offset ⇒ TimezoneOffset (readonly)
Returns the TZInfo::TimezoneOffset associated with this instance.
21 22 23 |
# File 'lib/tzinfo/time_with_offset.rb', line 21 def timezone_offset @timezone_offset end |
Instance Method Details
#dst? ⇒ Boolean Also known as: isdst
An overridden version of Time#dst?
that, if there is an associated
TZInfo::TimezoneOffset, returns the result of calling dst?
on that offset.
43 44 45 46 |
# File 'lib/tzinfo/time_with_offset.rb', line 43 def dst? to = timezone_offset to ? to.dst? : super end |
#gmtime ⇒ TimeWithOffset
An overridden version of Time#gmtime
that clears the associated
TZInfo::TimezoneOffset.
53 54 55 56 57 |
# File 'lib/tzinfo/time_with_offset.rb', line 53 def gmtime super @timezone_offset = nil self end |
#localtime(*args) ⇒ TimeWithOffset
An overridden version of Time#localtime
that clears the associated
TZInfo::TimezoneOffset.
63 64 65 66 67 |
# File 'lib/tzinfo/time_with_offset.rb', line 63 def localtime(*args) super @timezone_offset = nil self end |
#round(ndigits = 0) ⇒ Time
An overridden version of Time#round
that, if there is an associated
TZInfo::TimezoneOffset, returns a TZInfo::TimeWithOffset preserving that offset.
73 74 75 |
# File 'lib/tzinfo/time_with_offset.rb', line 73 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 |
#set_timezone_offset(timezone_offset) ⇒ TimeWithOffset
Marks this TZInfo::TimeWithOffset as a local time with the UTC offset of a given TZInfo::TimezoneOffset and sets the associated TZInfo::TimezoneOffset.
30 31 32 33 34 35 |
# File 'lib/tzinfo/time_with_offset.rb', line 30 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 |
#to_a ⇒ Array
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
TZInfo::TimezoneOffset.
82 83 84 85 86 87 88 |
# File 'lib/tzinfo/time_with_offset.rb', line 82 def to_a if_timezone_offset(super) do |o,a| a[8] = o.dst? a[9] = o.abbreviation a end end |
#to_datetime ⇒ DateTime
An overridden version of Time#to_datetime
that, if there is an
associated TZInfo::TimezoneOffset, returns a DateTimeWithOffset with that
offset.
119 120 121 122 123 124 125 126 |
# File 'lib/tzinfo/time_with_offset.rb', line 119 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 |
#utc ⇒ TimeWithOffset
An overridden version of Time#utc
that clears the associated
TZInfo::TimezoneOffset.
94 95 96 97 98 |
# File 'lib/tzinfo/time_with_offset.rb', line 94 def utc super @timezone_offset = nil self end |
#zone ⇒ String
An overridden version of Time#zone
that, if there is an associated
TZInfo::TimezoneOffset, returns the abbreviation
of that offset.
107 108 109 110 |
# File 'lib/tzinfo/time_with_offset.rb', line 107 def zone to = timezone_offset to ? to.abbreviation : super end |