/** * Writes the state to the stream. * * @param offset the offset, not null * @param out the output stream, not null * @throws IOException if an error occurs */ static void writeOffset(ZoneOffset offset, DataOutput out) throws IOException { final int offsetSecs = offset.getTotalSeconds(); int offsetByte = offsetSecs % 900 == 0 ? offsetSecs / 900 : 127; // compress to -72 to +72 out.writeByte(offsetByte); if (offsetByte == 127) { out.writeInt(offsetSecs); } }
/** * Creates the wall offset for the local date-time at the end of the window. * * @param savingsSecs the amount of savings in use in seconds * @return the created date-time epoch second in the wall offset, not null */ ZoneOffset createWallOffset(int savingsSecs) { return ZoneOffset.ofTotalSeconds(standardOffset.getTotalSeconds() + savingsSecs); }
/** * Gets the duration of the transition in seconds. * * @return the duration in seconds */ private int getDurationSeconds() { return getOffsetAfter().getTotalSeconds() - getOffsetBefore().getTotalSeconds(); }
/** * Does this transition represent a gap in the local time-line. * <p> * Overlaps occur where there are local date-times that exist twice. * An example would be when the offset changes from {@code +02:00} to {@code +01:00}. * This might be described as 'the clocks will move back one hour tonight at 2am'. * * @return true if this transition is an overlap, false if it is a gap */ public boolean isOverlap() { return getOffsetAfter().getTotalSeconds() < getOffsetBefore().getTotalSeconds(); }
/** * Converts this time to epoch nanos based on 1970-01-01Z. * * @return the epoch nanos value */ private long toEpochNano() { long nod = time.toNanoOfDay(); long offsetNanos = offset.getTotalSeconds() * NANOS_PER_SECOND; return nod - offsetNanos; }
/** * Creates the wall offset for the local date-time at the end of the window. * * @param savingsSecs the amount of savings in use in seconds * @return the created date-time epoch second in the wall offset, not null */ ZoneOffset createWallOffset(int savingsSecs) { return ZoneOffset.ofTotalSeconds(standardOffset.getTotalSeconds() + savingsSecs); }
/** * Does this transition represent a gap in the local time-line. * <p> * Gaps occur where there are local date-times that simply do not not exist. * An example would be when the offset changes from {@code +01:00} to {@code +02:00}. * This might be described as 'the clocks will move forward one hour tonight at 1am'. * * @return true if this transition is a gap, false if it is an overlap */ public boolean isGap() { return getOffsetAfter().getTotalSeconds() > getOffsetBefore().getTotalSeconds(); }
/** * Does this transition represent a gap in the local time-line. * <p> * Gaps occur where there are local date-times that simply do not not exist. * An example would be when the offset changes from {@code +01:00} to {@code +02:00}. * This might be described as 'the clocks will move forward one hour tonight at 1am'. * * @return true if this transition is a gap, false if it is an overlap */ public boolean isGap() { return getOffsetAfter().getTotalSeconds() > getOffsetBefore().getTotalSeconds(); }
/** * Converts this time to epoch nanos based on 1970-01-01Z. * * @return the epoch nanos value */ private long toEpochNano() { long nod = time.toNanoOfDay(); long offsetNanos = offset.getTotalSeconds() * NANOS_PER_SECOND; return nod - offsetNanos; }
/** * Gets the duration of the transition in seconds. * * @return the duration in seconds */ private int getDurationSeconds() { return getOffsetAfter().getTotalSeconds() - getOffsetBefore().getTotalSeconds(); }
/** * Does this transition represent a gap in the local time-line. * <p> * Overlaps occur where there are local date-times that exist twice. * An example would be when the offset changes from {@code +02:00} to {@code +01:00}. * This might be described as 'the clocks will move back one hour tonight at 2am'. * * @return true if this transition is an overlap, false if it is a gap */ public boolean isOverlap() { return getOffsetAfter().getTotalSeconds() < getOffsetBefore().getTotalSeconds(); }
@Override public Duration getDaylightSavings(Instant instant) { ZoneOffset standardOffset = getStandardOffset(instant); ZoneOffset actualOffset = getOffset(instant); return Duration.ofSeconds(actualOffset.getTotalSeconds() - standardOffset.getTotalSeconds()); }
@Override public Duration getDaylightSavings(Instant instant) { ZoneOffset standardOffset = getStandardOffset(instant); ZoneOffset actualOffset = getOffset(instant); return Duration.ofSeconds(actualOffset.getTotalSeconds() - standardOffset.getTotalSeconds()); }
private int findYear(long epochSecond, ZoneOffset offset) { // inline for performance long localSecond = epochSecond + offset.getTotalSeconds(); long localEpochDay = Jdk8Methods.floorDiv(localSecond, 86400); return LocalDate.ofEpochDay(localEpochDay).getYear(); }
private int findYear(long epochSecond, ZoneOffset offset) { // inline for performance long localSecond = epochSecond + offset.getTotalSeconds(); long localEpochDay = Jdk8Methods.floorDiv(localSecond, 86400); return LocalDate.ofEpochDay(localEpochDay).getYear(); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().getLong(field); } return field.getFrom(this); }
@Override public int get(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new UnsupportedTemporalTypeException("Field too large for an int: " + field); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().get(field); } return super.get(field); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().getLong(field); } return field.getFrom(this); }
@Override public int get(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new UnsupportedTemporalTypeException("Field too large for an int: " + field); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().get(field); } return super.get(field); }
/** * Converts this date-time to the number of seconds from the epoch * of 1970-01-01T00:00:00Z. * <p> * This combines this local date-time and the specified offset to calculate the * epoch-second value, which is the number of elapsed seconds from 1970-01-01T00:00:00Z. * Instants on the time-line after the epoch are positive, earlier are negative. * * @param offset the offset to use for the conversion, not null * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z */ public long toEpochSecond(ZoneOffset offset) { Jdk8Methods.requireNonNull(offset, "offset"); long epochDay = toLocalDate().toEpochDay(); long secs = epochDay * 86400 + toLocalTime().toSecondOfDay(); secs -= offset.getTotalSeconds(); return secs; }