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:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/openhab/core/types/date_time_type.rb', line 83

def initialize(value = nil)
  if value.nil?
    super()
    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



212
213
214
215
216
217
# File 'lib/openhab/core/types/date_time_type.rb', line 212

def method_missing(method, *args, &block)
  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

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:



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/openhab/core/types/date_time_type.rb', line 224

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:



247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/openhab/core/types/date_time_type.rb', line 247

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.



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/openhab/core/types/date_time_type.rb', line 126

def <=>(other)
  logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" }
  if other.is_a?(self.class)
    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:



148
149
150
151
152
153
# File 'lib/openhab/core/types/date_time_type.rb', line 148

def coerce(other)
  logger.trace { "Coercing #{self} as a request from #{other.class}" }
  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



110
111
112
113
114
# File 'lib/openhab/core/types/date_time_type.rb', line 110

def eql?(other)
  return false unless other.instance_of?(self.class)

  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



160
161
162
# File 'lib/openhab/core/types/date_time_type.rb', line 160

def to_f
  zoned_date_time.to_epoch_second + (zoned_date_time.nano / 1_000_000_000)
end

#to_instantInstant

Returns:



55
56
57
58
59
60
# File 'lib/openhab/core/types/date_time_type.rb', line 55

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

Parameters:

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

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

Returns:

  • (ZonedTimeTime)


50
51
52
# File 'lib/openhab/core/types/date_time_type.rb', line 50

def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
  zoned_date_time
end

#utc?true, false

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

Returns:

  • (true, false)

    true if utc_offset == 0, false otherwise



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

def utc?
  utc_offset.zero?
end

#utc_offsetInteger

The offset in seconds from UTC

Returns:

  • (Integer)

    The offset from UTC, in seconds



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

def utc_offset
  zoned_date_time.offset.total_seconds
end

#wdayInteger

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

Returns:



187
188
189
# File 'lib/openhab/core/types/date_time_type.rb', line 187

def wday
  zoned_date_time.day_of_week.value % 7
end

#zoneString

The timezone

Returns:

  • (String)

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



196
197
198
# File 'lib/openhab/core/types/date_time_type.rb', line 196

def zone
  zoned_date_time.zone.id
end