Class: OpenHAB::Core::Items::DateTimeItem

Inherits:
GenericItem
  • Object
show all
Defined in:
lib/openhab/core/items/date_time_item.rb

Overview

A DateTimeItem stores a timestamp including a valid time zone.

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.state + 600 # Number of seconds
Example_DateTimeItem.state - '01:15' # Subtracts 1h 15 min
Example_DateTimeItem.state + 2.hours # Use the helper library's duration methods

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

Comparisons between different time objects can be performed

Example_DateTimeItem.state == Example_DateTimeItem2.state # Equality, works across time zones
Example_DateTimeItem.state > Date.parse('2021-01-31') # After midnight jan 31st 2021
Example_DateTimeItem.state <= Time.now # Before or equal to now
Example_DateTimeItem.state < LocalTime::NOON # Before noon

LocalTime ranges created also work

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

Constant Summary

Constants included from Semantics

Semantics::Equipment, Semantics::Location, Semantics::Point, Semantics::Property, Semantics::Tag

Constants included from Persistence

Persistence::HistoricState

Instance Attribute Summary collapse

Attributes inherited from GenericItem

#category, #formatted_state, #label, #name, #raw_state, #tags

Attributes included from Semantics

#equipment, #equipment_type, #location, #location_type, #point_type, #property_type, #semantic_type

Attributes included from Item

#accepted_command_types, #accepted_data_types, #all_groups, #channel, #channel_uid, #channel_uids, #channels, #groups, #links, #metadata, #name, #provider, #thing, #things

Method Summary

Methods inherited from GenericItem

#command, #modify, #null?, #refresh, #state?, #time_series=, #undef?, #update

Methods included from Semantics

add, #equipment?, #location?, lookup, #point?, #points, remove, #semantic?, tags

Methods included from Item

#call_item?, #color_item?, #contact_item?, #date_time_item?, #dimmer_item?, #group_item?, #image_item?, #inspect, #link, #location_item?, #member_of?, #number_item?, #player_item?, #rollershutter_item?, #string_item?, #switch_item?, #tagged?, #to_s, #unlink

Methods included from DSL::Items::TimedCommand

#command

Methods included from Persistence

#average_between, #average_since, #average_until, #changed_between?, #changed_since?, #changed_until?, #count_between, #count_since, #count_state_changes_between, #count_state_changes_since, #count_state_changes_until, #count_until, #delta_between, #delta_since, #delta_until, #deviation_between, #deviation_since, #deviation_until, #evolution_rate, #evolution_rate_between, #evolution_rate_since, #evolution_rate_until, #historic_state, #last_change, #last_update, #maximum_between, #maximum_since, #maximum_until, #median_between, #median_since, #median_until, #minimum_between, #minimum_since, #minimum_until, #next_change, #next_state, #next_update, #persist, #persisted_state, #previous_state, #remove_all_states_between, #remove_all_states_since, #remove_all_states_until, #sum_between, #sum_since, #sum_until, #updated_between?, #updated_since?, #updated_until?, #variance_between, #variance_since, #variance_until

Methods included from DSL::Items::Ensure::Ensurable

#ensure

Instance Attribute Details

#stateDateTimeType? (readonly)

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/openhab/core/items/date_time_item.rb', line 44

class DateTimeItem < GenericItem
  # Time types need formatted as ISO8601
  # @!visibility private
  def format_type(command)
    return command if command.is_a?(Types::DateTimeType)
    return Types::DateTimeType.new(command.to_zoned_date_time) if command.respond_to?(:to_zoned_date_time)

    if command.respond_to?(:to_str)
      command = command.to_str
      begin
        return Types::DateTimeType.new(DSL.try_parse_time_like(command))
      rescue ArgumentError
        return Types::DateTimeType.new(command)
      end
    end

    super
  end
end