Skip to content

Commit

Permalink
Apply configured time-zone in Item UI registry
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Nov 12, 2024
1 parent 5fd517f commit e1d2570
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,6 +138,7 @@ public class ItemUIRegistryImpl implements ItemUIRegistry {
protected final Set<ItemUIProvider> itemUIProviders = new HashSet<>();

private final ItemRegistry itemRegistry;
private final TimeZoneProvider timeZoneProvider;

private final Map<Widget, Widget> defaultWidgets = Collections.synchronizedMap(new WeakHashMap<>());

Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e1d2570

Please sign in to comment.