diff options
author | Tarek Oraby <42799254+tarekoraby@users.noreply.github.com> | 2020-04-21 12:22:45 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-21 12:22:45 +0300 |
commit | 01936188ca3105271109e7b8a5ede42e8a1cc1d8 (patch) | |
tree | 2d11c192ffa55050e97cd87419b124d56272ed76 /server/src/main/java/com/vaadin/util | |
parent | d35bd4cde4ae0074d68c7e1f10f2be8757403cc7 (diff) | |
download | vaadin-framework-01936188ca3105271109e7b8a5ede42e8a1cc1d8.tar.gz vaadin-framework-01936188ca3105271109e7b8a5ede42e8a1cc1d8.zip |
Allow AbstractDateField to provide DST zone names over custom ranges (#11927)
DateTimeField and DateField currently implement a hardcoded logic by which they adjust their time zone names to display daylight-saving time (DST) zone names. Specifically, this hardcoded logic only adjusts the displayed date to DST format if that date falls in one of the years between 1980 and the following 20 years in the future from the current date (that is, until 2040 at the time of this commit).
For some use cases, this is problematic because it is desirable to display proper DST-adjusted time zones beyond the 20 years limit (and possibly also before 1980).
Rather than choosing another arbitrary, hardcoded threshold, this commit extends the AbstractDateField API to allow the user to choose the range (start and end years) between which the DST transition dates are calculated (and hence displayed properly). If the user doesn't invoke this new API, DateTimeField and DateField will default to behave according the existing logic (i.e. display DST zone names between 1980 and 20 years into the future).
Closes #11919
Diffstat (limited to 'server/src/main/java/com/vaadin/util')
-rw-r--r-- | server/src/main/java/com/vaadin/util/TimeZoneUtil.java | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/server/src/main/java/com/vaadin/util/TimeZoneUtil.java b/server/src/main/java/com/vaadin/util/TimeZoneUtil.java index 16e8323f9e..2997378de1 100644 --- a/server/src/main/java/com/vaadin/util/TimeZoneUtil.java +++ b/server/src/main/java/com/vaadin/util/TimeZoneUtil.java @@ -42,13 +42,14 @@ import elemental.json.impl.JsonUtil; public final class TimeZoneUtil implements Serializable { /** - * The start year used to send the time zone transition dates. + * The default start year (inclusive) from which to calculate the + * daylight-saving time zone transition dates. */ private static final int STARTING_YEAR = 1980; /** - * Till how many years from now, should we send the time zone transition - * dates. + * The default value of the number of future years from the current date for + * which the daylight-saving time zone transition dates are calculated. */ private static final int YEARS_FROM_NOW = 20; @@ -61,6 +62,12 @@ public final class TimeZoneUtil implements Serializable { * which is used in * {@link com.google.gwt.i18n.client.TimeZone#createTimeZone(String)}. * + * This method calculates the JSON string from the year + * {@value #STARTING_YEAR} until {@value #YEARS_FROM_NOW} years into the + * future from the current date. + * + * @see #toJSON(ZoneId, Locale, int, int) + * * @param zoneId * the {@link ZoneId} to get the daylight transitions from * @param locale @@ -69,6 +76,32 @@ public final class TimeZoneUtil implements Serializable { * @return the encoded string */ public static String toJSON(ZoneId zoneId, Locale locale) { + int endYear = LocalDate.now().getYear() + YEARS_FROM_NOW; + return toJSON(zoneId, locale, STARTING_YEAR, endYear); + } + + /** + * Returns a JSON string of the specified {@code zoneId} and {@link Locale}, + * which is used in + * {@link com.google.gwt.i18n.client.TimeZone#createTimeZone(String)}. + * + * This method calculates the JSON string from {@code startYear} until + * {@code startYear}, both inclusive. + * + * @param zoneId + * the {@link ZoneId} to get the daylight transitions from + * @param locale + * the locale used to determine the short name of the time zone + * @param startYear + * the start year of DST transitions + * @param endYear + * the end year of DST transitions + * + * @return the encoded string + * @since 8.11 + */ + public static String toJSON(ZoneId zoneId, Locale locale, int startYear, + int endYear) { if (zoneId == null || locale == null) { return null; } @@ -78,9 +111,8 @@ public final class TimeZoneUtil implements Serializable { TimeZoneInfo info = new TimeZoneInfo(); - int endYear = LocalDate.now().getYear() + YEARS_FROM_NOW; if (timeZone.useDaylightTime()) { - for (int year = STARTING_YEAR; year <= endYear; year++) { + for (int year = startYear; year <= endYear; year++) { ZonedDateTime i = LocalDateTime.of(year, 1, 1, 0, 0) .atZone(zoneId); while (true) { |