Skip to content

Commit

Permalink
Applied suggestion from @Stephan202
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolTomatos committed Oct 31, 2022
1 parent c4ecf06 commit 413ef61
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.chrono.ChronoLocalDate;
import java.time.chrono.ChronoLocalDateTime;
import java.time.chrono.ChronoZonedDateTime;
Expand Down Expand Up @@ -65,42 +66,62 @@ ZoneOffset after() {
}
}

/** Prefer more fluent way of converting an instant to a local date. */
static final class InstantToLocalDate {
@BeforeTemplate
LocalDate before(Instant instant, ZoneOffset zoneOffset) {
return LocalDate.ofInstant(instant, zoneOffset);
LocalDate before(Instant instant, ZoneId zoneId) {
return Refaster.anyOf(
instant.atZone(zoneId).toLocalDate(),
instant.atZone(zoneId).toOffsetDateTime().toLocalDate(),
LocalDateTime.ofInstant(instant, zoneId).toLocalDate());
}

@BeforeTemplate
LocalDate before(Instant instant, ZoneOffset zoneId) {
return instant.atOffset(zoneId).toLocalDate();
}

@AfterTemplate
LocalDate after(Instant instant, ZoneOffset zoneOffset) {
return instant.atOffset(zoneOffset).toLocalDate();
LocalDate after(Instant instant, ZoneId zoneId) {
return LocalDate.ofInstant(instant, zoneId);
}
}

/** Prefer more fluent way of converting an instant to a local date time. */
static final class InstantToLocalDateTime {
@BeforeTemplate
LocalDateTime before(Instant instant, ZoneOffset zoneOffset) {
return LocalDateTime.ofInstant(instant, zoneOffset);
LocalDateTime before(Instant instant, ZoneId zoneId) {
return Refaster.anyOf(
instant.atZone(zoneId).toLocalDateTime(),
instant.atZone(zoneId).toOffsetDateTime().toLocalDateTime());
}

@BeforeTemplate
LocalDateTime before(Instant instant, ZoneOffset zoneId) {
return instant.atOffset(zoneId).toLocalDateTime();
}

@AfterTemplate
LocalDateTime after(Instant instant, ZoneOffset zoneOffset) {
return instant.atOffset(zoneOffset).toLocalDateTime();
LocalDateTime after(Instant instant, ZoneId zoneId) {
return LocalDateTime.ofInstant(instant, zoneId);
}
}

/** Prefer more fluent way of converting an instant to a local time. */
static final class InstantToLocalTime {
@BeforeTemplate
LocalTime before(Instant instant, ZoneOffset zoneOffset) {
return LocalTime.ofInstant(instant, zoneOffset);
LocalTime before(Instant instant, ZoneId zoneId) {
return Refaster.anyOf(
instant.atZone(zoneId).toLocalTime(),
instant.atZone(zoneId).toOffsetDateTime().toLocalTime(),
LocalDateTime.ofInstant(instant, zoneId).toLocalTime());
}

@BeforeTemplate
LocalTime before(Instant instant, ZoneOffset zoneId) {
return instant.atOffset(zoneId).toLocalTime();
}

@AfterTemplate
LocalTime after(Instant instant, ZoneOffset zoneOffset) {
return instant.atOffset(zoneOffset).toLocalTime();
LocalTime after(Instant instant, ZoneId zoneId) {
return LocalTime.ofInstant(instant, zoneId);
}
}

Expand All @@ -117,16 +138,33 @@ OffsetDateTime after(Instant instant, ZoneOffset zoneOffset) {
}
}

/** Prefer more fluent way of converting an instant to an offset time. */
static final class InstantToOffsetTime {
@BeforeTemplate
OffsetTime before(Instant instant, ZoneOffset zoneOffset) {
return OffsetTime.ofInstant(instant, zoneOffset);
OffsetTime before(Instant instant, ZoneId zoneId) {
return instant.atZone(zoneId).toOffsetDateTime().toOffsetTime();
}

@BeforeTemplate
OffsetTime before(Instant instant, ZoneOffset zoneId) {
return instant.atOffset(zoneId).toOffsetTime();
}

@AfterTemplate
OffsetTime after(Instant instant, ZoneId zoneId) {
return OffsetTime.ofInstant(instant, zoneId);
}
}

/** Prefer {@link Instant#atZone(ZoneId)} over the more verbose alternative. */
static final class InstantToZonedDateTime {
@BeforeTemplate
ZonedDateTime before(Instant instant, ZoneId zoneId) {
return ZonedDateTime.ofInstant(instant, zoneId);
}

@AfterTemplate
OffsetTime after(Instant instant, ZoneOffset zoneOffset) {
return instant.atOffset(zoneOffset).toOffsetTime();
ZonedDateTime after(Instant instant, ZoneId zoneId) {
return instant.atZone(zoneId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,41 @@ ImmutableSet<ZoneId> testUtcConstant() {
ZoneId.from(ZoneOffset.UTC));
}

LocalDate testInstantToLocalDate() {
return LocalDate.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
ImmutableSet<LocalDate> testInstantToLocalDate() {
return ImmutableSet.of(
Instant.EPOCH.atZone(ZoneId.of("Europe/Amsterdam")).toLocalDate(),
Instant.EPOCH.atZone(ZoneId.of("Europe/Paris")).toOffsetDateTime().toLocalDate(),
Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalDate(),
LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")).toLocalDate());
}

LocalDateTime testInstantToLocalDateTime() {
return LocalDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
ImmutableSet<LocalDateTime> testInstantToLocalDateTime() {
return ImmutableSet.of(
Instant.EPOCH.atZone(ZoneId.of("Europe/Amsterdam")).toLocalDateTime(),
Instant.EPOCH.atZone(ZoneId.of("Europe/Berlin")).toOffsetDateTime().toLocalDateTime(),
Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalDateTime());
}

LocalTime testInstantToLocalTime() {
return LocalTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
ImmutableSet<LocalTime> testInstantToLocalTime() {
return ImmutableSet.of(
Instant.EPOCH.atZone(ZoneId.of("Europe/Amsterdam")).toLocalTime(),
Instant.EPOCH.atZone(ZoneId.of("Europe/Paris")).toOffsetDateTime().toLocalTime(),
Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalTime(),
LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")).toLocalTime());
}

OffsetDateTime testInstantToOffsetDateTime() {
return OffsetDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
}

OffsetTime testInstantToOffsetTime() {
return OffsetTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
ImmutableSet<OffsetTime> testInstantToOffsetTime() {
return ImmutableSet.of(
Instant.EPOCH.atZone(ZoneId.of("Europe/Amsterdam")).toOffsetDateTime().toOffsetTime(),
Instant.EPOCH.atOffset(ZoneOffset.UTC).toOffsetTime());
}

ZonedDateTime testInstantToZonedDateTime() {
return ZonedDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
}

Clock testUtcClock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,41 @@ ImmutableSet<ZoneId> testUtcConstant() {
ZoneOffset.UTC);
}

LocalDate testInstantToLocalDate() {
return Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalDate();
ImmutableSet<LocalDate> testInstantToLocalDate() {
return ImmutableSet.of(
LocalDate.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Amsterdam")),
LocalDate.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Paris")),
LocalDate.ofInstant(Instant.EPOCH, ZoneOffset.UTC),
LocalDate.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")));
}

LocalDateTime testInstantToLocalDateTime() {
return Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalDateTime();
ImmutableSet<LocalDateTime> testInstantToLocalDateTime() {
return ImmutableSet.of(
LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Amsterdam")),
LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")),
LocalDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC));
}

LocalTime testInstantToLocalTime() {
return Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalTime();
ImmutableSet<LocalTime> testInstantToLocalTime() {
return ImmutableSet.of(
LocalTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Amsterdam")),
LocalTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Paris")),
LocalTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC),
LocalTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")));
}

OffsetDateTime testInstantToOffsetDateTime() {
return Instant.EPOCH.atOffset(ZoneOffset.UTC);
}

OffsetTime testInstantToOffsetTime() {
return Instant.EPOCH.atOffset(ZoneOffset.UTC).toOffsetTime();
ImmutableSet<OffsetTime> testInstantToOffsetTime() {
return ImmutableSet.of(
OffsetTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Amsterdam")),
OffsetTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC));
}

ZonedDateTime testInstantToZonedDateTime() {
return Instant.EPOCH.atZone(ZoneOffset.UTC);
}

Clock testUtcClock() {
Expand Down

0 comments on commit 413ef61

Please sign in to comment.