/** * 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); } }
/** * 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); }
return new ZoneRegion(zoneId, ZoneOffset.UTC.getRules()); ZoneOffset offset = ZoneOffset.of(zoneId.substring(3)); if (offset.getTotalSeconds() == 0) { return new ZoneRegion(zoneId.substring(0, 3), offset.getRules()); return new ZoneRegion(zoneId.substring(0, 3) + offset.getId(), offset.getRules()); ZoneOffset offset = ZoneOffset.of(zoneId.substring(2)); if (offset.getTotalSeconds() == 0) { return new ZoneRegion("UT", offset.getRules()); return new ZoneRegion("UT" + offset.getId(), offset.getRules());
/** * 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); }
/** * Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30+01:00}. * <p> * The output will be one of the following ISO-8601 formats: * <p><ul> * <li>{@code yyyy-MM-dd'T'HH:mmXXXXX}</li> * <li>{@code yyyy-MM-dd'T'HH:mm:ssXXXXX}</li> * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX}</li> * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXXXX}</li> * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX}</li> * </ul><p> * The format used will be the shortest that outputs the full value of * the time where the omitted parts are implied to be zero. * * @return a string representation of this date-time, not null */ @Override public String toString() { return dateTime.toString() + offset.toString(); }
@Override public boolean isValidOffset(LocalDateTime dateTime, ZoneOffset offset) { return this.offset.equals(offset); }
@Override protected ZoneOffset deserialize(String key, DeserializationContext ctxt) throws IOException { try { return ZoneOffset.of(key); } catch (DateTimeException e) { return _rethrowDateTimeException(ctxt, ZoneOffset.class, e, key); } } }
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); }
@Override public ZoneOffset queryFrom(TemporalAccessor temporal) { return ZoneOffset.from(temporal); } };
/** * Constructor. * * @param totalSeconds the total time-zone offset in seconds, from -64800 to +64800 */ private ZoneOffset(int totalSeconds) { super(); this.totalSeconds = totalSeconds; id = buildId(totalSeconds); }
StringBuilder buf = new StringBuilder(); buf.append("TransitionRule[") .append(offsetBefore.compareTo(offsetAfter) > 0 ? "Gap " : "Overlap ") .append(offsetBefore).append(" to ").append(offsetAfter).append(", "); if (dow != null) {
return new ZoneRegion(zoneId, ZoneOffset.UTC.getRules()); ZoneOffset offset = ZoneOffset.of(zoneId.substring(3)); if (offset.getTotalSeconds() == 0) { return new ZoneRegion(zoneId.substring(0, 3), offset.getRules()); return new ZoneRegion(zoneId.substring(0, 3) + offset.getId(), offset.getRules()); ZoneOffset offset = ZoneOffset.of(zoneId.substring(2)); if (offset.getTotalSeconds() == 0) { return new ZoneRegion("UT", offset.getRules()); return new ZoneRegion("UT" + offset.getId(), offset.getRules());
/** * 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); }
/** * Outputs this time as a {@code String}, such as {@code 10:15:30+01:00}. * <p> * The output will be one of the following ISO-8601 formats: * <p><ul> * <li>{@code HH:mmXXXXX}</li> * <li>{@code HH:mm:ssXXXXX}</li> * <li>{@code HH:mm:ss.SSSXXXXX}</li> * <li>{@code HH:mm:ss.SSSSSSXXXXX}</li> * <li>{@code HH:mm:ss.SSSSSSSSSXXXXX}</li> * </ul><p> * The format used will be the shortest that outputs the full value of * the time where the omitted parts are implied to be zero. * * @return a string representation of this time, not null */ @Override public String toString() { return time.toString() + offset.toString(); }
/** * 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); }
@Override public boolean isValidOffset(LocalDateTime dateTime, ZoneOffset offset) { return this.offset.equals(offset); }
@Override public Object deserialize(JsonParser parser, DeserializationContext context) throws IOException { if (parser.hasToken(JsonToken.VALUE_STRING)) { String string = parser.getText().trim(); if (string.length() == 0) { return null; } try { switch (_valueType) { case TYPE_PERIOD: return Period.parse(string); case TYPE_ZONE_ID: return ZoneId.of(string); case TYPE_ZONE_OFFSET: return ZoneOffset.of(string); } } catch (DateTimeException e) { _rethrowDateTimeException(parser, context, e, string); } } if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { // 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded // values quite easily return parser.getEmbeddedObject(); } throw context.wrongTokenException(parser, JsonToken.VALUE_STRING, null); }
@Override public ZoneOffset queryFrom(TemporalAccessor temporal) { return ZoneOffset.from(temporal); } };