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

DateTimeItem

DateTime items are extended with a lot of methods that make working with them easy. Most of the methods defined by the ruby Time class are available, and some of them are extended or adapted with OpenHAB specific functionality.

Examples

DateTime items can be updated and commanded with ruby Time objects or Java ZonedDateTime objects

Example_DateTimeItem << Time.now
Example_DateTimeItem << ZonedDateTime.now

Math operations (+ and -) are available to make calculations with time in a few different ways

Example_DateTimeItem + 600 # Number of seconds
Example_DateTimeItem - '01:15' # Subtracts 1h 15 min
Example_DateTimeItem + 2.hours # Use the helper library's duration methods

Example_DateTimeItem - Example_DateTimeItem2 # Calculate the time difference, in seconds
Example_DateTimeItem - '2021-01-01 15:40' # Calculates time difference

Comparisons between different time objects can be performed

Example_DateTimeItem == Example_DateTimeItem2 # Equality, works across time zones
Example_DateTimeItem > '2021-01-31' # After midnight jan 31st 2021
Example_DateTimeItem <= Time.now # Before or equal to now
Example_DateTimeItem < TimeOfDay.noon # Before noon

TimeOfDay ranges created with the between method also works

case Example_DateTimeItem
when between('00:00'...'08:00')
  logger.info('Example_DateTimeItem is between 00:00..08:00')
when between('08:00'...'16:00')
  logger.info('Example_DateTimeItem is between 08:00..16:00')
when between('16:00'..'23:59')
  logger.info('Example_DateTimeItem is between 16:00...23:59')
end