diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TimeRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TimeRules.java index ba79bf2aff3..02cfe30b5ce 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TimeRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TimeRules.java @@ -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; @@ -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); } } @@ -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); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestInput.java index af0eefb3a68..0afc86a3152 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestInput.java @@ -36,24 +36,41 @@ ImmutableSet testUtcConstant() { ZoneId.from(ZoneOffset.UTC)); } - LocalDate testInstantToLocalDate() { - return LocalDate.ofInstant(Instant.EPOCH, ZoneOffset.UTC); + ImmutableSet 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 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 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 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() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestOutput.java index 9047a82cd70..bb4073e511b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestOutput.java @@ -36,24 +36,41 @@ ImmutableSet testUtcConstant() { ZoneOffset.UTC); } - LocalDate testInstantToLocalDate() { - return Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalDate(); + ImmutableSet 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 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 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 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() {