Class: Time

Inherits:
Object
  • Object
show all
Includes:
OpenHAB::CoreExt::Between, OpenHAB::CoreExt::Ephemeris
Defined in:
lib/openhab/core_ext/ruby/time.rb

Overview

Extensions to Time

Instance Method Summary collapse

Methods included from OpenHAB::CoreExt::Ephemeris

#days_until, #holiday, #holiday?, #in_dayset?, #next_holiday, #weekend?

Methods included from OpenHAB::CoreExt::Between

#between?

Instance Method Details

#coerce(other) ⇒ Array?

Converts to a ZonedDateTime if ‘other` is also convertible to a ZonedDateTime.

Parameters:

Returns:



102
103
104
105
106
107
# File 'lib/openhab/core_ext/ruby/time.rb', line 102

def coerce(other)
  return unless other.respond_to?(:to_zoned_date_time)

  zdt = to_zoned_date_time
  [other.to_zoned_date_time(zdt), zdt]
end

#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



55
56
57
58
59
60
61
62
63
64
# File 'lib/openhab/core_ext/ruby/time.rb', line 55

def minus_with_temporal(other)
  return to_zoned_date_time - other if other.is_a?(java.time.temporal.TemporalAmount)

  # 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

#plus_with_temporal(other) ⇒ ZonedDateTime, Time Also known as: +

Extends #+ to allow adding a TemporalAmount

Parameters:

  • other (java.time.temporal.TemporalAmount)

Returns:



20
21
22
23
24
# File 'lib/openhab/core_ext/ruby/time.rb', line 20

def plus_with_temporal(other)
  return to_zoned_date_time + other if other.is_a?(java.time.temporal.TemporalAmount)

  plus_without_temporal(other)
end

#to_local_date(_context = nil) ⇒ LocalDate

Returns:



69
70
71
# File 'lib/openhab/core_ext/ruby/time.rb', line 69

def to_local_date(_context = nil)
  java.time.LocalDate.of(year, month, day)
end

#to_local_timeLocalTime

Returns:



75
# File 'lib/openhab/core_ext/ruby/time.rb', line 75

def_delegator :to_zoned_date_time, :to_local_time

#to_monthMonth

Returns:



78
79
80
# File 'lib/openhab/core_ext/ruby/time.rb', line 78

def to_month
  java.time.Month.of(month)
end

#to_month_dayMonthDay

Returns:



83
84
85
# File 'lib/openhab/core_ext/ruby/time.rb', line 83

def to_month_day
  java.time.MonthDay.of(month, day)
end

#to_zoned_date_time(context = nil) ⇒ ZonedDateTime

Parameters:

  • context (ZonedDateTime, nil) (defaults to: nil)

    A ZonedDateTime used to fill in missing fields during conversion. Not used in this class.

Returns:



91
92
93
# File 'lib/openhab/core_ext/ruby/time.rb', line 91

def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
  to_java(java.time.ZonedDateTime)
end