diff --git a/README.md b/README.md index c2ace52..574d51d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,12 @@ This project provides a set of template renderers. - **rife.render.Capitalize** - Capitalizes a template value. +- **rife.render.DateIso** + - Renders the current date in ISO 8061 format. +- **rife.render.DateTimeIso** + - Renders the current date and time in ISO 8061 format. +- **rife.render.DateTimeRfc2822** + - Renders the current date and time in RFC 2822 format. - **rife.render.EncodeBase64** - Encodes a template value to Base64. - **rife.render.EncodeHtml** @@ -29,6 +35,8 @@ This project provides a set of template renderers. - Translates a template value to/from ROT13. - **rife.render.SwapCase** - Swap case of a template value. +- **rife.render.TimeIso** + - Renders the current time in ISO 8061 format. - **rife.render.Trim** - Removes leading and trailing whitespace from a template value. - **rife.render.Uncapitalize** diff --git a/lib/src/main/java/rife/render/DateIso.java b/lib/src/main/java/rife/render/DateIso.java new file mode 100644 index 0000000..c32cccb --- /dev/null +++ b/lib/src/main/java/rife/render/DateIso.java @@ -0,0 +1,56 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package rife.render; + +import rife.template.Template; +import rife.template.ValueRenderer; +import rife.tools.Localization; + +import java.text.DateFormat; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +/** + *
Return the current date in ISO 8601 format.
+ * + *Usage:
+ * + *+ * <!--v render:rife.render.DateIso/--> + * {{v render:rife.render.DateIso}} + *+ * + * @author Erik C. Thauvin + * @since 1.0 + */ +public class DateIso implements ValueRenderer { + /** + * ISO 8601 date and time formatter. + */ + static public final DateTimeFormatter iso8601Formatter = + DateTimeFormatter.ofPattern("yyyy-MM-dd").withLocale(Localization.getLocale()); + + + /** + * {@inheritDoc} + */ + @Override + public String render(Template template, String valueId, String differentiator) { + return ZonedDateTime.now().format(iso8601Formatter); + } +} diff --git a/lib/src/main/java/rife/render/DateTimeIso.java b/lib/src/main/java/rife/render/DateTimeIso.java new file mode 100644 index 0000000..23dbb45 --- /dev/null +++ b/lib/src/main/java/rife/render/DateTimeIso.java @@ -0,0 +1,54 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package rife.render; + +import rife.template.Template; +import rife.template.ValueRenderer; +import rife.tools.Localization; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +/** + *
Return the current date and time in ISO 8601 format.
+ * + *Usage:
+ * + *+ * <!--v render:rife.render.DateTimeIso/--> + * {{v render:rife.render.DateTimeIso}} + *+ * + * @author Erik C. Thauvin + * @since 1.0 + */ +public class DateTimeIso implements ValueRenderer { + /** + * ISO 8601 date and time formatter. + */ + static public final DateTimeFormatter iso8601Formatter = + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXXXX").withLocale(Localization.getLocale()); + + /** + * {@inheritDoc} + */ + @Override + public String render(Template template, String valueId, String differentiator) { + return ZonedDateTime.now().format(iso8601Formatter); + } +} diff --git a/lib/src/main/java/rife/render/DateTimeRfc2822.java b/lib/src/main/java/rife/render/DateTimeRfc2822.java new file mode 100644 index 0000000..551828f --- /dev/null +++ b/lib/src/main/java/rife/render/DateTimeRfc2822.java @@ -0,0 +1,54 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package rife.render; + +import rife.template.Template; +import rife.template.ValueRenderer; +import rife.tools.Localization; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +/** + *
Return the current date and time in RFC 2822 format.
+ * + *Usage:
+ * + *+ * <!--v render:rife.render.DateTimeRfc2822/--> + * {{v render:rife.render.DateTimeRfc2822}} + *+ * + * @author Erik C. Thauvin + * @since 1.0 + */ +public class DateTimeRfc2822 implements ValueRenderer { + /** + * RFC 2822 date and time formatter. + */ + static public final DateTimeFormatter rfc2822Formatter = + DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz").withLocale(Localization.getLocale()); + + /** + * {@inheritDoc} + */ + @Override + public String render(Template template, String valueId, String differentiator) { + return ZonedDateTime.now().format(rfc2822Formatter); + } +} diff --git a/lib/src/main/java/rife/render/TimeIso.java b/lib/src/main/java/rife/render/TimeIso.java new file mode 100644 index 0000000..191cfc9 --- /dev/null +++ b/lib/src/main/java/rife/render/TimeIso.java @@ -0,0 +1,54 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package rife.render; + +import rife.template.Template; +import rife.template.ValueRenderer; +import rife.tools.Localization; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +/** + *
Return the current time in ISO 8601 format.
+ * + *Usage:
+ * + *+ * <!--v render:rife.render.TimeIso/--> + * {{v render:rife.render.TimeIso}} + *+ * + * @author Erik C. Thauvin + * @since 1.0 + */ +public class TimeIso implements ValueRenderer { + /** + * ISO 8601 time formatter. + */ + static public final DateTimeFormatter iso8601Formatter = + DateTimeFormatter.ofPattern("HH:mm:ss").withLocale(Localization.getLocale()); + + /** + * {@inheritDoc} + */ + @Override + public String render(Template template, String valueId, String differentiator) { + return ZonedDateTime.now().format(iso8601Formatter); + } +}