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:

+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.


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

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

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

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