feat(clock): add local and zoned date methods to service
All checks were successful
Build / build (pull_request) Successful in 2m59s

This commit is contained in:
Jorge Bornhausen 2025-06-19 16:10:53 +02:00
parent bbd1d80d6c
commit 6ac4bd783e
Signed by: jorge.bornhausen
SSH key fingerprint: SHA256:X2ootOwvCeP4FoNfmVUFIKIbhq95tAgnt7Oqg3x+lfs
3 changed files with 56 additions and 4 deletions

View file

@ -1,10 +1,19 @@
package ch.phoenix.oss.quarkus.commons.clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
public interface ClockService {
Instant instant();
long currentTimeMillis();
LocalDate localDate();
LocalDateTime localDateTime();
ZonedDateTime zonedDateTime();
}

View file

@ -2,8 +2,7 @@ package ch.phoenix.oss.quarkus.commons.clock;
import io.quarkus.arc.DefaultBean;
import jakarta.enterprise.context.ApplicationScoped;
import java.time.Clock;
import java.time.Instant;
import java.time.*;
@DefaultBean
@ApplicationScoped
@ -24,4 +23,19 @@ class ClockServiceImpl implements ClockService {
public long currentTimeMillis() {
return clock.millis();
}
@Override
public LocalDate localDate() {
return LocalDate.now(clock);
}
@Override
public LocalDateTime localDateTime() {
return LocalDateTime.now(clock);
}
@Override
public ZonedDateTime zonedDateTime() {
return ZonedDateTime.now(clock);
}
}

View file

@ -6,8 +6,7 @@ import static org.mockito.Mockito.when;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import java.time.Clock;
import java.time.Instant;
import java.time.*;
import org.junit.jupiter.api.Test;
@QuarkusTest
@ -38,4 +37,34 @@ class ClockServiceImplTest {
.as("Instant should match expected value")
.isEqualTo(expected);
}
@Test
void localDate() {
var instant = Instant.ofEpochMilli(1729280640915L);
when(clock.instant()).thenReturn(instant);
when(clock.getZone()).thenReturn(ZoneId.of("UTC"));
assertThat(clockService.localDate())
.as("LocalDate should match expected value")
.isEqualTo(LocalDate.parse("2024-10-18"));
}
@Test
void localDateTime() {
var instant = Instant.ofEpochMilli(1729280640915L);
when(clock.instant()).thenReturn(instant);
when(clock.getZone()).thenReturn(ZoneId.of("UTC"));
assertThat(clockService.localDateTime())
.as("LocalDateTime should match expected value")
.isEqualTo(LocalDateTime.parse("2024-10-18T19:44:00.915"));
}
@Test
void zonedDateTime() {
var instant = Instant.ofEpochMilli(1729280640915L);
when(clock.instant()).thenReturn(instant);
when(clock.getZone()).thenReturn(ZoneId.of("UTC"));
assertThat(clockService.zonedDateTime())
.as("ZonedDateTime should match expected value")
.isEqualTo(ZonedDateTime.parse("2024-10-18T19:44:00.915Z[UTC]"));
}
}