Skip to main content Link Search Menu Expand Document (external link)

Working With Time

Several options are available for time related code, including but not limited to:

Ruby Time

The following methods are modified/added to Ruby Time:

MethodDescription
+Add a Numeric (seconds) or Duration and return the result as a Time object
-Subtract a Numeric (seconds) or Duration and return the result as a Time object
to_zdtReturn a ZonedDateTime equivalent of the time object

Time Comparisons and Arithmetic

Comparisons and arithmetic can be done using the corresponding operators between Java and Ruby objects.

Examples

# Comparing localtime against TimeOfDay with `<`
max_time = Solar_Power.maximum_since(24.hours).timestamp.to_local_time
if max_time < NOON
  logger.info 'Max solar power happened before noon'
end

# Comparing Time against ZonedDateTime with `>`
sunset = things['astro:sun:home'].getEventTime('SUN_SET', nil, nil)
if Time.now > sunset 
  logger.info 'it is after sunset'
end

# Subtracting Duration from Time and comparing Time against ZonedDateTime
Motion_Sensor.last_update < Time.now - 10.minutes

# Using `-` operator with ZonedDateTime
# Comparing two ZonedDateTime using `<` 
Motion_Sensor.last_update < Light_Item.last_update - 10.minutes
# is the same as:
Motion_Sensor.last_update.before?(Light_Item.last_update.minus_minutes(10))