Module: OpenHAB::DSL

Defined in:
lib/rspec/openhab/dsl/imports.rb,
lib/rspec/openhab/dsl/rules/rspec.rb,
lib/rspec/openhab/dsl/timers/timer.rb,
lib/rspec/openhab/core/script_handling.rb

Defined Under Namespace

Modules: Core, Imports, Rules Classes: Timer

Class Method Summary collapse

Class Method Details

.seconds_to_duration(secs) ⇒ Duration

Convert numeric seconds to a Duration object

Parameters:

  • secs (Float, Integer)

    The number of seconds in integer or float

Returns:

  • (Duration)


77
78
79
80
81
82
83
84
85
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 77

def self.seconds_to_duration(secs)
  return unless secs

  if secs.respond_to?(:to_f)
    secs.to_f.seconds
  elsif secs.respond_to?(:to_i)
    secs.to_i.seconds
  end
end

.to_zdt(timestamp) ⇒ ZonedDateTime

Convert TemporalAmount (Duration), seconds (float, integer), and Ruby Time to ZonedDateTime Note: TemporalAmount is added to now

Parameters:

  • timestamp (Object)

    to convert

Returns:

  • (ZonedDateTime)


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 56

def self.to_zdt(timestamp)
  logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
  return unless timestamp

  case timestamp
  when Java::JavaTimeTemporal::TemporalAmount then ZonedDateTime.now.plus(timestamp)
  when ZonedDateTime then timestamp
  when Time then timestamp.to_java(ZonedDateTime)
  else
    to_zdt(seconds_to_duration(timestamp)) ||
      raise(ArgumentError, "Timestamp must be a ZonedDateTime, a Duration, a Numeric, or a Time object")
  end
end