|
|
@@ -4,10 +4,10 @@ import lombok.RequiredArgsConstructor;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
import java.time.Duration;
|
|
|
-import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
import java.time.ZoneId;
|
|
|
import java.time.ZonedDateTime;
|
|
|
+import java.time.temporal.ChronoField;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
|
|
public abstract class ZonedTime implements Comparable<ZonedTime> {
|
|
|
@@ -37,23 +37,38 @@ public abstract class ZonedTime implements Comparable<ZonedTime> {
|
|
|
|
|
|
public abstract long toNanoOfDay();
|
|
|
|
|
|
+ @ZoneHazard
|
|
|
public abstract boolean isAfter(ZonedTime other);
|
|
|
|
|
|
- public abstract boolean isAfter(ZonedDateTime other);
|
|
|
+ public final boolean isAfter(ZonedDateTime other) {
|
|
|
+ return toNanoOfDay() > other.withZoneSameInstant(getZone()).toLocalTime().toNanoOfDay();
|
|
|
+ }
|
|
|
|
|
|
+ @ZoneHazard
|
|
|
public abstract boolean isBefore(ZonedTime other);
|
|
|
|
|
|
- public abstract boolean isBefore(ZonedDateTime other);
|
|
|
+ public final boolean isBefore(ZonedDateTime other) {
|
|
|
+ return toNanoOfDay() < other.withZoneSameInstant(getZone()).toLocalTime().toNanoOfDay();
|
|
|
+ }
|
|
|
|
|
|
+ @ZoneHazard
|
|
|
public abstract boolean isEqual(ZonedTime other);
|
|
|
|
|
|
- public abstract boolean isEqualTime(ZonedDateTime other);
|
|
|
+ public final boolean isEqualTime(ZonedDateTime other) {
|
|
|
+ return toNanoOfDay() == other.withZoneSameInstant(getZone()).toLocalTime().toNanoOfDay();
|
|
|
+ }
|
|
|
|
|
|
- public abstract boolean isSameZone(ZonedTime other);
|
|
|
+ public final boolean isSameZone(ZonedTime other) {
|
|
|
+ return getZone().equals(other.getZone());
|
|
|
+ }
|
|
|
|
|
|
- public abstract boolean isSameZone(ZonedDateTime other);
|
|
|
+ public final boolean isSameZone(ZonedDateTime other) {
|
|
|
+ return getZone().equals(other.getZone());
|
|
|
+ }
|
|
|
|
|
|
- public abstract boolean isSameZone(ZoneId other);
|
|
|
+ public final boolean isSameZone(ZoneId other) {
|
|
|
+ return getZone().equals(other);
|
|
|
+ }
|
|
|
|
|
|
public abstract boolean isZoneConvertible(ZoneId zone);
|
|
|
|
|
|
@@ -64,15 +79,24 @@ public abstract class ZonedTime implements Comparable<ZonedTime> {
|
|
|
public abstract ZonedDateTime withDate(ZonedDate time);
|
|
|
|
|
|
public ZonedDateTime withDate(ZonedDateTime datetime) {
|
|
|
- return withDate(ZonedDate.of(datetime).withZone(getZone()));
|
|
|
+ return withDate(ZonedDate.of(datetime));
|
|
|
}
|
|
|
|
|
|
- public abstract Duration until(ZonedDateTime end);
|
|
|
+ public final Duration until(ZonedDateTime end) {
|
|
|
+ return Duration.ofNanos(until(end, ChronoUnit.NANOS));
|
|
|
+ }
|
|
|
|
|
|
- public abstract long until(ZonedDateTime end, ChronoUnit unit);
|
|
|
+ public final long until(ZonedDateTime end, ChronoUnit unit) {
|
|
|
+ long nanos = end.withZoneSameInstant(getZone()).toLocalTime().toNanoOfDay() - toNanoOfDay();
|
|
|
+ return nanos / unit.getDuration().toNanos();
|
|
|
+ }
|
|
|
|
|
|
- public abstract Duration until(ZonedTime end);
|
|
|
+ @ZoneHazard
|
|
|
+ public final Duration until(ZonedTime end) {
|
|
|
+ return Duration.ofNanos(until(end, ChronoUnit.NANOS));
|
|
|
+ }
|
|
|
|
|
|
+ @ZoneHazard
|
|
|
public abstract long until(ZonedTime end, ChronoUnit unit);
|
|
|
|
|
|
@ZoneHazard
|
|
|
@@ -87,6 +111,11 @@ public abstract class ZonedTime implements Comparable<ZonedTime> {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return DateTimeFormatters.formatTime(withDate(ZonedDateTime.now())) + " [" + getZone() + "]";
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* This wrapper allows all operations even if timezones do not match.
|
|
|
* Because we have full datetime, we can convert between timezones safely.
|
|
|
@@ -103,102 +132,63 @@ public abstract class ZonedTime implements Comparable<ZonedTime> {
|
|
|
|
|
|
@Override
|
|
|
public boolean isAfter(ZonedTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isAfter(ZonedDateTime other) {
|
|
|
- return false;
|
|
|
+ return withZone(other.getZone()).toNanoOfDay() > other.toNanoOfDay();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isBefore(ZonedTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isBefore(ZonedDateTime other) {
|
|
|
- return false;
|
|
|
+ return withZone(other.getZone()).toNanoOfDay() < other.toNanoOfDay();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isEqual(ZonedTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isEqualTime(ZonedDateTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isSameZone(ZonedTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isSameZone(ZonedDateTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isSameZone(ZoneId other) {
|
|
|
- return false;
|
|
|
+ return withZone(other.getZone()).toNanoOfDay() == other.toNanoOfDay();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isZoneConvertible(ZoneId zone) {
|
|
|
- return false;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ZonedTime withZone(ZoneId zone) {
|
|
|
- return null;
|
|
|
+ return new Complete(value.withZoneSameInstant(zone));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ZonedDateTime withDate(ZonedDate time) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Duration until(ZonedDateTime end) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public long until(ZonedDateTime end, ChronoUnit unit) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Duration until(ZonedTime end) {
|
|
|
- return null;
|
|
|
+ public ZonedDateTime withDate(ZonedDate date) {
|
|
|
+ return value
|
|
|
+ .withZoneSameInstant(date.getZone())
|
|
|
+ .withYear(date.getYear())
|
|
|
+ .withMonth(date.getMonth())
|
|
|
+ .withDayOfMonth(date.getDay())
|
|
|
+ .withZoneSameInstant(getZone());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public long until(ZonedTime end, ChronoUnit unit) {
|
|
|
- return 0;
|
|
|
+ long nanos = end.toNanoOfDay() - withZone(end.getZone()).toNanoOfDay();
|
|
|
+ return nanos / unit.getDuration().toNanos();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getHour() {
|
|
|
- return 0;
|
|
|
+ return value.getHour();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getMinute() {
|
|
|
- return 0;
|
|
|
+ return value.getMinute();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getSecond() {
|
|
|
- return 0;
|
|
|
+ return value.getSecond();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getNano() {
|
|
|
- return 0;
|
|
|
+ return value.getNano();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -220,52 +210,22 @@ public abstract class ZonedTime implements Comparable<ZonedTime> {
|
|
|
|
|
|
@Override
|
|
|
public ZoneId getZone() {
|
|
|
- return null;
|
|
|
+ return zone;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isAfter(ZonedTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isAfter(ZonedDateTime other) {
|
|
|
- return false;
|
|
|
+ return toNanoOfDay() > other.withZone(getZone()).toNanoOfDay();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isBefore(ZonedTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isBefore(ZonedDateTime other) {
|
|
|
- return false;
|
|
|
+ return toNanoOfDay() < other.withZone(getZone()).toNanoOfDay();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isEqual(ZonedTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isEqualTime(ZonedDateTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isSameZone(ZonedTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isSameZone(ZonedDateTime other) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isSameZone(ZoneId other) {
|
|
|
- return false;
|
|
|
+ return toNanoOfDay() == other.withZone(getZone()).toNanoOfDay();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -275,52 +235,45 @@ public abstract class ZonedTime implements Comparable<ZonedTime> {
|
|
|
|
|
|
@Override
|
|
|
public ZonedTime withZone(ZoneId zone) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ZonedDateTime withDate(ZonedDate time) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Duration until(ZonedDateTime end) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public long until(ZonedDateTime end, ChronoUnit unit) {
|
|
|
- return 0;
|
|
|
+ if(!isSameZone(zone)) {
|
|
|
+ throw new UnsupportedOperationException("This time " + this + " can't be converted to " + zone);
|
|
|
+ } else {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Duration until(ZonedTime end) {
|
|
|
- return null;
|
|
|
+ public ZonedDateTime withDate(ZonedDate date) {
|
|
|
+ return date
|
|
|
+ .withZone(getZone())
|
|
|
+ .withZero()
|
|
|
+ .with(ChronoField.NANO_OF_DAY, toNanoOfDay());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public long until(ZonedTime end, ChronoUnit unit) {
|
|
|
- return 0;
|
|
|
+ long nanos = end.withZone(getZone()).toNanoOfDay() - toNanoOfDay();
|
|
|
+ return nanos / unit.getDuration().toNanos();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getHour() {
|
|
|
- return 0;
|
|
|
+ return value.getHour();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getMinute() {
|
|
|
- return 0;
|
|
|
+ return value.getMinute();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getSecond() {
|
|
|
- return 0;
|
|
|
+ return value.getSecond();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getNano() {
|
|
|
- return 0;
|
|
|
+ return value.getNano();
|
|
|
}
|
|
|
|
|
|
@Override
|