Class: Date
- Inherits:
-
Object
- Object
- Date
- Defined in:
- lib/openhab/core_ext/ruby/date.rb
Overview
Extensions to Date
Direct Known Subclasses
Instance Method Summary collapse
-
#coerce(other) ⇒ Array?
Convert
other
to Date, if possible. - #compare_with_coercion(other) ⇒ Integer? (also: #<=>)
- #inspect ⇒ String
-
#minus_with_temporal(other) ⇒ LocalDate
(also: #-)
Extends #- to allow subtracting a TemporalAmount.
-
#plus_with_temporal(other) ⇒ LocalDate
(also: #+)
Extends #+ to allow adding a TemporalAmount.
- #to_instant(context = nil) ⇒ Instant
- #to_local_date(_context = nil) ⇒ LocalDate
- #to_month ⇒ Month
- #to_month_day ⇒ MonthDay
- #to_zoned_date_time(context = nil) ⇒ ZonedDateTime
-
#today? ⇒ true, false
Returns true if the date, converted to the system time zone, is today.
-
#tomorrow? ⇒ true, false
Returns true if the date, converted to the system time zone, is tomorrow.
-
#yesterday? ⇒ true, false
Returns true if the date, converted to the system time zone, is yesterday.
Methods included from OpenHAB::CoreExt::Ephemeris
#days_until, #holiday, #holiday?, #in_dayset?, #next_holiday, #weekend?
Methods included from OpenHAB::CoreExt::Between
Instance Method Details
#coerce(other) ⇒ Array?
Convert other
to Date, if possible.
107 108 109 110 111 112 113 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 107 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: <=>
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 87 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 |
#inspect ⇒ String
117 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 117 alias_method :inspect, :to_s |
#minus_with_temporal(other) ⇒ LocalDate Also known as: -
Extends #- to allow subtracting a TemporalAmount
32 33 34 35 36 37 38 39 40 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 32 def minus_with_temporal(other) if other.instance_of?(java.time.LocalDate) to_local_date - other elsif other.respond_to?(:to_temporal_amount) to_local_date - other.to_temporal_amount else minus_without_temporal(other) end end |
#plus_with_temporal(other) ⇒ LocalDate Also known as: +
Extends #+ to allow adding a TemporalAmount
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.to_temporal_amount if other.respond_to?(:to_temporal_amount) plus_without_temporal(other) end |
#to_instant(context = nil) ⇒ Instant
81 82 83 84 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 81 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
45 46 47 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 45 def to_local_date(_context = nil) java.time.LocalDate.of(year, month, day) end |
#to_month ⇒ Month
50 51 52 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 50 def to_month java.time.Month.of(month) end |
#to_month_day ⇒ MonthDay
55 56 57 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 55 def to_month_day java.time.MonthDay.of(month, day) end |
#to_zoned_date_time(context = nil) ⇒ ZonedDateTime
72 73 74 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 72 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.
65 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 65 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.
65 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 65 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.
65 |
# File 'lib/openhab/core_ext/ruby/date.rb', line 65 def_delegators :to_zoned_date_time, :yesterday?, :today?, :tomorrow? |