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, *args, &block) ⇒ Object
Forward missing methods to the
ZonedDateTime
object or a rubyTime
object 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_instant ⇒ Instant
- #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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/openhab/core/types/date_time_type.rb', line 97 def initialize(value = nil) if value.nil? super() return elsif OpenHAB::Core.version >= OpenHAB::Core::V4_3 && 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, *args, &block) ⇒ Object
Forward missing methods to the ZonedDateTime
object or a ruby Time
object representing the same instant
242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/openhab/core/types/date_time_type.rb', line 242 def method_missing(method, *args, &block) # @deprecated OH 4.2 Remove version check when dropping OH 4.2 if OpenHAB::Core.version >= OpenHAB::Core::V4_3 && to_instant.respond_to?(method) return to_instant.send(method, *args, &block) end return zoned_date_time.send(method, *args, &block) if zoned_date_time.respond_to?(method) return to_time.send(method, *args, &block) if ::Time.instance_methods.include?(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.
39 40 41 42 43 |
# File 'lib/openhab/core/types/date_time_type.rb', line 39 def parse(time_string) DateTimeType.new(DSL.try_parse_time_like(time_string).to_zoned_date_time) rescue ArgumentError raise ArgumentError, e.message end |
Instance Method Details
#+(other) ⇒ DateTimeType
Add other to self
259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/openhab/core/types/date_time_type.rb', line 259 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.
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/openhab/core/types/date_time_type.rb', line 282 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/openhab/core/types/date_time_type.rb', line 146 def <=>(other) logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" } if other.is_a?(self.class) # @deprecated OH 4.2 Call compare_to(other) in OH 4.3 to avoid the deprecated getZonedDateTime() return compare_to(other) if OpenHAB::Core.version >= OpenHAB::Core::V4_3 zoned_date_time <=> other.zoned_date_time 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
171 172 173 174 175 176 177 |
# File 'lib/openhab/core/types/date_time_type.rb', line 171 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
127 128 129 130 131 132 133 134 |
# File 'lib/openhab/core/types/date_time_type.rb', line 127 def eql?(other) return false unless other.instance_of?(self.class) # @deprecated OH 4.2 Call compare_to(other).zero? in OH 4.3 to avoid the deprecated getZonedDateTime() return compare_to(other).zero? if OpenHAB::Core.version >= OpenHAB::Core::V4_3 zoned_date_time.compare_to(other.zoned_date_time).zero? end |
#to_f ⇒ Float
Returns the value of time as a floating point number of seconds since the Epoch
184 185 186 |
# File 'lib/openhab/core/types/date_time_type.rb', line 184 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 85
|
#to_instant ⇒ Instant
63 64 65 66 67 68 |
# File 'lib/openhab/core/types/date_time_type.rb', line 63 def to_instant(_context = nil) # @deprecated OH 3.4 getInstant() was added in OH 4.0 return get_instant if respond_to?(:get_instant) zoned_date_time.to_instant end |
#to_zoned_date_time(context = nil) ⇒ ZonedDateTime
48 49 50 |
# File 'lib/openhab/core/types/date_time_type.rb', line 48 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)
204 205 206 |
# File 'lib/openhab/core/types/date_time_type.rb', line 204 def utc? utc_offset.zero? end |
#utc_offset ⇒ Integer
This method has been deprecated in openHAB 4.3.
The offset in seconds from UTC
194 195 196 |
# File 'lib/openhab/core/types/date_time_type.rb', line 194 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.
214 215 216 |
# File 'lib/openhab/core/types/date_time_type.rb', line 214 def wday zoned_date_time.day_of_week.value % 7 end |
#zone ⇒ String
This method has been deprecated in openHAB 4.3.
The timezone
224 225 226 |
# File 'lib/openhab/core/types/date_time_type.rb', line 224 def zone zoned_date_time.zone.id end |