Class: OpenHAB::Core::Types::DateTimeType
- Inherits:
-
Object
- Object
- OpenHAB::Core::Types::DateTimeType
- Defined in:
- lib/openhab/core/types/date_time_type.rb
Overview
DateTimeType uses a ZonedDateTime internally.
Class Method Summary collapse
-
.parse(time_string) ⇒ DateTimeType
Parse a time string into a DateTimeType.
Instance Method Summary collapse
-
#+(other) ⇒ DateTimeType
Add other to self.
-
#-(other) ⇒ DateTimeType, Duration
Subtract other from self.
-
#<=>(other) ⇒ Integer?
Comparison.
-
#coerce(other) ⇒ [DateTimeType, DateTimeType]?
Type Coercion.
-
#eql?(other) ⇒ true, false
Check equality without type conversion.
-
#initialize(value = nil) ⇒ DateTimeType
constructor
Create a new instance of DateTimeType.
-
#method_missing(method) ⇒ Object
Forward missing methods to the
ZonedDateTimeobject or a rubyTimeobject representing the same instant. -
#to_f ⇒ Float
Returns the value of time as a floating point number of seconds since the Epoch.
-
#to_i ⇒ Integer
Returns the value of time as an integer number of seconds since the Epoch.
- #to_zoned_date_time(context = nil) ⇒ ZonedDateTime
-
#utc? ⇒ true, false
deprecated
Deprecated.
This method has been deprecated in openHAB 4.3.
-
#utc_offset ⇒ Integer
deprecated
Deprecated.
This method has been deprecated in openHAB 4.3.
-
#wday ⇒ Integer
deprecated
Deprecated.
This method has been deprecated in openHAB 4.3.
-
#zone ⇒ String
deprecated
Deprecated.
This method has been deprecated in openHAB 4.3.
Methods included from Type
Constructor Details
#initialize(value = nil) ⇒ DateTimeType
Create a new instance of DateTimeType
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/openhab/core/types/date_time_type.rb', line 79 def initialize(value = nil) if value.nil? super() return elsif value.respond_to?(:to_instant) super(value.to_instant) return elsif value.respond_to?(:to_zoned_date_time) super(value.to_zoned_date_time) return elsif value.respond_to?(:to_time) super(value.to_time.to_zoned_date_time) return elsif value.respond_to?(:to_str) # strings respond_do?(:to_d), but we want to avoid that conversion super(value.to_str) return elsif value.respond_to?(:to_d) super(Time.at(value.to_d).to_zoned_date_time) return end super end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
Forward missing methods to the ZonedDateTime object or a ruby Time object representing the same instant
217 218 219 220 221 222 223 |
# File 'lib/openhab/core/types/date_time_type.rb', line 217 def method_missing(method, ...) return to_instant.send(method, ...) if to_instant.respond_to?(method) return zoned_date_time.send(method, ...) if zoned_date_time.respond_to?(method) return to_time.send(method, ...) if ::Time.method_defined?(method.to_sym) super end |
Class Method Details
.parse(time_string) ⇒ DateTimeType
openHAB’s DateTimeType.new(String) constructor will parse time-only strings and fill in 1970-01-01 as the date, whereas this method will use the current date.
Parse a time string into a OpenHAB::Core::Types::DateTimeType.
33 34 35 36 37 |
# File 'lib/openhab/core/types/date_time_type.rb', line 33 def parse(time_string) DateTimeType.new(DSL.try_parse_time_like(time_string).to_zoned_date_time) rescue ArgumentError raise ArgumentError, e. end |
Instance Method Details
#+(other) ⇒ DateTimeType
Add other to self
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/openhab/core/types/date_time_type.rb', line 230 def +(other) if other.is_a?(Duration) DateTimeType.new(zoned_date_time.plus(other)) elsif other.respond_to?(:to_d) DateTimeType.new(zoned_date_time.plus_nanos((other.to_d * 1_000_000_000).to_i)) elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(to_d)) lhs + rhs else raise TypeError, "\#{other.class} can't be coerced into \#{self.class}" end end |
#-(other) ⇒ DateTimeType, Duration
Subtract other from self
if other is a Duration-like object, the result is a new OpenHAB::Core::Types::DateTimeType of duration seconds earlier in time.
if other is a DateTime-like object, the result is a Duration representing how long between the two instants in time.
253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/openhab/core/types/date_time_type.rb', line 253 def -(other) if other.is_a?(Duration) DateTimeType.new(zoned_date_time.minus(other)) elsif other.respond_to?(:to_time) to_time - other.to_time elsif other.respond_to?(:to_d) DateTimeType.new(zoned_date_time.minus_nanos((other.to_d * 1_000_000_000).to_i)) elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(to_d)) lhs - rhs else raise TypeError, "\#{other.class} can't be coerced into \#{self.class}" end end |
#<=>(other) ⇒ Integer?
Comparison
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/openhab/core/types/date_time_type.rb', line 125 def <=>(other) logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" } if other.is_a?(self.class) compare_to(other) elsif other.respond_to?(:to_time) to_time <=> other.to_time elsif other.respond_to?(:coerce) return nil unless (lhs, rhs = other.coerce(self)) lhs <=> rhs end end |
#coerce(other) ⇒ [DateTimeType, DateTimeType]?
Type Coercion
Coerce object to a DateTimeType
147 148 149 150 151 152 153 |
# File 'lib/openhab/core/types/date_time_type.rb', line 147 def coerce(other) logger.trace { "Coercing #{self} as a request from #{other.class}" } return [other, to_instant] if other.respond_to?(:to_instant) return [other, zoned_date_time] if other.respond_to?(:to_zoned_date_time) [DateTimeType.new(other), self] if other.respond_to?(:to_time) end |
#eql?(other) ⇒ true, false
Check equality without type conversion
109 110 111 112 113 |
# File 'lib/openhab/core/types/date_time_type.rb', line 109 def eql?(other) return false unless other.instance_of?(self.class) compare_to(other).zero? end |
#to_f ⇒ Float
Returns the value of time as a floating point number of seconds since the Epoch
160 161 162 |
# File 'lib/openhab/core/types/date_time_type.rb', line 160 def to_f to_instant.then { |instant| instant.epoch_second + (instant.nano / 1_000_000_000) } end |
#to_i ⇒ Integer
Returns the value of time as an integer number of seconds since the Epoch
|
|
# File 'lib/openhab/core/types/date_time_type.rb', line 67
|
#to_zoned_date_time(context = nil) ⇒ ZonedDateTime
40 41 42 |
# File 'lib/openhab/core/types/date_time_type.rb', line 40 def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument zoned_date_time(ZoneId.system_default) end |
#utc? ⇒ true, false
This method has been deprecated in openHAB 4.3.
Returns true if time represents a time in UTC (GMT)
180 181 182 |
# File 'lib/openhab/core/types/date_time_type.rb', line 180 def utc? utc_offset.zero? end |
#utc_offset ⇒ Integer
This method has been deprecated in openHAB 4.3.
The offset in seconds from UTC
170 171 172 |
# File 'lib/openhab/core/types/date_time_type.rb', line 170 def utc_offset zoned_date_time.offset.total_seconds end |
#wday ⇒ Integer
This method has been deprecated in openHAB 4.3.
Returns an integer representing the day of the week, 0..6, with Sunday == 0.
190 191 192 |
# File 'lib/openhab/core/types/date_time_type.rb', line 190 def wday zoned_date_time.day_of_week.value % 7 end |
#zone ⇒ String
This method has been deprecated in openHAB 4.3.
The timezone
200 201 202 |
# File 'lib/openhab/core/types/date_time_type.rb', line 200 def zone zoned_date_time.zone.id end |