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

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

Overview

Extensions to javajava.timejava.time.ZonedDateTime

Class Attribute Summary collapse

Ephemeris Methods (see CoreExt::Ephemeris) collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Between

#between?

Class Attribute Details

.nowZonedDateTime (readonly)

Returns:



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

Class Method Details

.parse(text, formatter = nil) ⇒ ZonedDateTime

Parses a string into a ZonedDateTime object.

Parameters:

  • text (String)

    The text to parse.

  • formatter (java.time.format.DateTimeFormatter) (defaults to: nil)

    The formatter to use.

Returns:



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

Instance Method Details

#+(other) ⇒ ZonedDateTime

Parameters:

Returns:



56
57
58
59
60
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 56

def +(other)
  return plus(other.seconds) if other.is_a?(Numeric)

  plus(other)
end

#-(other) ⇒ Duration, ZonedDateTime

Parameters:

Returns:

  • (Duration)

    If other responds to #to_zoned_date_time

  • (ZonedDateTime)

    If other is a TemporalAmount



43
44
45
46
47
48
49
50
51
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 43

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?

Returns:



189
190
191
192
193
194
195
196
197
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 189

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:



205
206
207
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 205

def coerce(other)
  [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



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

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:



118
119
120
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 118

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)


128
129
130
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 128

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)


163
164
165
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 163

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:



138
139
140
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 138

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:



79
80
81
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 79

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

#to_fFloat

The number of seconds since the Unix epoch.

Returns:



74
75
76
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 74

def to_f
  to_instant.to_epoch_milli / 1000.0
end

#to_iInteger

The number of seconds since the Unix epoch.

Returns:



66
67
68
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 66

def to_i
  to_instant.epoch_second
end

#to_local_date(_context = nil) ⇒ LocalDate

Returns:



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

def to_local_date(_context = nil)
  toLocalDate
end

#to_local_time(_context = nil) ⇒ LocalTime

Returns:



32
33
34
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 32

def to_local_time(_context = nil)
  toLocalTime
end

#to_month_dayMonthDay

Returns:



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

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 95

#to_zoned_date_time(context = nil) ⇒ self

Parameters:

Returns:

  • (self)


102
103
104
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 102

def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
  self
end

#weekend?true, false

Determines if this time is during a weekend.

Examples:

Time.now.weekend?

Returns:

  • (true, false)


150
151
152
# File 'lib/openhab/core_ext/java/zoned_date_time.rb', line 150

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