Method: DateTime#minus_with_temporal

Defined in:
lib/openhab/core_ext/ruby/date_time.rb

#minus_with_temporal(other) ⇒ ZonedDateTime, ... Also known as: -

Extends #- to allow subtracting a TemporalAmount or any other date/time class that responds to #to_zoned_date_time.

Subtractions with another object of the same class (e.g. Time - Other Time, or DateTime - Other DateTime) remains unchanged from its original behavior.

Examples:

Time - Duration -> ZonedDateTime

zdt_one_hour_ago = Time.now - 1.hour

Time - ZonedDateTime -> Duration

java_duration = Time.now - 1.hour.ago

Time - Numeric -> Time

time_one_hour_ago = Time - 3600

Time - Time -> Float

one_day_in_secs = Time.new(2002, 10, 31) - Time.new(2002, 10, 30)

Parameters:

Returns:

  • (ZonedDateTime)

    If other is a TemporalAmount

  • (Duration)

    If other responds to #to_zoned_date_time

  • (Time)

    If other is a Numeric

  • (Float)

    If other is a Time



26
27
28
29
30
31
32
33
34
35
# File 'lib/openhab/core_ext/ruby/date_time.rb', line 26

def minus_with_temporal(other)
  return to_zoned_date_time - other.to_temporal_amount if other.respond_to?(:to_temporal_amount)

  # Exclude subtracting against the same class
  if other.respond_to?(:to_zoned_date_time) && !other.is_a?(self.class)
    return to_zoned_date_time - other.to_zoned_date_time
  end

  minus_without_temporal(other)
end