/** * Obtains an instance of {@code ZoneOffset} specifying the total offset in seconds * <p> * The offset must be in the range {@code -18:00} to {@code +18:00}, which corresponds to -64800 to +64800. * * @param totalSeconds the total time-zone offset in seconds, from -64800 to +64800 * @return the ZoneOffset, not null * @throws DateTimeException if the offset is not in the required range */ public static ZoneOffset ofTotalSeconds(int totalSeconds) { if (Math.abs(totalSeconds) > MAX_SECONDS) { throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00"); } if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) { Integer totalSecs = totalSeconds; ZoneOffset result = SECONDS_CACHE.get(totalSecs); if (result == null) { result = new ZoneOffset(totalSeconds); SECONDS_CACHE.putIfAbsent(totalSecs, result); result = SECONDS_CACHE.get(totalSecs); ID_CACHE.putIfAbsent(result.getId(), result); } return result; } else { return new ZoneOffset(totalSeconds); } }
/** * Obtains an instance of {@code ZoneOffset} specifying the total offset in seconds * <p> * The offset must be in the range {@code -18:00} to {@code +18:00}, which corresponds to -64800 to +64800. * * @param totalSeconds the total time-zone offset in seconds, from -64800 to +64800 * @return the ZoneOffset, not null * @throws DateTimeException if the offset is not in the required range */ public static ZoneOffset ofTotalSeconds(int totalSeconds) { if (Math.abs(totalSeconds) > MAX_SECONDS) { throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00"); } if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) { Integer totalSecs = totalSeconds; ZoneOffset result = SECONDS_CACHE.get(totalSecs); if (result == null) { result = new ZoneOffset(totalSeconds); SECONDS_CACHE.putIfAbsent(totalSecs, result); result = SECONDS_CACHE.get(totalSecs); ID_CACHE.putIfAbsent(result.getId(), result); } return result; } else { return new ZoneOffset(totalSeconds); } }
private static void writeTimezone(final int position, final OffsetDateTime dt, final JsonWriter sw) { final ZoneOffset zone = dt.getOffset(); sw.advance(position); sw.writeAscii(zone.getId()); sw.writeByte(JsonWriter.QUOTE); }
/** * Obtains an instance of {@code ZoneId} wrapping an offset. * <p> * If the prefix is "GMT", "UTC", or "UT" a {@code ZoneId} * with the prefix and the non-zero offset is returned. * If the prefix is empty {@code ""} the {@code ZoneOffset} is returned. * * @param prefix the time-zone ID, not null * @param offset the offset, not null * @return the zone ID, not null * @throws IllegalArgumentException if the prefix is not one of * "GMT", "UTC", or "UT", or "" */ public static ZoneId ofOffset(String prefix, ZoneOffset offset) { Jdk8Methods.requireNonNull(prefix, "prefix"); Jdk8Methods.requireNonNull(offset, "offset"); if (prefix.length() == 0) { return offset; } if (prefix.equals("GMT") || prefix.equals("UTC") || prefix.equals("UT")) { if (offset.getTotalSeconds() == 0) { return new ZoneRegion(prefix, offset.getRules()); } return new ZoneRegion(prefix + offset.getId(), offset.getRules()); } throw new IllegalArgumentException("Invalid prefix, must be GMT, UTC or UT: " + prefix); }
/** * Obtains an instance of {@code ZoneId} wrapping an offset. * <p> * If the prefix is "GMT", "UTC", or "UT" a {@code ZoneId} * with the prefix and the non-zero offset is returned. * If the prefix is empty {@code ""} the {@code ZoneOffset} is returned. * * @param prefix the time-zone ID, not null * @param offset the offset, not null * @return the zone ID, not null * @throws IllegalArgumentException if the prefix is not one of * "GMT", "UTC", or "UT", or "" */ public static ZoneId ofOffset(String prefix, ZoneOffset offset) { Jdk8Methods.requireNonNull(prefix, "prefix"); Jdk8Methods.requireNonNull(offset, "offset"); if (prefix.length() == 0) { return offset; } if (prefix.equals("GMT") || prefix.equals("UTC") || prefix.equals("UT")) { if (offset.getTotalSeconds() == 0) { return new ZoneRegion(prefix, offset.getRules()); } return new ZoneRegion(prefix + offset.getId(), offset.getRules()); } throw new IllegalArgumentException("Invalid prefix, must be GMT, UTC or UT: " + prefix); }