From dd6c1f6d93386cab0ae4780106ade2eacaf61aa0 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 17 Dec 2024 21:52:56 +0100 Subject: [PATCH] Simplify DateTimeType handling for Freecurrency Signed-off-by: Jacob Laursen --- .../binding/freecurrency/internal/FreecurrencyHandler.java | 4 ++-- .../freecurrency/internal/FreecurrencyProvider.java | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.freecurrency/src/main/java/org/openhab/binding/freecurrency/internal/FreecurrencyHandler.java b/bundles/org.openhab.binding.freecurrency/src/main/java/org/openhab/binding/freecurrency/internal/FreecurrencyHandler.java index 5c07e09f95f..4dfdbf4ac3d 100644 --- a/bundles/org.openhab.binding.freecurrency/src/main/java/org/openhab/binding/freecurrency/internal/FreecurrencyHandler.java +++ b/bundles/org.openhab.binding.freecurrency/src/main/java/org/openhab/binding/freecurrency/internal/FreecurrencyHandler.java @@ -16,7 +16,7 @@ import static org.openhab.binding.freecurrency.internal.FreecurrencyBindingConst import static org.openhab.binding.freecurrency.internal.FreecurrencyBindingConstants.CHANNEL_TYPE_LAST_UPDATE; import java.math.BigDecimal; -import java.time.ZonedDateTime; +import java.time.Instant; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.freecurrency.internal.config.FreecurrencyExhangeRateChannelConfig; @@ -72,7 +72,7 @@ public class FreecurrencyHandler extends BaseThingHandler implements ExchangeRat BigDecimal val = freecurrencyProvider.getExchangeRate(config.currency1, config.currency2); updateState(channel.getUID(), val != null ? new DecimalType(val) : UnDefType.UNDEF); } else if (CHANNEL_TYPE_LAST_UPDATE.equals(channel.getChannelTypeUID())) { - ZonedDateTime lastUpdated = freecurrencyProvider.getLastUpdated(); + Instant lastUpdated = freecurrencyProvider.getLastUpdated(); updateState(channel.getUID(), lastUpdated == null ? UnDefType.UNDEF : new DateTimeType(lastUpdated)); } } diff --git a/bundles/org.openhab.binding.freecurrency/src/main/java/org/openhab/binding/freecurrency/internal/FreecurrencyProvider.java b/bundles/org.openhab.binding.freecurrency/src/main/java/org/openhab/binding/freecurrency/internal/FreecurrencyProvider.java index e7c20e4f899..4e35d1ac6ba 100644 --- a/bundles/org.openhab.binding.freecurrency/src/main/java/org/openhab/binding/freecurrency/internal/FreecurrencyProvider.java +++ b/bundles/org.openhab.binding.freecurrency/src/main/java/org/openhab/binding/freecurrency/internal/FreecurrencyProvider.java @@ -17,7 +17,6 @@ import java.math.MathContext; import java.net.URI; import java.time.Duration; import java.time.Instant; -import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.Collection; import java.util.HashMap; @@ -80,7 +79,7 @@ public class FreecurrencyProvider implements CurrencyProvider, ConfigOptionProvi private @Nullable ScheduledFuture refreshJob; private Map currencies = Map.of(); private Map, BigDecimal> exchangeRates = Map.of(); - private @Nullable ZonedDateTime lastUpdated = null; + private @Nullable Instant lastUpdated = null; private Set exchangeRateListeners = new HashSet<>(); @Activate @@ -133,7 +132,7 @@ public class FreecurrencyProvider implements CurrencyProvider, ConfigOptionProvi return rate2.divide(rate1, MathContext.DECIMAL128); } - public @Nullable ZonedDateTime getLastUpdated() { + public @Nullable Instant getLastUpdated() { return lastUpdated; } @@ -181,7 +180,7 @@ public class FreecurrencyProvider implements CurrencyProvider, ConfigOptionProvi }); exchangeRates = newExchangeRates; logger.debug("Retrieved exchange rates for {} currencies", newExchangeRates.size()); - lastUpdated = ZonedDateTime.now(); + lastUpdated = Instant.now(); // exchange rates are refreshed every day at midnight UTC // we refresh one minute later to be sure we are not too early.