Added BeatTime

This commit is contained in:
Erik C. Thauvin 2023-03-15 22:07:35 -07:00
parent 178688e7b3
commit 8050ebed46
6 changed files with 88 additions and 19 deletions

View file

@ -7,14 +7,15 @@ This project provides a collection of template renderers.
## Date/Time Renderers ## Date/Time Renderers
| Renderer | Description | | Renderer | Description |
|:---------------------------------|:----------------------------------------------------------| |:---------------------------------|:-----------------------------------------------------------------|
| `rife.render.DateIso` | Renders the current date in ISO 8061 format. | | `rife.render.BeatTime` | Renders the current time in Swatch Internet (.beat) Time format. |
| `rife.render.DateTimeIso` | Renders the current date and time in ISO 8061 format. | | `rife.render.DateIso` | Renders the current date in ISO 8061 format. |
| `rife.render.DateTimeRfc2822` | Renders the current date and time in RFC 2822 format. | | `rife.render.DateTimeIso` | Renders the current date and time in ISO 8061 format. |
| `rife.render.DateTimeUtc` | Renders the current UTC date and time in ISO 8061 format. | | `rife.render.DateTimeRfc2822` | Renders the current date and time in RFC 2822 format. |
| `rife.render.TimeIso` | Renders the current time in ISO 8061 format. | | `rife.render.DateTimeUtc` | Renders the current UTC date and time in ISO 8061 format. |
| `rife.render.Year` | Renders the current year. | | `rife.render.TimeIso` | Renders the current time in ISO 8061 format. |
| `rife.render.Year` | Renders the current year. |
## Encoding Renderers ## Encoding Renderers

View file

@ -16,20 +16,20 @@ repositories {
} }
dependencies { dependencies {
implementation("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT") { implementation("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT") {
this.isChanging = true this.isChanging = true
} }
runtimeOnly("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT:agent") { runtimeOnly("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT:agent") {
this.isChanging = true this.isChanging = true
} }
testImplementation(platform("org.junit:junit-bom:5.9.2")) testImplementation(platform("org.junit:junit-bom:5.9.2"))
testImplementation("org.junit.jupiter:junit-jupiter") testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.assertj:assertj-core:3.24.2") testImplementation("org.assertj:assertj-core:3.24.2")
} }
configurations { configurations {
all { all {
resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS) resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS)
} }
} }
@ -38,7 +38,7 @@ java {
withJavadocJar() withJavadocJar()
withSourcesJar() withSourcesJar()
toolchain { toolchain {
languageVersion.set(JavaLanguageVersion.of(17)) languageVersion.set(JavaLanguageVersion.of(17))
} }
} }

View file

@ -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;
/**
* <p>Renders the current time in Swatch Internet (.beat) Time format.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.BeatTime/--&gt;
* {{v render:rife.render.BeatTime/}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @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());
}
}

View file

@ -23,7 +23,7 @@ import rife.tools.Convert;
/** /**
* Collection of utility-type methods commonly used by the renderers. * Collection of utility-type methods commonly used by the renderers.
*/ */
public final class RenderUtils { public final class RenderUtils {
private RenderUtils() { private RenderUtils() {
// no-op // no-op
} }

View file

@ -24,6 +24,12 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatCode;
class TestDateTime { class TestDateTime {
@Test
void testBestTime() {
var t = TemplateFactory.HTML.get("beatTime");
assertThat(t.getContent()).matches("@\\d{3}");
}
@Test @Test
void testDateIso() { void testDateIso() {
var t = TemplateFactory.HTML.get("dateIso"); var t = TemplateFactory.HTML.get("dateIso");

View file

@ -0,0 +1 @@
<!--v render:rife.render.BeatTime/-->