Java Code Examples for java.time.zone.ZoneRules#getOffset()

The following examples show how to use java.time.zone.ZoneRules#getOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChronoZonedDateTimeImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
 
Example 2
Source File: ZoneIdFactory.java    From jphp with Apache License 2.0 5 votes vote down vote up
public static String aliasFor(ZonedDateTime dateTime) {
    String id = dateTime.getZone().getId();
    switch (id) {
        case "GMT":
        case "UTC":
            return id;
    }

    List<TimezoneWithAlias> aliases = idToAliases.get(id);
    if (aliases == null) {
        return null;
    }

    ZoneId zone = dateTime.getZone();
    ZoneRules rules = zone.getRules();
    boolean dst = zone.getRules().isDaylightSavings(dateTime.toInstant());
    ZoneOffset zoneOffset = rules.getOffset(dateTime.toInstant());

    int offset = zoneOffset.getTotalSeconds();

    for (TimezoneWithAlias alias : aliases) {
        if (alias.timezone.getOffset() == offset && alias.timezone.isDst() == dst) {
            return alias.alias.toUpperCase();
        }
    }

    return null;
}
 
Example 3
Source File: ChronoZonedDateTimeImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
 
Example 4
Source File: ChronoZonedDateTimeImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
 
Example 5
Source File: ChronoZonedDateTimeImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
 
Example 6
Source File: ZoneId.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible.
 * <p>
 * The returns a normalized {@code ZoneId} that can be used in place of this ID.
 * The result will have {@code ZoneRules} equivalent to those returned by this object,
 * however the ID returned by {@code getId()} may be different.
 * <p>
 * The normalization checks if the rules of this {@code ZoneId} have a fixed offset.
 * If they do, then the {@code ZoneOffset} equal to that offset is returned.
 * Otherwise {@code this} is returned.
 *
 * @return the time-zone unique ID, not null
 */
public ZoneId normalized() {
    try {
        ZoneRules rules = getRules();
        if (rules.isFixedOffset()) {
            return rules.getOffset(Instant.EPOCH);
        }
    } catch (ZoneRulesException ex) {
        // invalid ZoneRegion is not important to this method
    }
    return this;
}
 
Example 7
Source File: OffsetDateTime.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset date-time with the same instant as that specified.
 * Finding the offset from UTC/Greenwich is simple as there is only one valid
 * offset for each instant.
 *
 * @param instant  the instant to create the date-time from, not null
 * @param zone  the time-zone, which may be an offset, not null
 * @return the offset date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
    Objects.requireNonNull(instant, "instant");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    return new OffsetDateTime(ldt, offset);
}
 
Example 8
Source File: ZonedDateTime.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} using seconds from the
 * epoch of 1970-01-01T00:00:00Z.
 *
 * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 * @param zone  the time-zone, not null
 * @return the zoned date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond);  // TODO: rules should be queryable by epochSeconds
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
    return new ZonedDateTime(ldt, offset, zone);
}
 
Example 9
Source File: OffsetDateTime.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset date-time with the same instant as that specified.
 * Finding the offset from UTC/Greenwich is simple as there is only one valid
 * offset for each instant.
 *
 * @param instant  the instant to create the date-time from, not null
 * @param zone  the time-zone, which may be an offset, not null
 * @return the offset date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
    Objects.requireNonNull(instant, "instant");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    return new OffsetDateTime(ldt, offset);
}
 
Example 10
Source File: OffsetDateTime.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset date-time with the same instant as that specified.
 * Finding the offset from UTC/Greenwich is simple as there is only one valid
 * offset for each instant.
 *
 * @param instant  the instant to create the date-time from, not null
 * @param zone  the time-zone, which may be an offset, not null
 * @return the offset date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
    Objects.requireNonNull(instant, "instant");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    return new OffsetDateTime(ldt, offset);
}
 
Example 11
Source File: OffsetDateTime.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset date-time with the same instant as that specified.
 * Finding the offset from UTC/Greenwich is simple as there is only one valid
 * offset for each instant.
 *
 * @param instant  the instant to create the date-time from, not null
 * @param zone  the time-zone, which may be an offset, not null
 * @return the offset date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
    Objects.requireNonNull(instant, "instant");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    return new OffsetDateTime(ldt, offset);
}
 
Example 12
Source File: ZonedDateTime.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} using seconds from the
 * epoch of 1970-01-01T00:00:00Z.
 *
 * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 * @param zone  the time-zone, not null
 * @return the zoned date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond);  // TODO: rules should be queryable by epochSeconds
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
    return new ZonedDateTime(ldt, offset, zone);
}
 
Example 13
Source File: OffsetTime.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset time with the same instant as that specified.
 * Finding the offset from UTC/Greenwich is simple as there is only one valid
 * offset for each instant.
 * <p>
 * The date component of the instant is dropped during the conversion.
 * This means that the conversion can never fail due to the instant being
 * out of the valid range of dates.
 *
 * @param instant  the instant to create the time from, not null
 * @param zone  the time-zone, which may be an offset, not null
 * @return the offset time, not null
 */
public static OffsetTime ofInstant(Instant instant, ZoneId zone) {
    Objects.requireNonNull(instant, "instant");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
    int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);
    LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano());
    return new OffsetTime(time, offset);
}
 
Example 14
Source File: ZoneId.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible.
 * <p>
 * The returns a normalized {@code ZoneId} that can be used in place of this ID.
 * The result will have {@code ZoneRules} equivalent to those returned by this object,
 * however the ID returned by {@code getId()} may be different.
 * <p>
 * The normalization checks if the rules of this {@code ZoneId} have a fixed offset.
 * If they do, then the {@code ZoneOffset} equal to that offset is returned.
 * Otherwise {@code this} is returned.
 *
 * @return the time-zone unique ID, not null
 */
public ZoneId normalized() {
    try {
        ZoneRules rules = getRules();
        if (rules.isFixedOffset()) {
            return rules.getOffset(Instant.EPOCH);
        }
    } catch (ZoneRulesException ex) {
        // invalid ZoneRegion is not important to this method
    }
    return this;
}
 
Example 15
Source File: ZoneId.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible.
 * <p>
 * The returns a normalized {@code ZoneId} that can be used in place of this ID.
 * The result will have {@code ZoneRules} equivalent to those returned by this object,
 * however the ID returned by {@code getId()} may be different.
 * <p>
 * The normalization checks if the rules of this {@code ZoneId} have a fixed offset.
 * If they do, then the {@code ZoneOffset} equal to that offset is returned.
 * Otherwise {@code this} is returned.
 *
 * @return the time-zone unique ID, not null
 */
public ZoneId normalized() {
    try {
        ZoneRules rules = getRules();
        if (rules.isFixedOffset()) {
            return rules.getOffset(Instant.EPOCH);
        }
    } catch (ZoneRulesException ex) {
        // invalid ZoneRegion is not important to this method
    }
    return this;
}
 
Example 16
Source File: ZonedDateTime.java    From desugar_jdk_libs with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} using seconds from the
 * epoch of 1970-01-01T00:00:00Z.
 *
 * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 * @param zone  the time-zone, not null
 * @return the zoned date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond);  // TODO: rules should be queryable by epochSeconds
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
    return new ZonedDateTime(ldt, offset, zone);
}
 
Example 17
Source File: ZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} using seconds from the
 * epoch of 1970-01-01T00:00:00Z.
 *
 * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 * @param zone  the time-zone, not null
 * @return the zoned date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond);  // TODO: rules should be queryable by epochSeconds
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
    return new ZonedDateTime(ldt, offset, zone);
}
 
Example 18
Source File: LocalDateTime.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates a local date-time based on the specified instant.
 * First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
 * which is simple as there is only one valid offset for each instant.
 * Then, the instant and offset are used to calculate the local date-time.
 *
 * @param instant  the instant to create the date-time from, not null
 * @param zone  the time-zone, which may be an offset, not null
 * @return the local date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static LocalDateTime ofInstant(Instant instant, ZoneId zone) {
    Objects.requireNonNull(instant, "instant");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
}
 
Example 19
Source File: LocalDateTime.java    From desugar_jdk_libs with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates a local date-time based on the specified instant.
 * First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
 * which is simple as there is only one valid offset for each instant.
 * Then, the instant and offset are used to calculate the local date-time.
 *
 * @param instant  the instant to create the date-time from, not null
 * @param zone  the time-zone, which may be an offset, not null
 * @return the local date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static LocalDateTime ofInstant(Instant instant, ZoneId zone) {
    Objects.requireNonNull(instant, "instant");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
}
 
Example 20
Source File: OffsetDateTime.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset date-time with the same instant as that specified.
 * Finding the offset from UTC/Greenwich is simple as there is only one valid
 * offset for each instant.
 *
 * @param instant  the instant to create the date-time from, not null
 * @param zone  the time-zone, which may be an offset, not null
 * @return the offset date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
    Objects.requireNonNull(instant, "instant");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    return new OffsetDateTime(ldt, offset);
}