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 7fcb94e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions lib/openhab/core/types/date_time_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,38 @@ def parse(time_string)
rescue ArgumentError
raise ArgumentError, e.message
end

begin
@zoned_date_time_method = java_method :getZonedDateTime, [java.time.ZoneId]
rescue NameError
@zoned_date_time_method = nil
end
end

# @param [ZonedDateTime, nil] context
# A {ZonedDateTime} used to fill in missing
# 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)
@zoned_date_time_method = begin
# The method with a ZoneId argument was added in OH 4.3
java_method :getZonedDateTime, [java.time.ZoneId]
rescue NameError
nil
end
end

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

# @!visibility private
def to_instant(_context = nil)
# @deprecated OH 3.4 getInstant() was added in OH 4.0
return get_instant if respond_to?(:get_instant)

zoned_date_time.to_instant
to_zoned_date_time.to_instant
end

# @!method to_instant
Expand Down Expand Up @@ -110,6 +126,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 +145,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 7fcb94e

Please sign in to comment.