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:



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 82

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



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

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

Parses a String representing a time into a OpenHAB::Core::Types::DateTimeType. First tries to parse it using Java's parser, then falls back to the Ruby Time.parse.

Parameters:

  • time_string (String)

Returns:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/openhab/core/types/date_time_type.rb', line 43

def parse(time_string)
  time_string = "#{time_string}Z" if TIME_ONLY_REGEX.match?(time_string)
  DateTimeType.new(time_string)
rescue java.lang.StringIndexOutOfBoundsException, java.lang.IllegalArgumentException => e
  # Try Ruby's Time.parse if DateTimeType parser fails
  begin
    DateTimeType.new(::Time.parse(time_string).to_zoned_date_time)
  rescue ArgumentError
    raise ArgumentError, e.message
  end
end

Instance Method Details

#+(other) ⇒ DateTimeType

Add other to self

Parameters:

Returns:



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

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:



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

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.



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)
    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:



147
148
149
150
151
152
# 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, 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



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)

  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



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

def to_f
  zoned_date_time.to_epoch_second + (zoned_date_time.nano / 1_000_000_000)
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)


60
61
62
# File 'lib/openhab/core/types/date_time_type.rb', line 60

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



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

def utc?
  utc_offset.zero?
end

#utc_offsetInteger

The offset in seconds from UTC

Returns:

  • (Integer)

    The offset from UTC, in seconds



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

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:



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

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)



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

def zone
  zoned_date_time.zone.id
end