-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12128 from mkouba/issue-11181
Qute: add basic date-time formatting support
- Loading branch information
Showing
8 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...yment/src/test/java/io/quarkus/qute/deployment/extensions/TimeTemplateExtensionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.quarkus.qute.deployment.extensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.qute.Template; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class TimeTemplateExtensionsTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addAsResource(new StringAsset( | ||
"{now.format('d uuuu')}:{nowLocalDate.format('d MMM uuuu',myLocale)}:{time:format(nowDate,'d MMM uuuu',myLocale)}:{time:format(nowCalendar,'d uuuu')}"), | ||
"templates/foo.html")); | ||
|
||
@Inject | ||
Template foo; | ||
|
||
@Test | ||
public void testFormat() { | ||
Calendar nowCal = Calendar.getInstance(); | ||
nowCal.set(Calendar.YEAR, 2020); | ||
nowCal.set(Calendar.MONTH, Calendar.SEPTEMBER); | ||
nowCal.set(Calendar.DAY_OF_MONTH, 10); | ||
Date nowDate = nowCal.getTime(); | ||
assertEquals("10 2020:10 Sep 2020:10 Sep 2020:10 2020", foo | ||
.data("now", LocalDateTime.of(2020, 9, 10, 11, 12)) | ||
.data("nowLocalDate", LocalDate.of(2020, 9, 10)) | ||
.data("nowDate", nowDate) | ||
.data("nowCalendar", nowCal) | ||
.data("myLocale", Locale.ENGLISH).render()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...te/runtime/src/main/java/io/quarkus/qute/runtime/extensions/NumberTemplateExtensions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...qute/runtime/src/main/java/io/quarkus/qute/runtime/extensions/TimeTemplateExtensions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package io.quarkus.qute.runtime.extensions; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.enterprise.inject.Vetoed; | ||
|
||
import io.quarkus.qute.TemplateExtension; | ||
|
||
@Vetoed // Make sure no bean is created from this class | ||
@TemplateExtension | ||
public class TimeTemplateExtensions { | ||
|
||
private static final Map<Key, DateTimeFormatter> FORMATTER_CACHE = new ConcurrentHashMap<>(); | ||
|
||
static String format(TemporalAccessor temporal, String pattern) { | ||
return FORMATTER_CACHE.computeIfAbsent(new Key(pattern, null, null), TimeTemplateExtensions::formatterForKey) | ||
.format(temporal); | ||
} | ||
|
||
static String format(TemporalAccessor temporal, String pattern, Locale locale) { | ||
return FORMATTER_CACHE.computeIfAbsent(new Key(pattern, locale, null), TimeTemplateExtensions::formatterForKey) | ||
.format(temporal); | ||
} | ||
|
||
static String format(TemporalAccessor temporal, String pattern, Locale locale, ZoneId timeZone) { | ||
return FORMATTER_CACHE.computeIfAbsent(new Key(pattern, locale, timeZone), TimeTemplateExtensions::formatterForKey) | ||
.format(temporal); | ||
} | ||
|
||
@TemplateExtension(namespace = "time") | ||
static String format(Object dateTimeObject, String pattern) { | ||
return format(getFormattableObject(dateTimeObject, ZoneId.systemDefault()), pattern); | ||
} | ||
|
||
@TemplateExtension(namespace = "time") | ||
static String format(Object dateTimeObject, String pattern, Locale locale) { | ||
return format(getFormattableObject(dateTimeObject, ZoneId.systemDefault()), pattern, locale); | ||
} | ||
|
||
@TemplateExtension(namespace = "time") | ||
static String format(Object dateTimeObject, String pattern, Locale locale, ZoneId timeZone) { | ||
return format(getFormattableObject(dateTimeObject, timeZone), pattern, locale, timeZone); | ||
} | ||
|
||
private static TemporalAccessor getFormattableObject(Object value, | ||
ZoneId timeZone) { | ||
if (value instanceof TemporalAccessor) { | ||
return (TemporalAccessor) value; | ||
} else if (value instanceof Date) { | ||
return LocalDateTime.ofInstant(((Date) value).toInstant(), | ||
timeZone); | ||
} else if (value instanceof Calendar) { | ||
return LocalDateTime.ofInstant(((Calendar) value).toInstant(), | ||
timeZone); | ||
} else if (value instanceof Number) { | ||
return LocalDateTime.ofInstant( | ||
Instant.ofEpochMilli(((Number) value).longValue()), | ||
timeZone); | ||
} else { | ||
throw new IllegalArgumentException("Not a formattable date/time object: " + value); | ||
} | ||
} | ||
|
||
private static DateTimeFormatter formatterForKey(Key key) { | ||
DateTimeFormatter formatter; | ||
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); | ||
builder.appendPattern(key.pattern); | ||
if (key.locale != null) { | ||
formatter = builder.toFormatter(key.locale); | ||
} else { | ||
formatter = builder.toFormatter(); | ||
} | ||
return key.timeZone != null ? formatter.withZone(key.timeZone) : formatter; | ||
} | ||
|
||
static final class Key { | ||
|
||
private final String pattern; | ||
private final Locale locale; | ||
private final ZoneId timeZone; | ||
|
||
public Key(String pattern, Locale locale, ZoneId timeZone) { | ||
this.pattern = pattern; | ||
this.locale = locale; | ||
this.timeZone = timeZone; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((locale == null) ? 0 : locale.hashCode()); | ||
result = prime * result + ((pattern == null) ? 0 : pattern.hashCode()); | ||
result = prime * result + ((timeZone == null) ? 0 : timeZone.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
Key other = (Key) obj; | ||
return Objects.equals(locale, other.locale) && Objects.equals(pattern, other.pattern) | ||
&& Objects.equals(timeZone, other.timeZone); | ||
} | ||
|
||
} | ||
|
||
} |