aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/ui/AbstractDateField.java
diff options
context:
space:
mode:
authorTarek Oraby <42799254+tarekoraby@users.noreply.github.com>2020-04-21 12:22:45 +0300
committerGitHub <noreply@github.com>2020-04-21 12:22:45 +0300
commit01936188ca3105271109e7b8a5ede42e8a1cc1d8 (patch)
tree2d11c192ffa55050e97cd87419b124d56272ed76 /server/src/main/java/com/vaadin/ui/AbstractDateField.java
parentd35bd4cde4ae0074d68c7e1f10f2be8757403cc7 (diff)
downloadvaadin-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/ui/AbstractDateField.java')
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractDateField.java89
1 files changed, 84 insertions, 5 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java
index 9c5d9bb656..84a26436c0 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java
@@ -179,6 +179,30 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
};
/**
+ * The default start year (inclusive) from which to calculate the
+ * daylight-saving time zone transition dates.
+ */
+ private static final int DEFAULT_START_YEAR = 1980;
+
+ /**
+ * 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 DEFAULT_YEARS_FROM_NOW = 20;
+
+ /**
+ * The optional user-supplied start year (inclusive) from which to calculate
+ * the daylight-saving time zone transition dates.
+ */
+ private Integer startYear;
+
+ /**
+ * The optional user-supplied end year (inclusive) until which to calculate
+ * the daylight-saving time zone transition dates.
+ */
+ private Integer endYear;
+
+ /**
* Value of the field.
*/
private T value;
@@ -489,7 +513,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
/**
* Sets the {@link ZoneId}, which is used when {@code z} is included inside
- * the {@link #setDateFormat(String)}.
+ * the {@link #setDateFormat(String)} .
*
* @param zoneId
* the zone id
@@ -498,27 +522,82 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
public void setZoneId(ZoneId zoneId) {
if (zoneId != this.zoneId
|| (zoneId != null && !zoneId.equals(this.zoneId))) {
- updateTimeZoneJSON(zoneId, getLocale());
+ updateTimeZoneJSON(zoneId, getLocale(), getStartYear(),
+ getEndYear());
}
this.zoneId = zoneId;
}
- private void updateTimeZoneJSON(ZoneId zoneId, Locale locale) {
+ private void updateTimeZoneJSON(ZoneId zoneId, Locale locale, int startYear,
+ int endYear) {
String timeZoneJSON;
if (zoneId != null && locale != null) {
- timeZoneJSON = TimeZoneUtil.toJSON(zoneId, locale);
+ timeZoneJSON = TimeZoneUtil.toJSON(zoneId, locale, startYear,
+ endYear);
} else {
timeZoneJSON = null;
}
getState().timeZoneJSON = timeZoneJSON;
}
+ /**
+ * Sets {@link startYear} and {@link endYear}: the start and end years (both
+ * inclusive) between which to calculate the daylight-saving time zone
+ * transition dates. Both parameters are used when '{@code z}' is included
+ * inside the {@link #setDateFormat(String)}, they would have no effect
+ * otherwise. Specifically, these parameters determine the range of years in
+ * which zone names are are adjusted to show the daylight saving names.
+ *
+ * If no values are provided, by default {@link startYear} is set to
+ * {@value #DEFAULT_START_YEAR}, and {@link endYear} is set to
+ * {@value #DEFAULT_YEARS_FROM_NOW} years into the future from the current
+ * date.
+ *
+ * @param startYear
+ * the start year of DST transitions
+ * @param endYear
+ * the end year of DST transitions
+ * @since 8.11
+ */
+ public void setDaylightSavingTimeRange(int startYear, int endYear) {
+ if (startYear > endYear) {
+ throw new IllegalArgumentException(
+ "The start year from which to begin calculating the "
+ + "daylight-saving time zone transition dates must"
+ + " be less than or equal to the end year.\n"
+ + startYear + " is greater than " + endYear);
+ }
+ if (this.startYear == null || this.endYear == null
+ || startYear != this.startYear || endYear != this.endYear) {
+ updateTimeZoneJSON(getZoneId(), getLocale(), startYear, endYear);
+ }
+ this.startYear = startYear;
+ this.endYear = endYear;
+ }
+
+ private int getStartYear() {
+ if (startYear == null) {
+ return DEFAULT_START_YEAR;
+ } else {
+ return startYear;
+ }
+ }
+
+ private int getEndYear() {
+ if (endYear == null) {
+ return LocalDate.now().getYear() + DEFAULT_YEARS_FROM_NOW;
+ } else {
+ return endYear;
+ }
+ }
+
@Override
public void setLocale(Locale locale) {
Locale oldLocale = getLocale();
if (locale != oldLocale
|| (locale != null && !locale.equals(oldLocale))) {
- updateTimeZoneJSON(getZoneId(), locale);
+ updateTimeZoneJSON(getZoneId(), locale, getStartYear(),
+ getEndYear());
}
super.setLocale(locale);
}