Class: OpenHAB::CoreExt::Java::ZonedDateTime
- Inherits:
-
Object
- Object
- OpenHAB::CoreExt::Java::ZonedDateTime
- Defined in:
- lib/openhab/core_ext/java/zoned_date_time.rb
Overview
Extensions to java.time.ZonedDateTime
Class Attribute Summary collapse
- .now ⇒ ZonedDateTime readonly
Ephemeris Methods collapse
openHAB's built-in holiday definitions are based on bank holidays, so may give some unexpected results. For example, 2022-12-25 is not Christmas in England because it lands on a Sunday that year, so Christmas is considered to be 2022-12-26. See the source for exact definitions. You can always provide your own holiday definitions with holiday_file or holiday_file!.
Forwards ephemeris helper methods to #to_zoned_date_time
provided by
the mixed-in class.
-
#days_until(holiday, holiday_file = nil) ⇒ Integer
Calculate the number of days until a specific holiday.
-
#holiday(holiday_file = nil) ⇒ Symbol?
Name of the holiday for this date.
-
#holiday?(holiday_file = nil) ⇒ true, false
Determines if this date is on a holiday.
-
#in_dayset?(set) ⇒ true, false
Determines if this time is during a specific dayset.
-
#next_holiday(holiday_file = nil) ⇒ Symbol
Name of the closest holiday on or after this date.
-
#weekend? ⇒ true, false
Determines if this time is during a weekend.
Class Method Summary collapse
-
.parse(text, formatter = nil) ⇒ ZonedDateTime
Parses a string into a ZonedDateTime object.
Instance Method Summary collapse
- #+(other) ⇒ ZonedDateTime
- #-(other) ⇒ Duration, ZonedDateTime
- #<=>(other) ⇒ Integer?
-
#coerce(other) ⇒ Array?
Converts
other
to ZonedDateTime, if possible. - #to_date ⇒ Date
-
#to_f ⇒ Float
The number of seconds since the Unix epoch.
-
#to_i ⇒ Integer
The number of seconds since the Unix epoch.
-
#to_instant ⇒ Instant
Converts this object to an Instant.
- #to_local_date(_context = nil) ⇒ LocalDate
- #to_local_time(_context = nil) ⇒ LocalTime
- #to_month ⇒ Month
- #to_month_day ⇒ MonthDay
- #to_time ⇒ Time
- #to_zoned_date_time(context = nil) ⇒ self
-
#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 Between
Class Attribute Details
Class Method Details
.parse(text, formatter = nil) ⇒ ZonedDateTime
Parses a string into a ZonedDateTime object.
|
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 24
|
Instance Method Details
#+(other) ⇒ ZonedDateTime
59 60 61 62 63 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 59 def +(other) return plus(other.seconds) if other.is_a?(Numeric) plus(other) end |
#-(other) ⇒ Duration, ZonedDateTime
46 47 48 49 50 51 52 53 54 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 46 def -(other) if other.respond_to?(:to_zoned_date_time) java.time.Duration.between(other.to_zoned_date_time, self) elsif other.is_a?(Numeric) minus(other.seconds) else minus(other) end end |
#<=>(other) ⇒ Integer?
222 223 224 225 226 227 228 229 230 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 222 def <=>(other) # compare instants, otherwise it will differ by timezone, which we don't want # (use eql? if you care about that) if other.respond_to?(:to_zoned_date_time) to_instant.compare_to(other.to_zoned_date_time(self).to_instant) elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self)) lhs <=> rhs end end |
#coerce(other) ⇒ Array?
Converts other
to OpenHAB::CoreExt::Java::ZonedDateTime, if possible
252 253 254 255 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 252 def coerce(other) logger.trace { "Coercing #{self} as a request from #{other.class}" } [other.to_zoned_date_time(self), self] if other.respond_to?(:to_zoned_date_time) end |
#days_until(holiday, holiday_file = nil) ⇒ Integer
Calculate the number of days until a specific holiday
211 212 213 214 215 216 217 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 211 def days_until(holiday, holiday_file = nil) holiday = holiday.to_s.upcase r = ::Ephemeris.get_days_until(*[self, holiday, holiday_file || DSL.holiday_file].compact) raise ArgumentError, "#{holiday.inspect} isn't a recognized holiday" if r == -1 r end |
#holiday(holiday_file = nil) ⇒ Symbol?
Name of the holiday for this date.
151 152 153 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 151 def holiday(holiday_file = nil) ::Ephemeris.get_bank_holiday_name(*[self, holiday_file || DSL.holiday_file].compact)&.downcase&.to_sym end |
#holiday?(holiday_file = nil) ⇒ true, false
Determines if this date is on a holiday.
161 162 163 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 161 def holiday?(holiday_file = nil) ::Ephemeris.bank_holiday?(*[self, holiday_file || DSL.holiday_file].compact) end |
#in_dayset?(set) ⇒ true, false
Determines if this time is during a specific dayset
196 197 198 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 196 def in_dayset?(set) ::Ephemeris.in_dayset?(set.to_s, self) end |
#next_holiday(holiday_file = nil) ⇒ Symbol
Name of the closest holiday on or after this date.
171 172 173 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 171 def next_holiday(holiday_file = nil) ::Ephemeris.get_next_bank_holiday(*[self, holiday_file || DSL.holiday_file].compact).downcase.to_sym end |
#to_date ⇒ Date
82 83 84 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 82 def to_date Date.new(year, month_value, day_of_month) end |
#to_f ⇒ Float
The number of seconds since the Unix epoch.
79 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 79 delegate %i[to_i to_f] => :to_instant |
#to_i ⇒ Integer
The number of seconds since the Unix epoch.
|
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 65
|
#to_instant ⇒ Instant
Converts this object to an Instant
236 237 238 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 236 def to_instant(_context = nil) raw_to_instant end |
#to_local_date(_context = nil) ⇒ LocalDate
87 88 89 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 87 def to_local_date(_context = nil) toLocalDate end |
#to_local_time(_context = nil) ⇒ LocalTime
35 36 37 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 35 def to_local_time(_context = nil) toLocalTime end |
#to_month ⇒ Month
40 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 40 alias_method :to_month, :month |
#to_month_day ⇒ MonthDay
92 93 94 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 92 def to_month_day MonthDay.of(month, day_of_month) end |
#to_zoned_date_time(context = nil) ⇒ self
105 106 107 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 105 def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument self 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.
126 127 128 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 126 def today? with_zone_same_instant(ZoneId.system_default).to_local_date == LocalDate.now end |
#tomorrow? ⇒ true, false
Returns true if the date, converted to the system time zone, is tomorrow.
135 136 137 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 135 def tomorrow? with_zone_same_instant(ZoneId.system_default).to_local_date == LocalDate.now + 1 end |
#weekend? ⇒ true, false
Determines if this time is during a weekend.
183 184 185 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 183 def weekend? ::Ephemeris.weekend?(self) end |
#yesterday? ⇒ true, false
Returns true if the date, converted to the system time zone, is yesterday.
114 115 116 |
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 114 def yesterday? with_zone_same_instant(ZoneId.system_default).to_local_date == LocalDate.now - 1 end |