Skip to content

Commit

Permalink
use UTC to in optimal time comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbehr801 committed Dec 5, 2024
1 parent 09f5c64 commit 01f23c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import io.github.domainprimitives.validation.Validation;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.Temporal;
import java.util.function.Consumer;


public class OptimalTime extends ValueObject<Temporal> {

private static Consumer<Validation<Temporal>> isNotNull() {
return (val) -> val.constraint(val.value() != null, () -> "should not be null");
return val -> val.constraint(val.value() != null, () -> "should not be null");
}

public OptimalTime(OffsetDateTime value) {
Expand All @@ -24,6 +25,6 @@ public OffsetDateTime asOffsetDateTime() {

public boolean isInFuture() {
// TODO: Better Solution to handle "now == now"?
return this.asOffsetDateTime().isAfter(OffsetDateTime.now().minusMinutes(1));
return this.asOffsetDateTime().isAfter(OffsetDateTime.now(ZoneOffset.UTC).minusMinutes(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
import org.junit.jupiter.api.Test;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

class OptimalTimeTest {

private final OffsetDateTime utcNow = OffsetDateTime.now(ZoneOffset.UTC);

@Test
void should_not_throw_if_valid() {
assertDoesNotThrow(() -> new OptimalTime(OffsetDateTime.now()));
assertDoesNotThrow(() -> new OptimalTime(utcNow));
}

@Nested
class ConvertToOffsetDateTime {

@Test
void should_convert() {
final OffsetDateTime offsetDateTime = OffsetDateTime.now();

final OptimalTime optimalTime = new OptimalTime(offsetDateTime);
final OptimalTime optimalTime = new OptimalTime(utcNow);

SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(optimalTime.asOffsetDateTime()).isEqualTo(offsetDateTime);
softAssertions.assertThat(optimalTime.asOffsetDateTime()).isEqualTo(utcNow);
softAssertions.assertThat(optimalTime.asOffsetDateTime()).isInstanceOf(OffsetDateTime.class);
softAssertions.assertAll();
}
Expand All @@ -40,21 +40,21 @@ class IsInFuture {

@Test
void should_return_true_if_is_in_future() {
final OptimalTime nextHour = new OptimalTime(OffsetDateTime.now().plusHours(1));
final OptimalTime nextHour = new OptimalTime(utcNow.plusHours(1));

assertThat(nextHour.isInFuture()).isTrue();
}

@Test
void should_return_true_if_is_in_now() {
final OptimalTime now = new OptimalTime(OffsetDateTime.now());
final OptimalTime now = new OptimalTime(utcNow);

assertThat(now.isInFuture()).isTrue();
}

@Test
void should_return_false_if_is_in_past() {
final OptimalTime lastHour = new OptimalTime(OffsetDateTime.now().minusHours(1));
final OptimalTime lastHour = new OptimalTime(utcNow.minusHours(1));

assertThat(lastHour.isInFuture()).isFalse();
}
Expand Down

0 comments on commit 01f23c7

Please sign in to comment.