Search in sources :

Example 36 with ZoneRules

use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.

the class ZonedDateTime method create.

/**
     * 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();
    // TODO: rules should be queryable by epochSeconds
    Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond);
    ZoneOffset offset = rules.getOffset(instant);
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
    return new ZonedDateTime(ldt, offset, zone);
}
Also used : ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) ZoneRules(java.time.zone.ZoneRules)

Example 37 with ZoneRules

use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.

the class LocalDate method atStartOfDay.

/**
     * Returns a zoned date-time from this date at the earliest valid time according
     * to the rules in the time-zone.
     * <p>
     * Time-zone rules, such as daylight savings, mean that not every local date-time
     * is valid for the specified zone, thus the local date-time may not be midnight.
     * <p>
     * In most cases, there is only one valid offset for a local date-time.
     * In the case of an overlap, there are two valid offsets, and the earlier one is used,
     * corresponding to the first occurrence of midnight on the date.
     * In the case of a gap, the zoned date-time will represent the instant just after the gap.
     * <p>
     * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
     * <p>
     * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
     * followed by {@link LocalDateTime#atZone(ZoneId)}.
     *
     * @param zone  the zone ID to use, not null
     * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
     */
public ZonedDateTime atStartOfDay(ZoneId zone) {
    Objects.requireNonNull(zone, "zone");
    // need to handle case where there is a gap from 11:30 to 00:30
    // standard ZDT factory would result in 01:00 rather than 00:30
    LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
    if (zone instanceof ZoneOffset == false) {
        ZoneRules rules = zone.getRules();
        ZoneOffsetTransition trans = rules.getTransition(ldt);
        if (trans != null && trans.isGap()) {
            ldt = trans.getDateTimeAfter();
        }
    }
    return ZonedDateTime.of(ldt, zone);
}
Also used : ZoneRules(java.time.zone.ZoneRules) ZoneOffsetTransition(java.time.zone.ZoneOffsetTransition)

Example 38 with ZoneRules

use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.

the class LocalDateTime method ofInstant.

//-------------------------------------------------------------------------
/**
     * 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);
}
Also used : ZoneRules(java.time.zone.ZoneRules)

Example 39 with ZoneRules

use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.

the class OffsetDateTime method ofInstant.

//-----------------------------------------------------------------------
/**
     * 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);
}
Also used : ZoneRules(java.time.zone.ZoneRules)

Example 40 with ZoneRules

use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.

the class TCKZoneRules method test_London_preTimeZones.

public void test_London_preTimeZones() {
    ZoneRules test = europeLondon();
    ZonedDateTime old = createZDT(1800, 1, 1, ZoneOffset.UTC);
    Instant instant = old.toInstant();
    ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(0, -1, -15);
    assertEquals(test.getOffset(instant), offset);
    checkOffset(test, old.toLocalDateTime(), offset, 1);
    assertEquals(test.getStandardOffset(instant), offset);
    assertEquals(test.getDaylightSavings(instant), Duration.ZERO);
    assertEquals(test.isDaylightSavings(instant), false);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant) ZoneRules(java.time.zone.ZoneRules) ZoneOffset(java.time.ZoneOffset)

Aggregations

ZoneRules (java.time.zone.ZoneRules)68 ZoneOffsetTransition (java.time.zone.ZoneOffsetTransition)22 LocalDateTime (java.time.LocalDateTime)10 Test (org.testng.annotations.Test)10 Instant (java.time.Instant)9 ZoneOffset (java.time.ZoneOffset)9 ZonedDateTime (java.time.ZonedDateTime)9 ZoneOffsetTransitionRule (java.time.zone.ZoneOffsetTransitionRule)5 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 OffsetDateTime (java.time.OffsetDateTime)1 TimeDefinition (java.time.zone.ZoneOffsetTransitionRule.TimeDefinition)1 ZoneRulesException (java.time.zone.ZoneRulesException)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1