Repository: PebbleTemplates/pebble
Pull request #346 (2018-05-24 10:47:00)

Implement [java.util.Long] timestamp argument support in date filter


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 512b912d..83478d0c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@
 - Java8 Date API support on date filter
 - Replace guava cache with caffeine
 
+## v2.6.1
+- Implement [java.util.Long] timestamp argument support in date filter
+
 ## v2.6.0 (2018-05-20)
 - Add support for 'as' and 'from' in import statement (#271)
 - Add support for spring-boot-2 (#340)
diff --git a/src/main/java/com/mitchellbosecke/pebble/extension/core/DateFilter.java b/src/main/java/com/mitchellbosecke/pebble/extension/core/DateFilter.java
index ae229365..5f53b703 100644
--- a/src/main/java/com/mitchellbosecke/pebble/extension/core/DateFilter.java
+++ b/src/main/java/com/mitchellbosecke/pebble/extension/core/DateFilter.java
@@ -15,7 +15,13 @@ import com.mitchellbosecke.pebble.template.EvaluationContext;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import static java.lang.String.format;
 
 public class DateFilter implements Filter {
 
@@ -54,7 +60,13 @@ public class DateFilter implements Filter {
                 throw new RuntimeException("Could not parse date", e);
             }
         } else {
-            date = (Date) input;
+            if (input instanceof Date) {
+                date = (Date) input;
+            } else if (input instanceof Number) {
+                date = new Date(((Number) input).longValue());
+            } else {
+                throw new IllegalArgumentException(format("Unsupported argument type: %s (value: %s)", input.getClass().getName(), input));
+            }
         }
 
         return new SafeString(intendedFormat.format(date));


Test patch:
diff --git a/src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java b/src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java
index a818254b..fbca998f 100755
--- a/src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java
+++ b/src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java
@@ -32,6 +32,7 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
+import static java.lang.Boolean.TRUE;
 import static java.util.Collections.emptyList;
 import static org.junit.Assert.assertEquals;
 
@@ -162,6 +163,35 @@ public class CoreFiltersTest extends AbstractTest {
         assertEquals("", writer.toString());
     }
 
+    @Test
+    public void testDateWithNumberInput() throws IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "{{ dateAsNumber | date(\"MM/dd/yyyy\") }}";
+
+        PebbleTemplate template = pebble.getTemplate(source);
+        Map<String, Object> context = new HashMap<>();
+        context.put("dateAsNumber", 1518004210000L);
+
+        Writer writer = new StringWriter();
+        template.evaluate(writer, context);
+        assertEquals("02/07/2018", writer.toString());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testDateWithUnsupportedInput() throws IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "{{ unsupportedDateType | date(\"MM/dd/yyyy\") }}";
+
+        PebbleTemplate template = pebble.getTemplate(source);
+        Map<String, Object> context = new HashMap<>();
+        context.put("unsupportedDateType", TRUE);
+
+        Writer writer = new StringWriter();
+        template.evaluate(writer, context);
+    }
+
     @Test
     public void testUrlEncode() throws PebbleException, IOException {
         PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();