Skip to content

Commit

Permalink
feat(core): support LocalDate & LocalDateTime on pebble date
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo committed Jun 2, 2022
1 parent 18ccc98 commit 40efdcb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
25 changes: 21 additions & 4 deletions core/src/main/java/io/kestra/core/runners/pebble/AbstractDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.FormatStyle;
import java.util.*;

Expand Down Expand Up @@ -102,10 +103,26 @@ protected static ZonedDateTime convert(Object value, ZoneId zoneId, String exist
return Instant.ofEpochSecond((Long) value).atZone(zoneId);
}

if (existingFormat != null) {
return ZonedDateTime.parse((String) value, formatter(existingFormat));
try {
if (existingFormat != null) {
return ZonedDateTime.parse((String) value, formatter(existingFormat));
} else {
return ZonedDateTime.parse((String) value);
}
} catch (DateTimeParseException e) {
try {
if (existingFormat != null) {
return LocalDateTime.parse((String) value, formatter(existingFormat)).atZone(zoneId);
} else {
return LocalDateTime.parse((String) value).atZone(zoneId);
}
} catch (DateTimeParseException e2) {
if (existingFormat != null) {
return LocalDate.parse((String) value, formatter(existingFormat)).atStartOfDay().atZone(zoneId);
} else {
return LocalDate.parse((String) value).atStartOfDay().atZone(zoneId);
}
}
}

return ZonedDateTime.parse((String) value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

import jakarta.inject.Inject;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -49,6 +51,23 @@ void dateFormat() throws IllegalVariableEvaluationException {
));
}

@Test
void dateStringFormat() throws IllegalVariableEvaluationException {
String render = variableRenderer.render(
"{{ \"July 24, 2001\" | date(\"yyyy-MM-dd\", existingFormat=\"MMMM dd, yyyy\") }}\n" +
"{{ \"2013-09-08T17:19:12+02:00\" | date(timeZone=\"Europe/Paris\") }}\n" +
"{{ \"2013-09-08T17:19:12\" | date(timeZone=\"Europe/Paris\") }}\n" +
"{{ \"2013-09-08\" | date(timeZone=\"Europe/Paris\") }}\n",
Map.of()
);

assertThat(render, is("2001-07-24\n" +
"2013-09-08T17:19:12.000000+02:00\n" +
"2013-09-08T17:19:12.000000+02:00\n" +
"2013-09-08T00:00:00.000000+02:00\n"
));
}

@Test
void timestamp() throws IllegalVariableEvaluationException {
String render = variableRenderer.render(
Expand Down

0 comments on commit 40efdcb

Please sign in to comment.