From e1d2570a0e416721c89eff58bd92c11a819aedc8 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 12 Nov 2024 21:48:35 +0100 Subject: [PATCH] Apply configured time-zone in Item UI registry Signed-off-by: Jacob Laursen --- .../ui/internal/items/ItemUIRegistryImpl.java | 22 +++++++++++++++---- .../items/ItemUIRegistryImplTest.java | 10 ++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java index 97a39045eb2..5884192abfa 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java @@ -39,6 +39,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.registry.RegistryChangeListener; import org.openhab.core.config.core.ConfigurableService; +import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.items.GroupItem; import org.openhab.core.items.Item; import org.openhab.core.items.ItemNotFoundException; @@ -137,6 +138,7 @@ public class ItemUIRegistryImpl implements ItemUIRegistry { protected final Set itemUIProviders = new HashSet<>(); private final ItemRegistry itemRegistry; + private final TimeZoneProvider timeZoneProvider; private final Map defaultWidgets = Collections.synchronizedMap(new WeakHashMap<>()); @@ -153,8 +155,10 @@ public WidgetLabelWithSource(String l, WidgetLabelSource s) { } @Activate - public ItemUIRegistryImpl(@Reference ItemRegistry itemRegistry) { + public ItemUIRegistryImpl(final @Reference ItemRegistry itemRegistry, + final @Reference TimeZoneProvider timeZoneProvider) { this.itemRegistry = itemRegistry; + this.timeZoneProvider = timeZoneProvider; } @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) @@ -467,10 +471,20 @@ private Switch createPlayerButtons() { String type = matcher.group(1); String function = matcher.group(2); String value = matcher.group(3); - formatPattern = type + "(" + function + "):" + state.format(value); - transformFailbackValue = state.toString(); + formatPattern = type + "(" + function + "):"; + if (state instanceof DateTimeType dateTimeState) { + formatPattern += dateTimeState.format(value, timeZoneProvider.getTimeZone()); + transformFailbackValue = dateTimeState.toFullString(timeZoneProvider.getTimeZone()); + } else { + formatPattern += state.format(value); + transformFailbackValue = state.toString(); + } } else { - formatPattern = state.format(formatPattern); + if (state instanceof DateTimeType dateTimeState) { + formatPattern = dateTimeState.format(formatPattern, timeZoneProvider.getTimeZone()); + } else { + formatPattern = state.format(formatPattern); + } } } catch (IllegalArgumentException e) { logger.warn("Exception while formatting value '{}' of item {} with format '{}': {}", state, diff --git a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java index 5cc7388ce16..cc845feb620 100644 --- a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java +++ b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java @@ -20,6 +20,7 @@ import static org.mockito.Mockito.*; import java.text.DecimalFormatSymbols; +import java.time.ZoneId; import java.util.ArrayList; import java.util.List; import java.util.TimeZone; @@ -36,6 +37,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; +import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.items.GroupItem; import org.openhab.core.items.Item; import org.openhab.core.items.ItemNotFoundException; @@ -99,22 +101,24 @@ public class ItemUIRegistryImplTest { // we need to get the decimal separator of the default locale for our tests private static final char SEP = (new DecimalFormatSymbols().getDecimalSeparator()); private static final String ITEM_NAME = "Item"; + private static final String DEFAULT_TIME_ZONE = "GMT-6"; private @NonNullByDefault({}) ItemUIRegistryImpl uiRegistry; private @Mock @NonNullByDefault({}) ItemRegistry registryMock; + private @Mock @NonNullByDefault({}) TimeZoneProvider timeZoneProviderMock; private @Mock @NonNullByDefault({}) Widget widgetMock; private @Mock @NonNullByDefault({}) Item itemMock; @BeforeEach public void setup() throws Exception { - uiRegistry = new ItemUIRegistryImpl(registryMock); + uiRegistry = new ItemUIRegistryImpl(registryMock, timeZoneProviderMock); when(widgetMock.getItem()).thenReturn(ITEM_NAME); when(registryMock.getItem(ITEM_NAME)).thenReturn(itemMock); + when(timeZoneProviderMock.getTimeZone()).thenReturn(ZoneId.of(DEFAULT_TIME_ZONE)); - // Set default time zone to GMT-6 - TimeZone.setDefault(TimeZone.getTimeZone("GMT-6")); + TimeZone.setDefault(TimeZone.getTimeZone(DEFAULT_TIME_ZONE)); } @Test