java.time.zone.ZoneRules#getOffset ( )源码实例Demo

下面列出了java.time.zone.ZoneRules#getOffset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: j2objc   文件: ChronoZonedDateTimeImpl.java
/**
 * 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);
}
 
源代码2 项目: jphp   文件: ZoneIdFactory.java
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;
}
 
源代码3 项目: openjdk-8   文件: ChronoZonedDateTimeImpl.java
/**
 * 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);
}
 
源代码4 项目: TencentKona-8   文件: ChronoZonedDateTimeImpl.java
/**
 * 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);
}
 
/**
 * 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);
}
 
源代码6 项目: jdk1.8-source-analysis   文件: OffsetTime.java
/**
 * 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);
}
 
源代码7 项目: jdk1.8-source-analysis   文件: OffsetDateTime.java
/**
 * 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);
}
 
源代码8 项目: desugar_jdk_libs   文件: LocalDateTime.java
/**
 * 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);
}
 
源代码9 项目: j2objc   文件: LocalDateTime.java
/**
 * 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);
}
 
源代码10 项目: jdk8u-jdk   文件: ZonedDateTime.java
/**
 * 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);
}
 
源代码11 项目: desugar_jdk_libs   文件: ZonedDateTime.java
/**
 * 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);
}
 
源代码12 项目: dragonwell8_jdk   文件: ZoneId.java
/**
 * 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;
}
 
源代码13 项目: openjdk-8   文件: ZoneId.java
/**
 * 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;
}
 
源代码14 项目: openjdk-8-source   文件: ZoneId.java
/**
 * 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;
}
 
源代码15 项目: hottub   文件: ZonedDateTime.java
/**
 * 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);
}
 
源代码16 项目: TencentKona-8   文件: OffsetDateTime.java
/**
 * 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);
}
 
源代码17 项目: j2objc   文件: OffsetDateTime.java
/**
 * 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);
}
 
源代码18 项目: JDKSourceCode1.8   文件: OffsetDateTime.java
/**
 * 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);
}
 
源代码19 项目: TencentKona-8   文件: ZonedDateTime.java
/**
 * 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);
}
 
源代码20 项目: openjdk-8   文件: OffsetDateTime.java
/**
 * 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);
}