Class: DateTime
- Defined in:
- lib/openhab/core_ext/ruby/date_time.rb
Overview
Extensions to DateTime
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_time ⇒ LocalTime
- #to_zoned_date_time(context = nil) ⇒ ZonedDateTime
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
Methods inherited from Date
#compare_with_coercion, #inspect, #to_local_date, #to_month, #to_month_day, #today?, #tomorrow?, #yesterday?
Instance Method Details
#coerce(other) ⇒ Array?
Converts to a ZonedDateTime if other
is also convertible to a ZonedDateTime.
57 58 59 60 61 62 63 |
# File 'lib/openhab/core_ext/ruby/date_time.rb', line 57 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.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/openhab/core_ext/ruby/date_time.rb', line 29 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
20 21 22 23 24 |
# File 'lib/openhab/core_ext/ruby/date_time.rb', line 20 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
52 53 54 |
# File 'lib/openhab/core_ext/ruby/date_time.rb', line 52 def to_instant(_context = nil) to_java(Instant) end |
#to_local_time ⇒ LocalTime
44 |
# File 'lib/openhab/core_ext/ruby/date_time.rb', line 44 def_delegator :to_zoned_date_time, :to_local_time |
#to_zoned_date_time(context = nil) ⇒ ZonedDateTime
47 48 49 |
# File 'lib/openhab/core_ext/ruby/date_time.rb', line 47 def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument to_java(ZonedDateTime) end |