Module: OpenHAB::CoreExt::TimePredicates

Included in:
Date, DateTime, Java::Instant, Java::LocalDate, Java::LocalTime, Java::Month, Java::MonthDay, Java::ZonedDateTime, Time
Defined in:
lib/openhab/core_ext/time_predicates.rb

Overview

Predicate helpers for date/time-like objects.

Instance Method Summary collapse

Instance Method Details

#within?(epsilon, of: ZonedDateTime.now) ⇒ true, false

Checks whether the object is within a maximum allowable distance (epsilon) of a specific anchor time.

Examples:

Check if a timestamp is within 5 minutes of right now

event_time.within?(5.minutes)

Check if a timestamp is within 30 seconds of an explicit execution deadline

log_time.within?(30.seconds, of: deadline)

Parameters:

Returns:

  • (true, false)

    true if the absolute difference between self and the anchor is less than epsilon



21
22
23
24
25
# File 'lib/openhab/core_ext/time_predicates.rb', line 21

def within?(epsilon, of: ZonedDateTime.now)
  # Convert times to a common float representation (like epoch seconds) to do the math
  # Or use native Java duration differences if keeping it in the Java ecosystem
  (of - self).to_f.abs < epsilon.to_f
end