Java 类java.time.zone.ZoneRules 实例源码

项目:openjdk-jdk10    文件:ZonedDateTime.java   
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_NewYork_getOffsetInfo_overlap() {
    ZoneRules test = americaNewYork();
    final LocalDateTime dateTime = LocalDateTime.of(2008, 11, 2, 1, 0, 0, 0);
    ZoneOffsetTransition trans = checkOffset(test, dateTime, ZoneOffset.ofHours(-4), OVERLAP);
    assertEquals(trans.isGap(), false);
    assertEquals(trans.isOverlap(), true);
    assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(-4));
    assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(-5));
    assertEquals(trans.getInstant(), createInstant(2008, 11, 2, 2, 0, ZoneOffset.ofHours(-4)));
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-1)), false);
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-5)), true);
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-4)), true);
    assertEquals(trans.isValidOffset(OFFSET_PTWO), false);
    assertEquals(trans.toString(), "Transition[Overlap at 2008-11-02T02:00-04:00 to -05:00]");

    assertFalse(trans.equals(null));
    assertFalse(trans.equals(ZoneOffset.ofHours(-4)));
    assertTrue(trans.equals(trans));

    final ZoneOffsetTransition otherTrans = test.getTransition(dateTime);
    assertTrue(trans.equals(otherTrans));
    assertEquals(trans.hashCode(), otherTrans.hashCode());
}
项目:jdk8u-jdk    文件:TCKFixedZoneRules.java   
@Test
public void test_equalsHashCode() {
    ZoneRules a = make(OFFSET_PONE);
    ZoneRules b = make(OFFSET_PTWO);

    assertEquals(a.equals(a), true);
    assertEquals(a.equals(b), false);
    assertEquals(b.equals(a), false);
    assertEquals(b.equals(b), true);

    assertEquals(a.equals("Rubbish"), false);
    assertEquals(a.equals(null), false);

    assertEquals(a.hashCode() == a.hashCode(), true);
    assertEquals(b.hashCode() == b.hashCode(), true);
}
项目:openjdk-jdk10    文件:TestZoneId.java   
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_London_getTransitionRules() {
    ZoneRules test = europeLondon();
    List<ZoneOffsetTransitionRule> rules = test.getTransitionRules();
    assertEquals(rules.size(), 2);

    ZoneOffsetTransitionRule in = rules.get(0);
    assertEquals(in.getMonth(), Month.MARCH);
    assertEquals(in.getDayOfMonthIndicator(), 25);  // optimized from -1
    assertEquals(in.getDayOfWeek(), DayOfWeek.SUNDAY);
    assertEquals(in.getLocalTime(), LocalTime.of(1, 0));
    assertEquals(in.getTimeDefinition(), TimeDefinition.UTC);
    assertEquals(in.getStandardOffset(), OFFSET_ZERO);
    assertEquals(in.getOffsetBefore(), OFFSET_ZERO);
    assertEquals(in.getOffsetAfter(), OFFSET_PONE);

    ZoneOffsetTransitionRule out = rules.get(1);
    assertEquals(out.getMonth(), Month.OCTOBER);
    assertEquals(out.getDayOfMonthIndicator(), 25);  // optimized from -1
    assertEquals(out.getDayOfWeek(), DayOfWeek.SUNDAY);
    assertEquals(out.getLocalTime(), LocalTime.of(1, 0));
    assertEquals(out.getTimeDefinition(), TimeDefinition.UTC);
    assertEquals(out.getStandardOffset(), OFFSET_ZERO);
    assertEquals(out.getOffsetBefore(), OFFSET_PONE);
    assertEquals(out.getOffsetAfter(), OFFSET_ZERO);
}
项目:jdk8u-jdk    文件:ZonedDateTime.java   
/**
 * Obtains an instance of {@code ZonedDateTime} strictly validating the
 * combination of local date-time, offset and zone ID.
 * <p>
 * This creates a zoned date-time ensuring that the offset is valid for the
 * local date-time according to the rules of the specified zone.
 * If the offset is invalid, an exception is thrown.
 *
 * @param localDateTime  the local date-time, not null
 * @param offset  the zone offset, not null
 * @param zone  the time-zone, not null
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(offset, "offset");
    Objects.requireNonNull(zone, "zone");
    ZoneRules rules = zone.getRules();
    if (rules.isValidOffset(localDateTime, offset) == false) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        if (trans != null && trans.isGap()) {
            // error message says daylight savings for simplicity
            // even though there are other kinds of gaps
            throw new DateTimeException("LocalDateTime '" + localDateTime +
                    "' does not exist in zone '" + zone +
                    "' due to a gap in the local time-line, typically caused by daylight savings");
        }
        throw new DateTimeException("ZoneOffset '" + offset + "' is not valid for LocalDateTime '" +
                localDateTime + "' in zone '" + zone + "'");
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
项目:jdk8u-jdk    文件:TestZoneId.java   
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_London_getOffsetInfo_gap() {
    ZoneRules test = europeLondon();
    final LocalDateTime dateTime = LocalDateTime.of(2008, 3, 30, 1, 0, 0, 0);
    ZoneOffsetTransition trans = checkOffset(test, dateTime, OFFSET_ZERO, GAP);
    assertEquals(trans.isGap(), true);
    assertEquals(trans.isOverlap(), false);
    assertEquals(trans.getOffsetBefore(), OFFSET_ZERO);
    assertEquals(trans.getOffsetAfter(), OFFSET_PONE);
    assertEquals(trans.getInstant(), createInstant(2008, 3, 30, 1, 0, ZoneOffset.UTC));
    assertEquals(trans.getDateTimeBefore(), LocalDateTime.of(2008, 3, 30, 1, 0));
    assertEquals(trans.getDateTimeAfter(), LocalDateTime.of(2008, 3, 30, 2, 0));
    assertEquals(trans.isValidOffset(OFFSET_ZERO), false);
    assertEquals(trans.isValidOffset(OFFSET_PONE), false);
    assertEquals(trans.isValidOffset(OFFSET_PTWO), false);
    assertEquals(trans.toString(), "Transition[Gap at 2008-03-30T01:00Z to +01:00]");

    assertFalse(trans.equals(null));
    assertFalse(trans.equals(OFFSET_ZERO));
    assertTrue(trans.equals(trans));

    final ZoneOffsetTransition otherTrans = test.getTransition(dateTime);
    assertTrue(trans.equals(otherTrans));
    assertEquals(trans.hashCode(), otherTrans.hashCode());
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_London_previousTransition_historic() {
    ZoneRules test = europeLondon();
    List<ZoneOffsetTransition> trans = test.getTransitions();

    ZoneOffsetTransition first = trans.get(0);
    assertEquals(test.previousTransition(first.getInstant()), null);
    assertEquals(test.previousTransition(first.getInstant().minusNanos(1)), null);

    for (int i = 0; i < trans.size() - 1; i++) {
        ZoneOffsetTransition prev = trans.get(i);
        ZoneOffsetTransition cur = trans.get(i + 1);

        assertEquals(test.previousTransition(cur.getInstant()), prev);
        assertEquals(test.previousTransition(prev.getInstant().plusSeconds(1)), prev);
        assertEquals(test.previousTransition(prev.getInstant().plusNanos(1)), prev);
    }
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_Paris_getOffsetInfo_gap() {
    ZoneRules test = europeParis();
    final LocalDateTime dateTime = LocalDateTime.of(2008, 3, 30, 2, 0, 0, 0);
    ZoneOffsetTransition trans = checkOffset(test, dateTime, OFFSET_PONE, GAP);
    assertEquals(trans.isGap(), true);
    assertEquals(trans.isOverlap(), false);
    assertEquals(trans.getOffsetBefore(), OFFSET_PONE);
    assertEquals(trans.getOffsetAfter(), OFFSET_PTWO);
    assertEquals(trans.getInstant(), createInstant(2008, 3, 30, 1, 0, ZoneOffset.UTC));
    assertEquals(trans.isValidOffset(OFFSET_ZERO), false);
    assertEquals(trans.isValidOffset(OFFSET_PONE), false);
    assertEquals(trans.isValidOffset(OFFSET_PTWO), false);
    assertEquals(trans.toString(), "Transition[Gap at 2008-03-30T02:00+01:00 to +02:00]");

    assertFalse(trans.equals(null));
    assertFalse(trans.equals(OFFSET_PONE));
    assertTrue(trans.equals(trans));

    final ZoneOffsetTransition otherTrans = test.getTransition(dateTime);
    assertTrue(trans.equals(otherTrans));
    assertEquals(trans.hashCode(), otherTrans.hashCode());
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_Paris_getStandardOffset() {
    ZoneRules test = europeParis();
    ZonedDateTime zdt = createZDT(1840, 1, 1, ZoneOffset.UTC);
    while (zdt.getYear() < 2010) {
        Instant instant = zdt.toInstant();
        if (zdt.toLocalDate().isBefore(LocalDate.of(1911, 3, 11))) {
            assertEquals(test.getStandardOffset(instant), ZoneOffset.ofHoursMinutesSeconds(0, 9, 21));
        } else if (zdt.toLocalDate().isBefore(LocalDate.of(1940, 6, 14))) {
            assertEquals(test.getStandardOffset(instant), OFFSET_ZERO);
        } else if (zdt.toLocalDate().isBefore(LocalDate.of(1944, 8, 25))) {
            assertEquals(test.getStandardOffset(instant), OFFSET_PONE);
        } else if (zdt.toLocalDate().isBefore(LocalDate.of(1945, 9, 16))) {
            assertEquals(test.getStandardOffset(instant), OFFSET_ZERO);
        } else {
            assertEquals(test.getStandardOffset(instant), OFFSET_PONE);
        }
        zdt = zdt.plusMonths(6);
    }
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_London_nextTransition_rulesBased() {
    ZoneRules test = europeLondon();
    List<ZoneOffsetTransitionRule> rules = test.getTransitionRules();
    List<ZoneOffsetTransition> trans = test.getTransitions();

    ZoneOffsetTransition last = trans.get(trans.size() - 1);
    assertEquals(test.nextTransition(last.getInstant()), rules.get(0).createTransition(1998));

    for (int year = 1998; year < 2010; year++) {
        ZoneOffsetTransition a = rules.get(0).createTransition(year);
        ZoneOffsetTransition b = rules.get(1).createTransition(year);
        ZoneOffsetTransition c = rules.get(0).createTransition(year + 1);

        assertEquals(test.nextTransition(a.getInstant()), b);
        assertEquals(test.nextTransition(b.getInstant().minusNanos(1)), b);

        assertEquals(test.nextTransition(b.getInstant()), c);
        assertEquals(test.nextTransition(c.getInstant().minusNanos(1)), c);
    }
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_NewYork_getOffsetInfo_gap() {
    ZoneRules test = americaNewYork();
    final LocalDateTime dateTime = LocalDateTime.of(2008, 3, 9, 2, 0, 0, 0);
    ZoneOffsetTransition trans = checkOffset(test, dateTime, ZoneOffset.ofHours(-5), GAP);
    assertEquals(trans.isGap(), true);
    assertEquals(trans.isOverlap(), false);
    assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(-5));
    assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(-4));
    assertEquals(trans.getInstant(), createInstant(2008, 3, 9, 2, 0, ZoneOffset.ofHours(-5)));
    assertEquals(trans.isValidOffset(OFFSET_PTWO), false);
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-5)), false);
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-4)), false);
    assertEquals(trans.toString(), "Transition[Gap at 2008-03-09T02:00-05:00 to -04:00]");

    assertFalse(trans.equals(null));
    assertFalse(trans.equals(ZoneOffset.ofHours(-5)));
    assertTrue(trans.equals(trans));

    final ZoneOffsetTransition otherTrans = test.getTransition(dateTime);
    assertTrue(trans.equals(otherTrans));
    assertEquals(trans.hashCode(), otherTrans.hashCode());
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_NewYork_getOffsetInfo_overlap() {
    ZoneRules test = americaNewYork();
    final LocalDateTime dateTime = LocalDateTime.of(2008, 11, 2, 1, 0, 0, 0);
    ZoneOffsetTransition trans = checkOffset(test, dateTime, ZoneOffset.ofHours(-4), OVERLAP);
    assertEquals(trans.isGap(), false);
    assertEquals(trans.isOverlap(), true);
    assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(-4));
    assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(-5));
    assertEquals(trans.getInstant(), createInstant(2008, 11, 2, 2, 0, ZoneOffset.ofHours(-4)));
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-1)), false);
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-5)), true);
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-4)), true);
    assertEquals(trans.isValidOffset(OFFSET_PTWO), false);
    assertEquals(trans.toString(), "Transition[Overlap at 2008-11-02T02:00-04:00 to -05:00]");

    assertFalse(trans.equals(null));
    assertFalse(trans.equals(ZoneOffset.ofHours(-4)));
    assertTrue(trans.equals(trans));

    final ZoneOffsetTransition otherTrans = test.getTransition(dateTime);
    assertTrue(trans.equals(otherTrans));
    assertEquals(trans.hashCode(), otherTrans.hashCode());
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_London_previousTransition_rulesBased() {
    ZoneRules test = europeLondon();
    List<ZoneOffsetTransitionRule> rules = test.getTransitionRules();
    List<ZoneOffsetTransition> trans = test.getTransitions();

    ZoneOffsetTransition last = trans.get(trans.size() - 1);
    assertEquals(test.previousTransition(last.getInstant().plusSeconds(1)), last);
    assertEquals(test.previousTransition(last.getInstant().plusNanos(1)), last);

    // Jan 1st of year between transitions and rules
    ZonedDateTime odt = ZonedDateTime.ofInstant(last.getInstant(), last.getOffsetAfter());
    odt = odt.withDayOfYear(1).plusYears(1).with(LocalTime.MIDNIGHT);
    assertEquals(test.previousTransition(odt.toInstant()), last);

    // later years
    for (int year = 1998; year < 2010; year++) {
        ZoneOffsetTransition a = rules.get(0).createTransition(year);
        ZoneOffsetTransition b = rules.get(1).createTransition(year);
        ZoneOffsetTransition c = rules.get(0).createTransition(year + 1);

        assertEquals(test.previousTransition(c.getInstant()), b);
        assertEquals(test.previousTransition(b.getInstant().plusSeconds(1)), b);
        assertEquals(test.previousTransition(b.getInstant().plusNanos(1)), b);

        assertEquals(test.previousTransition(b.getInstant()), a);
        assertEquals(test.previousTransition(a.getInstant().plusSeconds(1)), a);
        assertEquals(test.previousTransition(a.getInstant().plusNanos(1)), a);
    }
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_NewYork_getOffsetInfo_fromDST() {
    ZoneRules test = americaNewYork();
    checkOffset(test, createLDT(2008, 11, 1), ZoneOffset.ofHours(-4), 1);
    checkOffset(test, createLDT(2008, 11, 2), ZoneOffset.ofHours(-4), 1);
    checkOffset(test, createLDT(2008, 11, 3), ZoneOffset.ofHours(-5), 1);
    checkOffset(test, createLDT(2008, 11, 4), ZoneOffset.ofHours(-5), 1);
    checkOffset(test, createLDT(2008, 11, 5), ZoneOffset.ofHours(-5), 1);
    checkOffset(test, createLDT(2008, 11, 6), ZoneOffset.ofHours(-5), 1);
    checkOffset(test, createLDT(2008, 11, 7), ZoneOffset.ofHours(-5), 1);
    // cutover at 02:00 local
    checkOffset(test, LocalDateTime.of(2008, 11, 2, 0, 59, 59, 999999999), ZoneOffset.ofHours(-4), 1);
    checkOffset(test, LocalDateTime.of(2008, 11, 2, 2, 0, 0, 0), ZoneOffset.ofHours(-5), 1);
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
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);
}
项目:openjdk-jdk10    文件:TestFixedZoneRules.java   
@Test
public void test_data_nullInput() {
    ZoneRules test = make(OFFSET_PONE);
    assertEquals(test.getOffset((Instant) null), OFFSET_PONE);
    assertEquals(test.getOffset((LocalDateTime) null), OFFSET_PONE);
    assertEquals(test.getValidOffsets(null).size(), 1);
    assertEquals(test.getValidOffsets(null).get(0), OFFSET_PONE);
    assertEquals(test.getTransition(null), null);
    assertEquals(test.getStandardOffset(null), OFFSET_PONE);
    assertEquals(test.getDaylightSavings(null), Duration.ZERO);
    assertEquals(test.isDaylightSavings(null), false);
    assertEquals(test.nextTransition(null), null);
    assertEquals(test.previousTransition(null), null);
}
项目:OpenJSharp    文件: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);
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_London_nextTransition_historic() {
    ZoneRules test = europeLondon();
    List<ZoneOffsetTransition> trans = test.getTransitions();

    ZoneOffsetTransition first = trans.get(0);
    assertEquals(test.nextTransition(first.getInstant().minusNanos(1)), first);

    for (int i = 0; i < trans.size() - 1; i++) {
        ZoneOffsetTransition cur = trans.get(i);
        ZoneOffsetTransition next = trans.get(i + 1);

        assertEquals(test.nextTransition(cur.getInstant()), next);
        assertEquals(test.nextTransition(next.getInstant().minusNanos(1)), next);
    }
}
项目:optashift-employee-rostering    文件: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) {

  Jdk7Methods.Objects_requireNonNull(instant, "instant");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  ZoneOffset offset = rules.getOffset(instant);
  long secsOfDay = instant.getEpochSecond() % SECONDS_PER_DAY;
  secsOfDay = (secsOfDay + offset.getTotalSeconds()) % SECONDS_PER_DAY;
  if (secsOfDay < 0) {
    secsOfDay += SECONDS_PER_DAY;
  }
  LocalTime time = LocalTime.ofSecondOfDay(secsOfDay, instant.getNano());
  return new OffsetTime(time, offset);
}
项目:optashift-employee-rostering    文件:ZonedDateTime.java   
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time using the preferred offset if
 * possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line. This is achieved by finding a valid
 * offset from UTC/Greenwich for the local date-time as defined by the {@link ZoneRules rules} of the zone
 * ID.
 * <p>
 * In most cases, there is only one valid offset for a local date-time. In the case of an overlap, where
 * clocks are set back, there are two valid offsets. If the preferred offset is one of the valid offsets
 * then it is used. Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset. Instead, the local date-time
 * is adjusted to be later by the length of the gap. For a typical one hour daylight savings change, the
 * local date-time will be moved one hour later into the offset typically corresponding to "summer".
 * 
 * @param localDateTime the local date-time, not null
 * @param zone the time-zone, not null
 * @param preferredOffset the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {

  Jdk7Methods.Objects_requireNonNull(localDateTime, "localDateTime");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  if (zone instanceof ZoneOffset) {
    return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
  }
  ZoneRules rules = zone.getRules();
  List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
  ZoneOffset offset;
  if (validOffsets.size() == 1) {
    offset = validOffsets.get(0);
  } else if (validOffsets.size() == 0) {
    // ZoneOffsetTransition trans = rules.getTransition(localDateTime);
    // localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
    offset = rules.getOffset(localDateTime); // trans.getOffsetAfter();
  } else {
    if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
      offset = preferredOffset;
    } else {
      offset = Jdk7Methods.Objects_requireNonNull(validOffsets.get(0), "offset"); // protect against bad
                                                                                  // ZoneRules
    }
  }
  return new ZonedDateTime(localDateTime, offset, zone);
}
项目:openjdk-jdk10    文件:TCKZoneRulesProvider.java   
@Override
protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
    NavigableMap<String, ZoneRules> result = new TreeMap<>();
    result.put("DynamicVersion1", BASE);
    if (count > 2) {
        result.put("DynamicVersion2", ALTERNATE);
    }
    return result;
}
项目:jdk8u-jdk    文件:TCKFixedZoneRules.java   
@Test(dataProvider="rules")
public void test_getValidOffsets_LDT(ZoneRules test, ZoneOffset expectedOffset) {
    assertEquals(test.getValidOffsets(LDT).size(), 1);
    assertEquals(test.getValidOffsets(LDT).get(0), expectedOffset);
    assertEquals(test.getValidOffsets(null).size(), 1);
    assertEquals(test.getValidOffsets(null).get(0), expectedOffset);
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_London_getOffsetInfo_toDST() {
    ZoneRules test = europeLondon();
    checkOffset(test, createLDT(2008, 3, 24), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 25), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 26), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 27), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 28), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 29), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 30), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 31), OFFSET_PONE, 1);
    // cutover at 01:00Z
    checkOffset(test, LocalDateTime.of(2008, 3, 30, 0, 59, 59, 999999999), OFFSET_ZERO, 1);
    checkOffset(test, LocalDateTime.of(2008, 3, 30, 2, 0, 0, 0), OFFSET_PONE, 1);
}
项目:jdk8u-jdk    文件:ChronoZonedDateTimeImpl.java   
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
项目:openjdk-jdk10    文件:TCKZoneRulesSerialization.java   
private void assertSerialization(ZoneRules test) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(test);
    baos.close();
    byte[] bytes = baos.toByteArray();

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream in = new ObjectInputStream(bais);
    ZoneRules result = (ZoneRules) in.readObject();

    assertEquals(result, test);
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_London_getOffset_toDST() {
    ZoneRules test = europeLondon();
    assertEquals(test.getOffset(createInstant(2008, 3, 24, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 25, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 26, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 27, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 28, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 29, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 30, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 31, ZoneOffset.UTC)), OFFSET_PONE);
    // cutover at 01:00Z
    assertEquals(test.getOffset(createInstant(2008, 3, 30, 0, 59, 59, 999999999, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 30, 1, 0, 0, 0, ZoneOffset.UTC)), OFFSET_PONE);
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_London_getOffsetInfo_toDST() {
    ZoneRules test = europeLondon();
    checkOffset(test, createLDT(2008, 3, 24), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 25), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 26), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 27), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 28), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 29), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 30), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 3, 31), OFFSET_PONE, 1);
    // cutover at 01:00Z
    checkOffset(test, LocalDateTime.of(2008, 3, 30, 0, 59, 59, 999999999), OFFSET_ZERO, 1);
    checkOffset(test, LocalDateTime.of(2008, 3, 30, 2, 0, 0, 0), OFFSET_PONE, 1);
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_London_getOffsetInfo_fromDST() {
    ZoneRules test = europeLondon();
    checkOffset(test, createLDT(2008, 10, 24), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 10, 25), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 10, 26), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 10, 27), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 10, 28), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 10, 29), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 10, 30), OFFSET_ZERO, 1);
    checkOffset(test, createLDT(2008, 10, 31), OFFSET_ZERO, 1);
    // cutover at 01:00Z
    checkOffset(test, LocalDateTime.of(2008, 10, 26, 0, 59, 59, 999999999), OFFSET_PONE, 1);
    checkOffset(test, LocalDateTime.of(2008, 10, 26, 2, 0, 0, 0), OFFSET_ZERO, 1);
}
项目:jdk8u-jdk    文件:TCKZoneRules.java   
public void test_London_getStandardOffset() {
    ZoneRules test = europeLondon();
    ZonedDateTime zdt = createZDT(1840, 1, 1, ZoneOffset.UTC);
    while (zdt.getYear() < 2010) {
        Instant instant = zdt.toInstant();
        if (zdt.getYear() < 1848) {
            assertEquals(test.getStandardOffset(instant), ZoneOffset.ofHoursMinutesSeconds(0, -1, -15));
        } else if (zdt.getYear() >= 1969 && zdt.getYear() < 1972) {
            assertEquals(test.getStandardOffset(instant), OFFSET_PONE);
        } else {
            assertEquals(test.getStandardOffset(instant), OFFSET_ZERO);
        }
        zdt = zdt.plusMonths(6);
    }
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_London_getOffset() {
    ZoneRules test = europeLondon();
    assertEquals(test.getOffset(createInstant(2008, 1, 1, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 2, 1, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 3, 1, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 4, 1, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 5, 1, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 6, 1, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 7, 1, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 8, 1, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 9, 1, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 10, 1, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 11, 1, ZoneOffset.UTC)), OFFSET_ZERO);
    assertEquals(test.getOffset(createInstant(2008, 12, 1, ZoneOffset.UTC)), OFFSET_ZERO);
}
项目:openjdk-jdk10    文件:TCKZoneRulesProvider.java   
@Test
public void test_getRules_StringBoolean_dynamic() {
    MockDynamicProvider dynamicProvider = new MockDynamicProvider();
    ZoneRulesProvider.registerProvider(dynamicProvider);

    assertEquals(dynamicProvider.count, 0);
    ZoneRules rules1 = ZoneId.of("DynamicLocation").getRules();
    assertEquals(dynamicProvider.count, 2);
    assertEquals(rules1, dynamicProvider.BASE);
    ZoneRules rules2 = ZoneId.of("DynamicLocation").getRules();
    assertEquals(dynamicProvider.count, 4);
    assertEquals(rules2, dynamicProvider.ALTERNATE);
}
项目:openjdk-jdk10    文件:TCKZoneRulesProvider.java   
@Override
protected ZoneRules provideRules(String zoneId, boolean forCaching) {
    if (zoneId.equals("FooLocation")) {
        return rules;
    }
    throw new ZoneRulesException("Invalid");
}
项目:jdk8u-jdk    文件:TCKFixedZoneRules.java   
@Test(dataProvider="rules")
public void test_isValidOffset_LDT_ZO(ZoneRules test, ZoneOffset expectedOffset) {
    assertEquals(test.isValidOffset(LDT, expectedOffset), true);
    assertEquals(test.isValidOffset(LDT, ZoneOffset.UTC), false);
    assertEquals(test.isValidOffset(LDT, null), false);

    assertEquals(test.isValidOffset(null, expectedOffset), true);
    assertEquals(test.isValidOffset(null, ZoneOffset.UTC), false);
    assertEquals(test.isValidOffset(null, null), false);
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_NewYork_getOffset() {
    ZoneRules test = americaNewYork();
    ZoneOffset offset = ZoneOffset.ofHours(-5);
    assertEquals(test.getOffset(createInstant(2008, 1, 1, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 2, 1, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 3, 1, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 4, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 5, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 6, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 7, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 8, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 9, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 10, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 11, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 12, 1, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 1, 28, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 2, 28, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 3, 28, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 4, 28, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 5, 28, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 6, 28, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 7, 28, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 8, 28, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 9, 28, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 10, 28, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 11, 28, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 12, 28, offset)), ZoneOffset.ofHours(-5));
}
项目:openjdk-jdk10    文件:TCKZoneRulesProvider.java   
@Test
public void test_getVersions_String() {
    NavigableMap<String, ZoneRules> versions = ZoneRulesProvider.getVersions("Europe/London");
    assertTrue(versions.size() >= 1);
    ZoneRules rules = ZoneRulesProvider.getRules("Europe/London", false);
    assertEquals(versions.lastEntry().getValue(), rules);

    NavigableMap<String, ZoneRules> copy = new TreeMap<>(versions);
    versions.clear();
    assertEquals(versions.size(), 0);
    NavigableMap<String, ZoneRules> versions2 = ZoneRulesProvider.getVersions("Europe/London");
    assertEquals(versions2, copy);
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_Paris_getOffsetInfo_toDST() {
    ZoneRules test = europeParis();
    checkOffset(test, createLDT(2008, 3, 24), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 3, 25), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 3, 26), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 3, 27), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 3, 28), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 3, 29), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 3, 30), OFFSET_PONE, 1);
    checkOffset(test, createLDT(2008, 3, 31), OFFSET_PTWO, 1);
    // cutover at 01:00Z which is 02:00+01:00(local Paris time)
    checkOffset(test, LocalDateTime.of(2008, 3, 30, 1, 59, 59, 999999999), OFFSET_PONE, 1);
    checkOffset(test, LocalDateTime.of(2008, 3, 30, 3, 0, 0, 0), OFFSET_PTWO, 1);
}
项目:openjdk-jdk10    文件:TCKZoneRules.java   
public void test_Paris_getOffset_fromDST() {
    ZoneRules test = europeParis();
    assertEquals(test.getOffset(createInstant(2008, 10, 24, ZoneOffset.UTC)), OFFSET_PTWO);
    assertEquals(test.getOffset(createInstant(2008, 10, 25, ZoneOffset.UTC)), OFFSET_PTWO);
    assertEquals(test.getOffset(createInstant(2008, 10, 26, ZoneOffset.UTC)), OFFSET_PTWO);
    assertEquals(test.getOffset(createInstant(2008, 10, 27, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 10, 28, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 10, 29, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 10, 30, ZoneOffset.UTC)), OFFSET_PONE);
    assertEquals(test.getOffset(createInstant(2008, 10, 31, ZoneOffset.UTC)), OFFSET_PONE);
    // cutover at 01:00Z
    assertEquals(test.getOffset(createInstant(2008, 10, 26, 0, 59, 59, 999999999, ZoneOffset.UTC)), OFFSET_PTWO);
    assertEquals(test.getOffset(createInstant(2008, 10, 26, 1, 0, 0, 0, ZoneOffset.UTC)), OFFSET_PONE);
}