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

@ -8,7 +8,8 @@ This project provides a collection of template renderers.
## Date/Time Renderers
| 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. |

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

@ -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");

View file

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