From e1287975bdb50e69ce1436af2a5048872c8e545c Mon Sep 17 00:00:00 2001 From: Libo Song Date: Thu, 30 May 2024 17:03:33 -0400 Subject: [PATCH 1/2] Fix date-time format with configured timezone. --- src/main/java/com/hubspot/jinjava/lib/fn/Functions.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java b/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java index 396a0f9be..259eb0e9f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java +++ b/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java @@ -213,6 +213,8 @@ public static String dateTimeFormat(Object var, String... format) { zoneOffset = ((ZonedDateTime) var).getZone(); } else if (var instanceof PyishDate) { zoneOffset = ((PyishDate) var).toDateTime().getZone(); + } else if (interpreter != null) { + zoneOffset = interpreter.getConfig().getTimeZone(); } if (var == null) { From 181ba21cf5c1b66a2a1ac5155a43630fa92a71bf Mon Sep 17 00:00:00 2001 From: Libo Song Date: Thu, 30 May 2024 17:32:23 -0400 Subject: [PATCH 2/2] Add a test. --- .../lib/filter/DateTimeFormatFilterTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilterTest.java index 13761e0b9..353778e7c 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilterTest.java @@ -204,4 +204,24 @@ public void itUsesDeprecationDateIfNoDateProvided() { JinjavaInterpreter.popCurrent(); } } + + @Test + public void itUsesTimezoneFromConfigToFormatString() { + Jinjava jinjava = new Jinjava( + JinjavaConfig + .newBuilder() + .withTimeZone(ZoneOffset.ofHours(+2)) + .withLocale(new Locale("da")) + .build() + ); + JinjavaInterpreter interpreter = jinjava.newInterpreter(); + JinjavaInterpreter.pushCurrent(interpreter); + try { + long timestamp = 1718920800000L; // 2024-06-20 22:00:00 UTC + assertThat(filter.filter(timestamp, interpreter, "%b %d, %Y, at %I:%M %p")) + .isEqualTo("jun. 21, 2024, at 12:00 AM"); + } finally { + JinjavaInterpreter.popCurrent(); + } + } }