You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2021/12/15 17:48:29 UTC

[freemarker] branch FREEMARKER-35 updated: [FREEMARKER-35] Fixed: Added automatic adjustment of the format style for OffsetTime, if the time zone has DST, and the style doesn't show the offset.

This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch FREEMARKER-35
in repository https://gitbox.apache.org/repos/asf/freemarker.git


The following commit(s) were added to refs/heads/FREEMARKER-35 by this push:
     new c915e2e  [FREEMARKER-35] Fixed: Added automatic adjustment of the format style for OffsetTime, if the time zone has DST, and the style doesn't show the offset.
c915e2e is described below

commit c915e2e2a638a15d5ca1de3d6d634a0da3bb07be
Author: ddekany <dd...@apache.org>
AuthorDate: Wed Dec 15 18:48:21 2021 +0100

    [FREEMARKER-35] Fixed: Added automatic adjustment of the format style for OffsetTime, if the time zone has DST, and the style doesn't show the offset.
---
 src/main/java/freemarker/core/JavaTemplateTemporalFormat.java |  9 +++++----
 .../freemarker/test/templatesuite/templates/temporal.ftl      | 11 +++++++++++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/main/java/freemarker/core/JavaTemplateTemporalFormat.java b/src/main/java/freemarker/core/JavaTemplateTemporalFormat.java
index 1d232d3..06ef378 100644
--- a/src/main/java/freemarker/core/JavaTemplateTemporalFormat.java
+++ b/src/main/java/freemarker/core/JavaTemplateTemporalFormat.java
@@ -113,13 +113,12 @@ class JavaTemplateTemporalFormat extends TemplateTemporalFormat {
                 throw new InvalidFormatParametersException(e.getMessage(), e);
             }
         }
-        this.dateTimeFormatter = dateTimeFormatter.withLocale(locale);
 
         if (isLocalTemporalClass(temporalClass)) {
             this.preFormatValueConversion = null;
         } else {
             PreFormatValueConversion preFormatValueConversion;
-            nonLocalFormatAttempt: do {
+            nonLocalFormatAttempt: while (true) {
                 if (showsZone(dateTimeFormatter)) {
                     if (temporalClass == Instant.class) {
                         preFormatValueConversion = PreFormatValueConversion.INSTANT_TO_ZONED_DATE_TIME;
@@ -143,17 +142,19 @@ class JavaTemplateTemporalFormat extends TemplateTemporalFormat {
                                             + "it's not possible to convert the value to the local time in that zone, "
                                             + "since we don't know the day.");
                         }
+                        dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(timePartFormatStyle);
                         formatString = timePartFormatStyle.name().toLowerCase(Locale.ROOT);
-                        preFormatValueConversion = null; // Avoid false alarm "might not have been initialized"
                         continue nonLocalFormatAttempt;
                     } else {
                         preFormatValueConversion = PreFormatValueConversion.CONVERT_TO_CURRENT_ZONE;
                     }
                 }
-            } while (false);
+                break nonLocalFormatAttempt;
+            };
             this.preFormatValueConversion = preFormatValueConversion;
         }
 
+        this.dateTimeFormatter = dateTimeFormatter.withLocale(locale);
         this.formatString = formatString;
         this.zoneId = timeZone.toZoneId();
     }
diff --git a/src/test/resources/freemarker/test/templatesuite/templates/temporal.ftl b/src/test/resources/freemarker/test/templatesuite/templates/temporal.ftl
index b850552..79346e8 100644
--- a/src/test/resources/freemarker/test/templatesuite/templates/temporal.ftl
+++ b/src/test/resources/freemarker/test/templatesuite/templates/temporal.ftl
@@ -28,6 +28,10 @@
 <@assertEquals expected="Apr 5, 2003 7:07:08 AM" actual=zonedDateTime?string />
 
 <#setting timeZone="America/New_York">
+<@assertEquals expected="6:07:08 AM" actual=localTime?string />
+<#-- Automatic medium->long step up: -->
+<@assertEquals expected="6:07:08 AM Z" actual=offsetTime?string />
+
 <@assertEquals expected="2003-04-05T01:07:08-05:00" actual=instant?string.iso />
 <@assertEquals expected="2003-04-05T06:07:08" actual=localDateTime?string.iso />
 <@assertEquals expected="2003-04-05" actual=localDate?string.iso />
@@ -76,6 +80,13 @@
 <#-- These fail on Java 8 because of JDK-8085887
 <@assertEquals expected="5 avril 2003 06:07:08 ET" actual=localDateTime?string.long />
 <@assertEquals expected="samedi 5 avril 2003 06 h 07 ET" actual=localDateTime?string.full />
+
+<#-- Automatic short->medium->long step up: -->
+<@assertEquals expected="06:07:08 Z" actual=offsetTime?string.short />
+<#-- Automatic medium->long step up: -->
+<@assertEquals expected="06:07:08 Z" actual=offsetTime?string.medium />
+<@assertEquals expected="06:07:08 Z" actual=offsetTime?string.long />
+<@assertEquals expected="06 h 07 Z" actual=offsetTime?string.full />
 -->
 
 <#-- There combinations are clearly not supported by the "localized pattern" API. -->