From 8050ebed4614485973b87f3fc0655f55d65fc50a Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 15 Mar 2023 22:07:35 -0700 Subject: [PATCH] Added BeatTime --- README.md | 17 +++--- lib/build.gradle.kts | 20 +++--- lib/src/main/java/rife/render/BeatTime.java | 61 +++++++++++++++++++ .../main/java/rife/render/RenderUtils.java | 2 +- .../test/java/rife/render/TestDateTime.java | 6 ++ .../test/resources/templates/beatTime.html | 1 + 6 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 lib/src/main/java/rife/render/BeatTime.java create mode 100644 lib/src/test/resources/templates/beatTime.html diff --git a/README.md b/README.md index 42fa07c..957b331 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,15 @@ This project provides a collection of template renderers. ## Date/Time Renderers -| Renderer | Description | -|:---------------------------------|:----------------------------------------------------------| -| `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.DateTimeUtc` | Renders the current UTC date and time in ISO 8061 format. | -| `rife.render.TimeIso` | Renders the current time in ISO 8061 format. | -| `rife.render.Year` | Renders the current year. | +| Renderer | Description | +|:---------------------------------|:-----------------------------------------------------------------| +| `rife.render.BeatTime` | Renders the current time in Swatch Internet (.beat) Time format. | +| `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.DateTimeUtc` | Renders the current UTC date and time in ISO 8061 format. | +| `rife.render.TimeIso` | Renders the current time in ISO 8061 format. | +| `rife.render.Year` | Renders the current year. | ## Encoding Renderers diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 2bd34c1..99c03bc 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -16,20 +16,20 @@ repositories { } dependencies { - implementation("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT") { - this.isChanging = true - } - runtimeOnly("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT:agent") { - this.isChanging = true - } + implementation("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT") { + this.isChanging = true + } + runtimeOnly("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT:agent") { + this.isChanging = true + } - testImplementation(platform("org.junit:junit-bom:5.9.2")) - testImplementation("org.junit.jupiter:junit-jupiter") + testImplementation(platform("org.junit:junit-bom:5.9.2")) + testImplementation("org.junit.jupiter:junit-jupiter") testImplementation("org.assertj:assertj-core:3.24.2") } configurations { - all { + all { resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS) } } @@ -38,7 +38,7 @@ java { withJavadocJar() withSourcesJar() toolchain { - languageVersion.set(JavaLanguageVersion.of(17)) + languageVersion.set(JavaLanguageVersion.of(17)) } } diff --git a/lib/src/main/java/rife/render/BeatTime.java b/lib/src/main/java/rife/render/BeatTime.java new file mode 100644 index 0000000..eb8f7c3 --- /dev/null +++ b/lib/src/main/java/rife/render/BeatTime.java @@ -0,0 +1,61 @@ +/* + * 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 java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; + +/** + *

Renders the current time in Swatch Internet (.beat) Time format.

+ * + *

Usage:

+ * + *
+ *   <!--v render:rife.render.BeatTime/-->
+ *   {{v render:rife.render.BeatTime/}}
+ * 
+ * + * @author Erik C. Thauvin + * @since 1.0 + */ +public class BeatTime implements ValueRenderer { + /** + * Returns the Swatch Internet (.beat) Time for the give date & time. + * + * @param zonedDateTime the date and time. + * @return the .beat time. (eg.: {@code @248}) + */ + public static String beatTime(ZonedDateTime zonedDateTime) { + var zdt = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC+01:00")); + var beats = (int) ((zdt.get(ChronoField.SECOND_OF_MINUTE) + (zdt.get(ChronoField.MINUTE_OF_HOUR) * 60) + + (zdt.get(ChronoField.HOUR_OF_DAY) * 3600)) / 86.4); + return String.format("@%03d", beats); + } + + /** + * {@inheritDoc} + */ + @Override + public String render(Template template, String valueId, String differentiator) { + return beatTime(ZonedDateTime.now()); + } +} \ No newline at end of file diff --git a/lib/src/main/java/rife/render/RenderUtils.java b/lib/src/main/java/rife/render/RenderUtils.java index b30a43b..609f65b 100644 --- a/lib/src/main/java/rife/render/RenderUtils.java +++ b/lib/src/main/java/rife/render/RenderUtils.java @@ -23,7 +23,7 @@ import rife.tools.Convert; /** * Collection of utility-type methods commonly used by the renderers. */ -public final class RenderUtils { +public final class RenderUtils { private RenderUtils() { // no-op } diff --git a/lib/src/test/java/rife/render/TestDateTime.java b/lib/src/test/java/rife/render/TestDateTime.java index 36ef326..bc3d7e1 100644 --- a/lib/src/test/java/rife/render/TestDateTime.java +++ b/lib/src/test/java/rife/render/TestDateTime.java @@ -24,6 +24,12 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; class TestDateTime { + @Test + void testBestTime() { + var t = TemplateFactory.HTML.get("beatTime"); + assertThat(t.getContent()).matches("@\\d{3}"); + } + @Test void testDateIso() { var t = TemplateFactory.HTML.get("dateIso"); diff --git a/lib/src/test/resources/templates/beatTime.html b/lib/src/test/resources/templates/beatTime.html new file mode 100644 index 0000000..1dcaea5 --- /dev/null +++ b/lib/src/test/resources/templates/beatTime.html @@ -0,0 +1 @@ + \ No newline at end of file