Skip to content

Commit

Permalink
Avoid calling the deprecated DateTimeType.getZonedDateTime()
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng committed Dec 16, 2024
1 parent e6736a5 commit d051346
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/openhab/core/types/date_time_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ def parse(time_string)
# fields during conversion. Not used in this class.
# @return [ZonedTimeTime]
def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
zoned_date_time
# @deprecated OH 4.2 Just call zoned_date_time(ZoneId.system_default) in OH 4.3
unless instance_variable_defined?(:@zoned_date_time_method)
begin
# The method with a ZoneId argument was added in OH 4.3
@zoned_date_time_method = java_method :getZonedDateTime, [java.time.ZoneId]
rescue NameError
@zoned_date_time_method = nil
end
end

@zoned_date_time_method&.call(ZoneId.system_default) || zoned_date_time
end

# @!visibility private
Expand Down Expand Up @@ -110,6 +120,9 @@ def initialize(value = nil)
def eql?(other)
return false unless other.instance_of?(self.class)

# @deprecated OH 4.2 Call compare_to(other).zero? in OH 4.3 to avoid the deprecated getZonedDateTime()
return compare_to(other).zero? if respond_to?(:compare_to)

zoned_date_time.compare_to(other.zoned_date_time).zero?
end

Expand All @@ -126,6 +139,9 @@ def eql?(other)
def <=>(other)
logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" }
if other.is_a?(self.class)
# @deprecated OH 4.2 Call compare_to(other) in OH 4.3 to avoid the deprecated getZonedDateTime()
return compare_to(other) if respond_to?(:compare_to)

zoned_date_time <=> other.zoned_date_time
elsif other.respond_to?(:to_time)
to_time <=> other.to_time
Expand Down
2 changes: 1 addition & 1 deletion spec/openhab/core/types/date_time_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

describe "#initialize" do
it "initializes to `now` if no argument is given" do
expect(DateTimeType.new.zoned_date_time.to_epoch_second).to be_within(1).of(ZonedDateTime.now.to_epoch_second)
expect(DateTimeType.new.to_zoned_date_time.to_epoch_second).to be_within(1).of(ZonedDateTime.now.to_epoch_second)
end
end

Expand Down

0 comments on commit d051346

Please sign in to comment.