Class: OpenHAB::CoreExt::Java::ZonedDateTime

Inherits:
Object
  • Object
show all
Includes:
Between, Time, TimePredicates
Defined in:
lib/openhab/core_ext/java/zoned_date_time.rb

Overview

Extensions to java.time.ZonedDateTime

Class Attribute Summary collapse

Ephemeris Methods collapse

Note:

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.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Between

#between?

Methods included from TimePredicates

#within?

Class Attribute Details

.nowZonedDateTime (readonly)

Returns:



# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 22

Class Method Details

.parse(text, formatter = nil) ⇒ ZonedDateTime

Parses a string into a ZonedDateTime object.

Parameters:

Returns:



# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 25

Instance Method Details

#+(other) ⇒ ZonedDateTime

Parameters:

Returns:



62
63
64
65
66
67
68
69
70
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 62

def +(other)
  if other.is_a?(Numeric)
    plus(other.seconds)
  elsif other.is_a?(QuantityType)
    plus(other.to_temporal_amount)
  else
    plus(other)
  end
end

#-(other) ⇒ Duration, ZonedDateTime

Parameters:

Returns:

  • (Duration)

    If other responds to #to_zoned_date_time

  • (ZonedDateTime)

    If other is a TemporalAmount or a Time QuantityType



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 47

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)
  elsif other.is_a?(QuantityType)
    minus(other.to_temporal_amount)
  else
    minus(other)
  end
end

#<=>(other) ⇒ Integer?

Returns:



229
230
231
232
233
234
235
236
237
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 229

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

Parameters:

Returns:



259
260
261
262
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 259

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

Examples:

Time.now.days_until(:christmas) # => 2

Parameters:

  • holiday (String, Symbol)
  • holiday_file (String, nil) (defaults to: nil)

    Optional path to XML file to use for holiday definitions.

Returns:

Raises:

  • (ArgumentError)

    if the holiday isn't valid



218
219
220
221
222
223
224
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 218

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.

Examples:

MonthDay.parse("12-25").holiday # => :christmas

Parameters:

  • holiday_file (String, nil) (defaults to: nil)

    Optional path to XML file to use for holiday definitions.

Returns:



158
159
160
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 158

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.

Parameters:

  • holiday_file (String, nil) (defaults to: nil)

    Optional path to XML file to use for holiday definitions.

Returns:

  • (true, false)


168
169
170
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 168

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

Examples:

Time.now.in_dayset?("school")

Parameters:

Returns:

  • (true, false)


203
204
205
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 203

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.

Parameters:

  • holiday_file (String, nil) (defaults to: nil)

    Optional path to XML file to use for holiday definitions.

Returns:



178
179
180
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 178

def next_holiday(holiday_file = nil)
  ::Ephemeris.get_next_bank_holiday(*[self, holiday_file || DSL.holiday_file].compact).downcase.to_sym
end

#to_dateDate

Returns:



89
90
91
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 89

def to_date
  Date.new(year, month_value, day_of_month)
end

#to_fFloat

The number of seconds since the Unix epoch.

Returns:



86
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 86

delegate %i[to_i to_f] => :to_instant

#to_iInteger

The number of seconds since the Unix epoch.

Returns:



# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 72

#to_instantInstant

Converts this object to an Instant

Returns:



243
244
245
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 243

def to_instant(_context = nil)
  raw_to_instant
end

#to_local_date(_context = nil) ⇒ LocalDate

Returns:



94
95
96
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 94

def to_local_date(_context = nil)
  toLocalDate
end

#to_local_time(_context = nil) ⇒ LocalTime

Returns:



36
37
38
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 36

def to_local_time(_context = nil)
  toLocalTime
end

#to_monthMonth

Returns:



41
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 41

alias_method :to_month, :month

#to_month_dayMonthDay

Returns:



99
100
101
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 99

def to_month_day
  MonthDay.of(month, day_of_month)
end

#to_timeTime

Returns:



# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 105

#to_zoned_date_time(context = nil) ⇒ self

Parameters:

Returns:

  • (self)


112
113
114
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 112

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.

Returns:

  • (true, false)


133
134
135
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 133

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.

Returns:

  • (true, false)


142
143
144
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 142

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.

Examples:

Time.now.weekend?

Returns:

  • (true, false)


190
191
192
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 190

def weekend?
  ::Ephemeris.weekend?(self)
end

#yesterday?true, false

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

Returns:

  • (true, false)


121
122
123
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 121

def yesterday?
  with_zone_same_instant(ZoneId.system_default).to_local_date == LocalDate.now - 1
end