From 04b9abda3b1fd4aa7fd89e39546f28a6f4097f60 Mon Sep 17 00:00:00 2001 From: Jeroen Laverman Date: Wed, 17 Apr 2019 11:09:44 +0200 Subject: [PATCH] Feature fixed timezone (#824) * Fix typo * Set fixedTimeZone via properties * Fix Sonar findings * Fix review findings * Rename variable * Fix logic Signed-off-by: Jeroen Laverman --- .../eclipse/hawkbit/ui/AbstractHawkbitUI.java | 2 ++ .../org/eclipse/hawkbit/ui/UiProperties.java | 11 ++++++++++ .../hawkbit/ui/utils/SPDateTimeUtil.java | 21 ++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/AbstractHawkbitUI.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/AbstractHawkbitUI.java index 58592b566..619f92d15 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/AbstractHawkbitUI.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/AbstractHawkbitUI.java @@ -16,6 +16,7 @@ import org.eclipse.hawkbit.ui.menu.DashboardMenuItem; import org.eclipse.hawkbit.ui.push.EventPushStrategy; import org.eclipse.hawkbit.ui.themes.HawkbitTheme; import org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil; +import org.eclipse.hawkbit.ui.utils.SPDateTimeUtil; import org.eclipse.hawkbit.ui.utils.SpringContextHelper; import org.eclipse.hawkbit.ui.utils.VaadinMessageSource; import org.slf4j.Logger; @@ -121,6 +122,7 @@ public abstract class AbstractHawkbitUI extends UI implements DetachListener { rootLayout.setSizeFull(); HawkbitCommonUtil.initLocalization(this, uiProperties.getLocalization(), i18n); + SPDateTimeUtil.initializeFixedTimeZoneProperty(uiProperties.getFixedTimeZone()); dashboardMenu.init(); dashboardMenu.setResponsive(true); diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/UiProperties.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/UiProperties.java index 4caf8f3a1..cb1c11dec 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/UiProperties.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/UiProperties.java @@ -25,6 +25,8 @@ public class UiProperties implements Serializable { private boolean gravatar; + private String fixedTimeZone; + private final Localization localization = new Localization(); private final Links links = new Links(); @@ -43,6 +45,15 @@ public class UiProperties implements Serializable { this.gravatar = gravatar; } + public String getFixedTimeZone() { + return fixedTimeZone; + } + + public void setFixedTimeZone(final String fixedTimeZone) { + this.fixedTimeZone = fixedTimeZone; + } + + /** * Localization information */ diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/utils/SPDateTimeUtil.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/utils/SPDateTimeUtil.java index 64481434d..763c600cc 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/utils/SPDateTimeUtil.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/utils/SPDateTimeUtil.java @@ -19,6 +19,7 @@ import org.eclipse.hawkbit.repository.model.BaseEntity; import com.google.common.collect.Maps; import com.vaadin.server.WebBrowser; +import org.springframework.util.StringUtils; /** * Common Util to get date/time related information. @@ -27,6 +28,7 @@ public final class SPDateTimeUtil { private static final String DURATION_FORMAT = "y','M','d','H','m','s"; private static final Map DURATION_I18N = Maps.newHashMapWithExpectedSize(6); + private static String fixedTimeZoneProperty; static { DURATION_I18N.put(0, CalendarI18N.YEAR); @@ -41,12 +43,29 @@ public final class SPDateTimeUtil { } + /** - * Get browser time zone. + * Set fixed UI timezone + * + * @param fixedTimeZoneProperty + * time zone e.g. Europe/Berlin. If time zone is unknown, it will default to GMT + */ + public static void initializeFixedTimeZoneProperty(String fixedTimeZoneProperty) { + SPDateTimeUtil.fixedTimeZoneProperty = fixedTimeZoneProperty; + } + + /** + * Get browser time zone or fixed time zone if configured * * @return TimeZone */ public static TimeZone getBrowserTimeZone() { + + + if (!StringUtils.isEmpty(fixedTimeZoneProperty)) { + return TimeZone.getTimeZone(fixedTimeZoneProperty); + } + final WebBrowser webBrowser = com.vaadin.server.Page.getCurrent().getWebBrowser(); final String[] timeZones = TimeZone.getAvailableIDs(webBrowser.getRawTimezoneOffset()); TimeZone tz = TimeZone.getDefault();