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 17, 2024
1 parent 0f042e1 commit e780346
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
43 changes: 36 additions & 7 deletions lib/openhab/core/types/date_time_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@ def parse(time_string)
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
if OpenHAB::Core.version >= OpenHAB::Core::V4_3
def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
zoned_date_time(ZoneId.system_default)
end
else
def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
zoned_date_time
end
end

# @!method to_zoned_date_time(context = nil)
# @param [ZonedDateTime, nil] context
# A {ZonedDateTime} used to fill in missing fields during conversion. Not used in this class.
# @return [ZonedDateTime]

# @!visibility private
def to_instant(_context = nil)
# @deprecated OH 3.4 getInstant() was added in OH 4.0
Expand All @@ -62,6 +70,7 @@ def to_instant(_context = nil)
# @!method to_instant
# @return [Instant]

# @deprecated These methods have been deprecated in openHAB 4.3.
# act like a Ruby Time
def_delegator :zoned_date_time, :month_value, :month
def_delegator :zoned_date_time, :day_of_month, :mday
Expand All @@ -84,6 +93,9 @@ def initialize(value = nil)
if value.nil?
super()
return
elsif OpenHAB::Core.version >= OpenHAB::Core::V4_3 && value.respond_to?(:to_instant)
super(value.to_instant)
return
elsif value.respond_to?(:to_zoned_date_time)
super(value.to_zoned_date_time)
return
Expand All @@ -110,6 +122,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 OpenHAB::Core.version >= OpenHAB::Core::V4_3

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

Expand All @@ -126,6 +141,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 OpenHAB::Core.version >= OpenHAB::Core::V4_3

zoned_date_time <=> other.zoned_date_time
elsif other.respond_to?(:to_time)
to_time <=> other.to_time
Expand All @@ -147,6 +165,7 @@ def <=>(other)
#
def coerce(other)
logger.trace { "Coercing #{self} as a request from #{other.class}" }
return [other, to_instant] if other.respond_to?(:to_instant)
return [other, zoned_date_time] if other.respond_to?(:to_zoned_date_time)

[DateTimeType.new(other), self] if other.respond_to?(:to_time)
Expand All @@ -158,14 +177,15 @@ def coerce(other)
# @return [Float] Number of seconds since the Epoch, with nanosecond presicion
#
def to_f
zoned_date_time.to_epoch_second + (zoned_date_time.nano / 1_000_000_000)
to_instant.then { |instant| instant.epoch_second + (instant.nano / 1_000_000_000) }
end

#
# The offset in seconds from UTC
#
# @return [Integer] The offset from UTC, in seconds
#
# @deprecated This method has been deprecated in openHAB 4.3.
def utc_offset
zoned_date_time.offset.total_seconds
end
Expand All @@ -175,6 +195,7 @@ def utc_offset
#
# @return [true,false] true if utc_offset == 0, false otherwise
#
# @deprecated This method has been deprecated in openHAB 4.3.
def utc?
utc_offset.zero?
end
Expand All @@ -184,6 +205,7 @@ def utc?
#
# @return [Integer] The day of week
#
# @deprecated This method has been deprecated in openHAB 4.3.
def wday
zoned_date_time.day_of_week.value % 7
end
Expand All @@ -200,6 +222,8 @@ def zone

# @!visibility private
def respond_to_missing?(method, _include_private = false)
# @deprecated OH 4.2 Remove version check when dropping OH 4.2
return true if OpenHAB::Core.version >= OpenHAB::Core::V4_3 && to_instant.respond_to?(method)
return true if zoned_date_time.respond_to?(method)
return true if ::Time.instance_methods.include?(method.to_sym)

Expand All @@ -211,6 +235,11 @@ def respond_to_missing?(method, _include_private = false)
# object representing the same instant
#
def method_missing(method, *args, &block)
# @deprecated OH 4.2 Remove version check when dropping OH 4.2
if OpenHAB::Core.version >= OpenHAB::Core::V4_3 && to_instant.respond_to?(method)
return to_instant.send(method, *args, &block)
end

return zoned_date_time.send(method, *args, &block) if zoned_date_time.respond_to?(method)
return to_time.send(method, *args, &block) if ::Time.instance_methods.include?(method.to_sym)

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 e780346

Please sign in to comment.