Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/openhab/core_ext/ruby/time.rb
Overview
Extensions to Time
Instance Method Summary collapse
-
#coerce(other) ⇒ Array?
Converts to a ZonedDateTime if
otheris also convertible to a ZonedDateTime. -
#minus_with_temporal(other) ⇒ ZonedDateTime, ...
(also: #-)
Extends #- to allow subtracting a TemporalAmount or any other date/time class that responds to #to_zoned_date_time.
-
#plus_with_temporal(other) ⇒ ZonedDateTime, Time
(also: #+)
Extends #+ to allow adding a TemporalAmount.
- #to_instant(_context = nil) ⇒ Instant
- #to_local_date(_context = nil) ⇒ LocalDate
- #to_local_time ⇒ LocalTime
- #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::TimePredicates
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?
Converts to a ZonedDateTime if other
is also convertible to a ZonedDateTime.
118 119 120 121 122 123 124 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 118 def coerce(other) logger.trace { "Coercing #{self} as a request from #{other.class}" } 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.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 58 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 |
#plus_with_temporal(other) ⇒ ZonedDateTime, Time Also known as: +
Extends #+ to allow adding a TemporalAmount
23 24 25 26 27 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 23 def plus_with_temporal(other) return to_zoned_date_time + other.to_temporal_amount if other.respond_to?(:to_temporal_amount) plus_without_temporal(other) end |
#to_instant(_context = nil) ⇒ Instant
107 108 109 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 107 def to_instant(_context = nil) to_java(java.time.Instant) end |
#to_local_date(_context = nil) ⇒ LocalDate
72 73 74 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 72 def to_local_date(_context = nil) java.time.LocalDate.of(year, month, day) end |
#to_local_time ⇒ LocalTime
78 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 78 def_delegator :to_zoned_date_time, :to_local_time |
#to_month ⇒ Month
81 82 83 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 81 def to_month java.time.Month.of(month) end |
#to_month_day ⇒ MonthDay
86 87 88 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 86 def to_month_day java.time.MonthDay.of(month, day) end |
#to_zoned_date_time(context = nil) ⇒ ZonedDateTime
102 103 104 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 102 def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument to_java(java.time.ZonedDateTime) 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.
96 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 96 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.
96 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 96 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.
96 |
# File 'lib/openhab/core_ext/ruby/time.rb', line 96 def_delegators :to_zoned_date_time, :yesterday?, :today?, :tomorrow? |