Class: OpenHAB::Core::Types::DateTimeType

Inherits:
Object
  • Object
show all
Includes:
Command, State
Defined in:
lib/openhab/core/types/date_time_type.rb

Overview

DateTimeType uses a ZonedDateTime internally.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Type

#==

Constructor Details

#initialize(value = nil) ⇒ DateTimeType

Create a new instance of DateTimeType

Parameters:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/openhab/core/types/date_time_type.rb', line 92

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) ⇒ Object

Forward missing methods to the ZonedDateTime object or a ruby Time object representing the same instant



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/openhab/core/types/date_time_type.rb', line 237

def method_missing(method, ...)
  # @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, ...)
  end

  return zoned_date_time.send(method, ...) if zoned_date_time.respond_to?(method)
  return to_time.send(method, ...) if ::Time.instance_methods.include?(method.to_sym)

  super
end

Class Method Details

.parse(time_string) ⇒ DateTimeType

Note:

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.

Parameters:

Returns:



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

Parameters:

Returns:



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/openhab/core/types/date_time_type.rb', line 254

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.

Parameters:

Returns:



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/openhab/core/types/date_time_type.rb', line 277

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

Parameters:

  • other (Object)

    object to compare to

Returns:

  • (Integer, nil)

    -1, 0, +1 depending on whether other is less than, equal to, or greater than self

    nil is returned if the two values are incomparable.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/openhab/core/types/date_time_type.rb', line 141

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

Parameters:

  • other (Time)

    object to coerce to a DateTimeType

Returns:



166
167
168
169
170
171
172
# File 'lib/openhab/core/types/date_time_type.rb', line 166

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

Returns:

  • (true, false)

    if the same value is represented, without type conversion



122
123
124
125
126
127
128
129
# File 'lib/openhab/core/types/date_time_type.rb', line 122

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_fFloat

Returns the value of time as a floating point number of seconds since the Epoch

Returns:

  • (Float)

    Number of seconds since the Epoch, with nanosecond presicion



179
180
181
# File 'lib/openhab/core/types/date_time_type.rb', line 179

def to_f
  to_instant.then { |instant| instant.epoch_second + (instant.nano / 1_000_000_000) }
end

#to_iInteger

Returns the value of time as an integer number of seconds since the Epoch

Returns:

  • (Integer)

    Number of seconds since the Epoch



# File 'lib/openhab/core/types/date_time_type.rb', line 80

#to_instantInstant

Returns:



65
# File 'lib/openhab/core/types/date_time_type.rb', line 65

alias_method :to_instant, :get_instant

#to_zoned_date_time(context = nil) ⇒ ZonedDateTime

Parameters:

  • context (ZonedDateTime, nil) (defaults to: nil)

    A ZonedDateTime used to fill in missing fields during conversion. Not used in this class.

Returns:



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

Deprecated.

This method has been deprecated in openHAB 4.3.

Returns true if time represents a time in UTC (GMT)

Returns:

  • (true, false)

    true if utc_offset == 0, false otherwise



199
200
201
# File 'lib/openhab/core/types/date_time_type.rb', line 199

def utc?
  utc_offset.zero?
end

#utc_offsetInteger

Deprecated.

This method has been deprecated in openHAB 4.3.

The offset in seconds from UTC

Returns:

  • (Integer)

    The offset from UTC, in seconds



189
190
191
# File 'lib/openhab/core/types/date_time_type.rb', line 189

def utc_offset
  zoned_date_time.offset.total_seconds
end

#wdayInteger

Deprecated.

This method has been deprecated in openHAB 4.3.

Returns an integer representing the day of the week, 0..6, with Sunday == 0.

Returns:



209
210
211
# File 'lib/openhab/core/types/date_time_type.rb', line 209

def wday
  zoned_date_time.day_of_week.value % 7
end

#zoneString

Deprecated.

This method has been deprecated in openHAB 4.3.

The timezone

Returns:

  • (String)

    The timezone in [+-]hh:mm(:ss) format (Z for UTC)



219
220
221
# File 'lib/openhab/core/types/date_time_type.rb', line 219

def zone
  zoned_date_time.zone.id
end