Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented default behavior of date if there is no inputted existingTemplate #677

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import io.pebbletemplates.pebble.template.PebbleTemplate;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.ZoneId;
Expand Down Expand Up @@ -50,40 +49,46 @@ public Object apply(Object input, Map<String, Object> args, PebbleTemplate self,
final Locale locale = context.getLocale();
final String format = (String) args.get("format");
final String timeZone = (String) args.get("timeZone");
final String existingFormat = (String) args.get("existingFormat");

if (TemporalAccessor.class.isAssignableFrom(input.getClass())) {
return this.applyTemporal((TemporalAccessor) input, self, locale, lineNumber, format, timeZone);
}
return this.applyDate(
input, self, locale, lineNumber,
format, (String) args.get("existingFormat"), timeZone);
format, existingFormat, timeZone);
}

private Object applyDate(Object dateOrString, final PebbleTemplate self, final Locale locale,
int lineNumber, final String format, final String existingFormatString, final String timeZone)
throws PebbleException {
Date date;
DateFormat existingFormat;
Date date = null;
DateFormat intendedFormat;
if (existingFormatString != null) {
existingFormat = new SimpleDateFormat(existingFormatString, locale);

if (dateOrString instanceof Date) {
date = (Date) dateOrString;
} else if (dateOrString instanceof Number) {
date = new Date(((Number) dateOrString).longValue());
} else if (dateOrString instanceof String) {
try {
date = existingFormat.parse(dateOrString.toString());
} catch (ParseException e) {
throw new PebbleException(e, String.format("Could not parse the string '%s' into a date.",
dateOrString.toString()), lineNumber, self.getName());
SimpleDateFormat formatter;
if (existingFormatString != null){
formatter = new SimpleDateFormat(existingFormatString);
} else {
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
}
date = formatter.parse(dateOrString.toString());
} catch (Exception e) {
String formatTried = existingFormatString != null ? existingFormatString : "yyyy-MM-dd'T'HH:mm:ssZ";
throw new PebbleException(e, String.format("Could not parse the string '%s' into a date, with formatting: %s",
dateOrString.toString(), formatTried), lineNumber, self.getName());
}
} else {
if (dateOrString instanceof Date) {
date = (Date) dateOrString;
} else if (dateOrString instanceof Number) {
date = new Date(((Number) dateOrString).longValue());
} else {
throw new IllegalArgumentException(
format("Unsupported argument type: %s (value: %s)", dateOrString.getClass().getName(),
dateOrString));
}
throw new IllegalArgumentException(
format("Unsupported argument type: %s (value: %s)", dateOrString.getClass().getName(),
dateOrString));
}

intendedFormat = new SimpleDateFormat(format == null ? "yyyy-MM-dd'T'HH:mm:ssZ" : format, locale);
if (timeZone != null) {
intendedFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ void testDate() throws ParseException, PebbleException, IOException {
assertEquals("07/01/20122012-July-12012/July/1", writer.toString());
}

@Test
void testDefaultDateFormat() throws PebbleException, IOException{
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
.strictVariables(false)
.defaultLocale(Locale.ENGLISH).build();

String source = "{{ stringDate | date('MM/dd/yyyy') }}";

PebbleTemplate template = pebble.getTemplate(source);
Map<String, Object> context = new HashMap<>();
context.put("stringDate", "2004-02-12T15:19:21+04:00");

Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("02/12/2004", writer.toString());
}

@Test
void testDateJava8() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine
Expand Down