PebbleTemplates / pebble

Java Template Engine
https://pebbletemplates.io
BSD 3-Clause "New" or "Revised" License
1.1k stars 168 forks source link

Feature:Timestamp Filter #673

Closed freshchen closed 8 months ago

freshchen commented 8 months ago

Converting dates and timestamps to and from each other is very common.

I want to discuss whether we can add optional values ​​about timestamps in format in DateFilter(format,existingFormat)

e.g.: {{ time | date(existingFormat='yyyy-MMMM-dd', format='timestamp') }} {{ time | date(existingFormat='timestamp', format='yyyy-MMMM-dd') }}

optional values: timestamp timestampMillisecond

ebussieres commented 8 months ago

Date filter already supports Temporal and older java.util.Date, so it should cover almost all use cases.

freshchen commented 8 months ago

e.g.: variables:timestamp=1709564462 template:{{ timestamp | date(... ...) }} output:2024-03-04 23:01:02

I didn't find it in the test case. After looking at the code, I still don't know how to use DateFilter to achieve the above effect. Can you help tell me how to configure it?

ebussieres commented 8 months ago

Just use {{ timestamp | date("MM/dd/yyyy HH:mm:ss") }}. It's just plain SimpleDateFormat syntax ... not pebble related

freshchen commented 8 months ago

The Date constructor accepts milliseconds by default, so it does not support the second timestamp that is more commonly used in real life. This is very unintuitive for non-java programmers

If no changes are made, for date processing I have to define another timestamp filter to convert the timestamp into a millisecond timestamp, which is very troublesome to use.

The same goes for converting a formatted date into a timestamp with different time units.

ebussieres commented 8 months ago

You can use any Temporal date too ... such as a LocalDate, LocalDateTime, ZonedDateTime etc.

freshchen commented 8 months ago

Ok got it. I will add existingTimestamp parameter to my project