Class: Date

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

Overview

Extensions to Date

Direct Known Subclasses

DateTime

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?

Convert ‘other` to Date, if possible.

Parameters:

  • other (#to_date)

Returns:



86
87
88
89
90
91
# File 'lib/openhab/core_ext/ruby/date.rb', line 86

def coerce(other)
  return nil unless other.respond_to?(:to_date)
  return [other.to_date, self] if other.method(:to_date).arity.zero?

  [other.to_date(self), self]
end

#compare_with_coercion(other) ⇒ Integer? Also known as: <=>

Returns:



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/openhab/core_ext/ruby/date.rb', line 66

def compare_with_coercion(other)
  return compare_without_coercion(other) if other.is_a?(self.class)

  return self <=> other.to_date(self) if other.is_a?(java.time.MonthDay)

  if other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self))
    return lhs <=> rhs
  end

  compare_without_coercion(other)
end

#minus_with_temporal(other) ⇒ LocalDate Also known as: -

Extends #- to allow subtracting a TemporalAmount

Parameters:

  • other (java.time.temporal.TemporalAmount)

Returns:

  • (LocalDate)

    If other is a TemporalAmount



30
31
32
33
34
35
36
37
# File 'lib/openhab/core_ext/ruby/date.rb', line 30

def minus_with_temporal(other)
  case other
  when java.time.temporal.TemporalAmount, java.time.LocalDate
    to_local_date - other
  else
    minus_without_temporal(other)
  end
end

#plus_with_temporal(other) ⇒ LocalDate Also known as: +

Extends #+ to allow adding a TemporalAmount

Parameters:

  • other (java.time.temporal.TemporalAmount)

Returns:

  • (LocalDate)

    If other is a TemporalAmount



16
17
18
19
20
# File 'lib/openhab/core_ext/ruby/date.rb', line 16

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

  plus_without_temporal(other)
end

#to_local_date(_context = nil) ⇒ LocalDate

Returns:



42
43
44
# File 'lib/openhab/core_ext/ruby/date.rb', line 42

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

#to_monthMonth

Returns:



47
48
49
# File 'lib/openhab/core_ext/ruby/date.rb', line 47

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

#to_month_dayMonthDay

Returns:



52
53
54
# File 'lib/openhab/core_ext/ruby/date.rb', line 52

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

#to_zoned_date_time(context = nil) ⇒ ZonedDateTime

Parameters:

Returns:



61
62
63
# File 'lib/openhab/core_ext/ruby/date.rb', line 61

def to_zoned_date_time(context = nil)
  to_local_date.to_zoned_date_time(context)
end