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:



106
107
108
109
110
111
112
# File 'lib/openhab/core_ext/ruby/date.rb', line 106

def coerce(other)
  logger.trace { "Coercing #{self} as a request from #{other.class}" }
  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:



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/openhab/core_ext/ruby/date.rb', line 86

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

#inspectString

Returns:

  • (String)


116
# File 'lib/openhab/core_ext/ruby/date.rb', line 116

alias_method :inspect, :to_s

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

Extends #- to allow subtracting a TemporalAmount

Parameters:

Returns:



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

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:

Returns:



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

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

  plus_without_temporal(other)
end

#to_instant(context = nil) ⇒ Instant

Parameters:

Returns:



80
81
82
83
# File 'lib/openhab/core_ext/ruby/date.rb', line 80

def to_instant(context = nil)
  context ||= Instant.now.to_zoned_date_time
  to_zoned_date_time(context).to_instant
end

#to_local_date(_context = nil) ⇒ LocalDate

Returns:



44
45
46
# File 'lib/openhab/core_ext/ruby/date.rb', line 44

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

#to_monthMonth

Returns:



49
50
51
# File 'lib/openhab/core_ext/ruby/date.rb', line 49

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

#to_month_dayMonthDay

Returns:



54
55
56
# File 'lib/openhab/core_ext/ruby/date.rb', line 54

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

#to_zoned_date_time(context = nil) ⇒ ZonedDateTime

Parameters:

Returns:



71
72
73
# File 'lib/openhab/core_ext/ruby/date.rb', line 71

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

#today?true, false

Returns true if the date, converted to the system time zone, is today.

This is the equivalent of checking if the current datetime is between midnight and end of the day of the system time zone.

Returns:

  • (true, false)


64
# File 'lib/openhab/core_ext/ruby/date.rb', line 64

def_delegators :to_zoned_date_time, :yesterday?, :today?, :tomorrow?

#tomorrow?true, false

Returns true if the date, converted to the system time zone, is tomorrow.

Returns:

  • (true, false)


64
# File 'lib/openhab/core_ext/ruby/date.rb', line 64

def_delegators :to_zoned_date_time, :yesterday?, :today?, :tomorrow?

#yesterday?true, false

Returns true if the date, converted to the system time zone, is yesterday.

Returns:

  • (true, false)


64
# File 'lib/openhab/core_ext/ruby/date.rb', line 64

def_delegators :to_zoned_date_time, :yesterday?, :today?, :tomorrow?