org.threeten.bp.ZoneOffset Java Examples

The following examples show how to use org.threeten.bp.ZoneOffset. 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: DateTimeFormatterBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean print(DateTimePrintContext context, StringBuilder buf) {
    ZoneId zone = context.getValue(TemporalQueries.zoneId());
    if (zone == null) {
        return false;
    }
    if (zone.normalized() instanceof ZoneOffset) {
        buf.append(zone.getId());
        return true;
    }
    TemporalAccessor temporal = context.getTemporal();
    boolean daylight = false;
    if (temporal.isSupported(INSTANT_SECONDS)) {
        Instant instant = Instant.ofEpochSecond(temporal.getLong(INSTANT_SECONDS));
        daylight = zone.getRules().isDaylightSavings(instant);
    }
    TimeZone tz = TimeZone.getTimeZone(zone.getId());
    int tzstyle = (textStyle.asNormal() == TextStyle.FULL ? TimeZone.LONG : TimeZone.SHORT);
    String text = tz.getDisplayName(daylight, tzstyle, context.getLocale());
    buf.append(text);
    return true;
}
 
Example #2
Source File: TestDateTimeFormatters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider="sample_isoDateTime")
public void test_parse_isoDateTime(
        Integer year, Integer month, Integer day,
        Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId,
        String input, Class<?> invalid) {
    if (input != null) {
        Expected expected = createDateTime(year, month, day, hour, min, sec, nano);
        if (offsetId != null) {
            expected.fieldValues.put(OFFSET_SECONDS, (long) ZoneOffset.of(offsetId).getTotalSeconds());
            if (zoneId != null) {
                expected.zone = ZoneId.of(zoneId);
            }
        }
        assertParseMatch(DateTimeFormatter.ISO_DATE_TIME.parseUnresolved(input, new ParsePosition(0)), expected);
    }
}
 
Example #3
Source File: TestZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_jordan2400() {
    // rule is 24:00 - this is simplified from the TZDB
    //    Rule    Jordan  2002    max -   Mar lastThu 24:00   1:00    S
    //    Rule    Jordan  2002    max -   Sep lastFri 0:00s   0   -
    //    # Zone  NAME        GMTOFF  RULES   FORMAT  [UNTIL]
    //                2:00    Jordan  EE%sT
    ZoneOffset plus2 = ZoneOffset.ofHours(2);
    ZoneOffset plus3 = ZoneOffset.ofHours(3);
    ZoneRulesBuilder b = new ZoneRulesBuilder();
    b.addWindowForever(plus2);
    b.addRuleToWindow(2002, Year.MAX_VALUE, MARCH, -1, THURSDAY, time(0, 0), true, WALL, PERIOD_1HOUR);
    b.addRuleToWindow(2002, Year.MAX_VALUE, SEPTEMBER, -1, FRIDAY, time(0, 0), false, STANDARD, PERIOD_0);
    ZoneRules test = b.toRules("Asia/Amman");

    assertOffsetInfo(test, DATE_TIME_FIRST, plus2);
    assertOffsetInfo(test, DATE_TIME_LAST, plus2);

    assertGap(test, 2002, 3, 29, 0, 0, plus2, plus3);
    assertOffsetInfo(test, dateTime(2002, 3, 28, 23, 0), plus2);
    assertOffsetInfo(test, dateTime(2002, 3, 29, 1, 0), plus3);

    assertOverlap(test, 2002, 9, 27, 0, 0, plus3, plus2);
    assertOffsetInfo(test, dateTime(2002, 9, 26, 23, 0), plus3);
    assertOffsetInfo(test, dateTime(2002, 9, 27, 1, 0), plus2);
}
 
Example #4
Source File: TestStandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void test_Dublin_dst() {
    ZoneRules test = europeDublin();
    assertEquals(test.isDaylightSavings(createZDT(1960, 1, 1, ZoneOffset.UTC).toInstant()), false);
    assertEquals(test.getDaylightSavings(createZDT(1960, 1, 1, ZoneOffset.UTC).toInstant()), Duration.ofHours(0));
    assertEquals(test.isDaylightSavings(createZDT(1960, 7, 1, ZoneOffset.UTC).toInstant()), true);
    assertEquals(test.getDaylightSavings(createZDT(1960, 7, 1, ZoneOffset.UTC).toInstant()), Duration.ofHours(1));
    // check negative DST is correctly handled
    assertEquals(test.isDaylightSavings(createZDT(2016, 1, 1, ZoneOffset.UTC).toInstant()), false);
    assertEquals(test.getDaylightSavings(createZDT(2016, 1, 1, ZoneOffset.UTC).toInstant()), Duration.ofHours(0));
    assertEquals(test.isDaylightSavings(createZDT(2016, 7, 1, ZoneOffset.UTC).toInstant()), true);
    assertEquals(test.getDaylightSavings(createZDT(2016, 7, 1, ZoneOffset.UTC).toInstant()), Duration.ofHours(1));

    // TZDB data is messed up, comment out tests until better fix available
    DateTimeFormatter formatter1 = new DateTimeFormatterBuilder().appendZoneText(TextStyle.FULL).toFormatter();
    assertEquals(formatter1.format(createZDT(2016, 1, 1, ZoneId.of("Europe/Dublin"))), "Greenwich Mean Time");
    assertEquals(formatter1.format(createZDT(2016, 7, 1, ZoneId.of("Europe/Dublin"))).startsWith("Irish S"), true);

    DateTimeFormatter formatter2 = new DateTimeFormatterBuilder().appendZoneText(TextStyle.SHORT).toFormatter();
    assertEquals(formatter2.format(createZDT(2016, 1, 1, ZoneId.of("Europe/Dublin"))), "GMT");
    assertEquals(formatter2.format(createZDT(2016, 7, 1, ZoneId.of("Europe/Dublin"))), "IST");
}
 
Example #5
Source File: TestChronoZonedDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badWithAdjusterChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(1900, 1, 1);
    ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalAdjuster adjuster = new FixedAdjuster(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.with(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + czdt + ", supplied: " + czdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            ChronoZonedDateTime<?> result = czdt.with(adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #6
Source File: ZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates an instance defining the yearly rule to create transitions between two offsets.
 *
 * @param month  the month of the month-day of the first day of the cutover week, not null
 * @param dayOfMonthIndicator  the day of the month-day of the cutover week, positive if the week is that
 *  day or later, negative if the week is that day or earlier, counting from the last day of the month,
 *  from -28 to 31 excluding 0
 * @param dayOfWeek  the required day-of-week, null if the month-day should not be changed
 * @param time  the cutover time in the 'before' offset, not null
 * @param adjustDays  the time days adjustment
 * @param timeDefnition  how to interpret the cutover
 * @param standardOffset  the standard offset in force at the cutover, not null
 * @param offsetBefore  the offset before the cutover, not null
 * @param offsetAfter  the offset after the cutover, not null
 * @throws IllegalArgumentException if the day of month indicator is invalid
 * @throws IllegalArgumentException if the end of day flag is true when the time is not midnight
 */
ZoneOffsetTransitionRule(
        Month month,
        int dayOfMonthIndicator,
        DayOfWeek dayOfWeek,
        LocalTime time,
        int adjustDays,
        TimeDefinition timeDefnition,
        ZoneOffset standardOffset,
        ZoneOffset offsetBefore,
        ZoneOffset offsetAfter) {
    this.month = month;
    this.dom = (byte) dayOfMonthIndicator;
    this.dow = dayOfWeek;
    this.time = time;
    this.adjustDays = adjustDays;
    this.timeDefinition = timeDefnition;
    this.standardOffset = standardOffset;
    this.offsetBefore = offsetBefore;
    this.offsetAfter = offsetAfter;
}
 
Example #7
Source File: TestZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_twoChangesSameDay() {
    // ensures that TZRule.compare works
    ZoneOffset plus2 = ZoneOffset.ofHours(2);
    ZoneOffset plus3 = ZoneOffset.ofHours(3);
    ZoneRulesBuilder b = new ZoneRulesBuilder();
    b.addWindowForever(plus2);
    b.addRuleToWindow(2010, 2010, SEPTEMBER, 10, null, time(12, 0), false, STANDARD, PERIOD_1HOUR);
    b.addRuleToWindow(2010, 2010, SEPTEMBER, 10, null, time(23, 0), false, STANDARD, PERIOD_0);
    ZoneRules test = b.toRules("Africa/Cairo");

    assertOffsetInfo(test, DATE_TIME_FIRST, plus2);
    assertOffsetInfo(test, DATE_TIME_LAST, plus2);

    assertGap(test, 2010, 9, 10, 12, 0, plus2, plus3);  // jump forward from 12:00 to 13:00 on Tue 10th Sep
    assertOverlap(test, 2010, 9, 10, 23, 0, plus3, plus2);  // overlaps from Wed 11th Sep 00:00 back to Tue 10th Sep 23:00
}
 
Example #8
Source File: TestChronoZonedDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badPlusAdjusterChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(1900, 1, 1);
    ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalAmount adjuster = new FixedAdjuster(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.plus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + czdt + ", supplied: " + czdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.plus(adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date time");
        }
    }
}
 
Example #9
Source File: TestStandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void test_Dublin_getOffsetInfo_gap() {
    ZoneRules test = europeDublin();
    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]");
}
 
Example #10
Source File: TestZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_cairo_dateChange() {
    //    Rule    Egypt   2008    max -   Apr lastFri  0:00s  1:00    S
    //    Rule    Egypt   2008    max -   Aug lastThu 23:00s  0   -
    //    Zone    Africa/Cairo    2:05:00 -     LMT   1900  Oct
    //                            2:00    Egypt EE%sT
    ZoneOffset plus2 = ZoneOffset.ofHours(2);
    ZoneOffset plus3 = ZoneOffset.ofHours(3);
    ZoneRulesBuilder b = new ZoneRulesBuilder();
    b.addWindowForever(plus2);
    b.addRuleToWindow(2008, Year.MAX_VALUE, APRIL, -1, FRIDAY, time(0, 0), false, STANDARD, PERIOD_1HOUR);
    b.addRuleToWindow(2008, Year.MAX_VALUE, AUGUST, -1, THURSDAY, time(23, 0), false, STANDARD, PERIOD_0);
    ZoneRules test = b.toRules("Africa/Cairo");

    assertOffsetInfo(test, DATE_TIME_FIRST, plus2);
    assertOffsetInfo(test, DATE_TIME_LAST, plus2);

    assertGap(test, 2009, 4, 24, 0, 0, plus2, plus3);
    assertOverlap(test, 2009, 8, 27, 23, 0, plus3, plus2);  // overlaps from Fri 00:00 back to Thu 23:00
}
 
Example #11
Source File: TestChronoZonedDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badMinusAdjusterChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(1900, 1, 1);
    ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalAmount adjuster = new FixedAdjuster(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.minus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + czdt + ", supplied: " + czdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.minus(adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #12
Source File: TestStandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void test_London_getOffsetInfo_overlap() {
    ZoneRules test = europeLondon();
    final LocalDateTime dateTime = LocalDateTime.of(2008, 10, 26, 1, 0, 0, 0);
    ZoneOffsetTransition trans = checkOffset(test, dateTime, OFFSET_PONE, OVERLAP);
    assertEquals(trans.isGap(), false);
    assertEquals(trans.isOverlap(), true);
    assertEquals(trans.getOffsetBefore(), OFFSET_PONE);
    assertEquals(trans.getOffsetAfter(), OFFSET_ZERO);
    assertEquals(trans.getInstant(), createInstant(2008, 10, 26, 1, 0, ZoneOffset.UTC));
    assertEquals(trans.getDateTimeBefore(), LocalDateTime.of(2008, 10, 26, 2, 0));
    assertEquals(trans.getDateTimeAfter(), LocalDateTime.of(2008, 10, 26, 1, 0));
    assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-1)), false);
    assertEquals(trans.isValidOffset(OFFSET_ZERO), true);
    assertEquals(trans.isValidOffset(OFFSET_PONE), true);
    assertEquals(trans.isValidOffset(OFFSET_PTWO), false);
    assertEquals(trans.toString(), "Transition[Overlap at 2008-10-26T02:00+01:00 to Z]");

    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());
}
 
Example #13
Source File: TestZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_cairo_twoChangesSameMonth() {
    // 2011i
    //    Rule    Egypt    2010    only    -    Aug    11       0:00      0      -
    //    Rule    Egypt    2010    only    -    Sep    10       0:00      1:00   S
    //    Rule    Egypt    2010    only    -    Sep    lastThu  23:00s    0      -
    //    Zone    Africa/Cairo    2:05:00 -     LMT   1900  Oct
    //                            2:00    Egypt EE%sT
    ZoneOffset plus2 = ZoneOffset.ofHours(2);
    ZoneOffset plus3 = ZoneOffset.ofHours(3);
    ZoneRulesBuilder b = new ZoneRulesBuilder();
    b.addWindowForever(plus2);
    b.addRuleToWindow(2010, 2010, AUGUST, 11, null, time(0, 0), false, STANDARD, PERIOD_0);
    b.addRuleToWindow(2010, 2010, SEPTEMBER, 10, null, time(0, 0), false, STANDARD, PERIOD_1HOUR);
    b.addRuleToWindow(2010, 2010, SEPTEMBER, -1, THURSDAY, time(23, 0), false, STANDARD, PERIOD_0);
    ZoneRules test = b.toRules("Africa/Cairo");

    assertOffsetInfo(test, DATE_TIME_FIRST, plus2);
    assertOffsetInfo(test, DATE_TIME_LAST, plus2);

    assertGap(test, 2010, 9, 10, 0, 0, plus2, plus3);  // jump forward from 00:00 to 01:00 on Fri 10th Sep
    assertOverlap(test, 2010, 9, 30, 23, 0, plus3, plus2);  // overlaps from Fri 1st Oct 00:00 back to Thu 30th Sep 23:00 (!!!)
}
 
Example #14
Source File: StandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ZoneOffset getOffset(LocalDateTime localDateTime) {
    Object info = getOffsetInfo(localDateTime);
    if (info instanceof ZoneOffsetTransition) {
        return ((ZoneOffsetTransition) info).getOffsetBefore();
    }
    return (ZoneOffset) info;
}
 
Example #15
Source File: TestDateTimeFormatters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="sample_isoDate")
public void test_parse_isoDate(
        Integer year, Integer month, Integer day, String offsetId, String zoneId,
        String input, Class<?> invalid) {
    if (input != null) {
        Expected expected = createDate(year, month, day);
        if (offsetId != null) {
            expected.fieldValues.put(OFFSET_SECONDS, (long) ZoneOffset.of(offsetId).getTotalSeconds());
        }
        assertParseMatch(DateTimeFormatter.ISO_DATE.parseUnresolved(input, new ParsePosition(0)), expected);
    }
}
 
Example #16
Source File: StandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Reads the state from the stream.
 *
 * @param in  the input stream, not null
 * @return the created object, not null
 * @throws IOException if an error occurs
 */
static StandardZoneRules readExternal(DataInput in) throws IOException, ClassNotFoundException {
    int stdSize = in.readInt();
    long[] stdTrans = new long[stdSize];
    for (int i = 0; i < stdSize; i++) {
        stdTrans[i] = Ser.readEpochSec(in);
    }
    ZoneOffset[] stdOffsets = new ZoneOffset[stdSize + 1];
    for (int i = 0; i < stdOffsets.length; i++) {
        stdOffsets[i] = Ser.readOffset(in);
    }
    int savSize = in.readInt();
    long[] savTrans = new long[savSize];
    for (int i = 0; i < savSize; i++) {
        savTrans[i] = Ser.readEpochSec(in);
    }
    ZoneOffset[] savOffsets = new ZoneOffset[savSize + 1];
    for (int i = 0; i < savOffsets.length; i++) {
        savOffsets[i] = Ser.readOffset(in);
    }
    int ruleSize = in.readByte();
    ZoneOffsetTransitionRule[] rules = new ZoneOffsetTransitionRule[ruleSize];
    for (int i = 0; i < ruleSize; i++) {
        rules[i] = ZoneOffsetTransitionRule.readExternal(in);
    }
    return new StandardZoneRules(stdTrans, stdOffsets, savTrans, savOffsets, rules);
}
 
Example #17
Source File: StandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ZoneOffset getOffset(Instant instant) {
    long epochSec = instant.getEpochSecond();

    // check if using last rules
    if (lastRules.length > 0 &&
            epochSec > savingsInstantTransitions[savingsInstantTransitions.length - 1]) {
        int year = findYear(epochSec, wallOffsets[wallOffsets.length - 1]);
        ZoneOffsetTransition[] transArray = findTransitionArray(year);
        ZoneOffsetTransition trans = null;
        for (int i = 0; i < transArray.length; i++) {
            trans = transArray[i];
            if (epochSec < trans.toEpochSecond()) {
                return trans.getOffsetBefore();
            }
        }
        return trans.getOffsetAfter();
    }

    // using historic rules
    int index  = Arrays.binarySearch(savingsInstantTransitions, epochSec);
    if (index < 0) {
        // switch negative insert position to start of matched range
        index = -index - 2;
    }
    return wallOffsets[index + 1];
}
 
Example #18
Source File: TestFixedZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="rules")
public void test_serialization(ZoneRules test, ZoneOffset expectedOffset) 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);
    assertEquals(result.getClass(), test.getClass());
}
 
Example #19
Source File: TestMinguoChronology.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test()
public void test_MinguoChrono() {
    ChronoLocalDate h1 = MinguoChronology.INSTANCE.date(MinguoEra.ROC, 1, 2, 3);
    ChronoLocalDate h2 = h1;
    ChronoLocalDateTime<?> h3 = h2.atTime(LocalTime.NOON);
    @SuppressWarnings("unused")
    ChronoZonedDateTime<?> h4 = h3.atZone(ZoneOffset.UTC);
}
 
Example #20
Source File: ZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param standardOffset  the standard offset applicable during the window, not null
 * @param windowEnd  the end of the window, relative to the time definition, null if forever
 * @param timeDefinition  the time definition for calculating the true end, not null
 */
TZWindow(
        ZoneOffset standardOffset,
        LocalDateTime windowEnd,
        TimeDefinition timeDefinition) {
    super();
    this.windowEnd = windowEnd;
    this.timeDefinition = timeDefinition;
    this.standardOffset = standardOffset;
}
 
Example #21
Source File: TestZoneOffsetParser.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="offsets")
public void test_parse_midStringMatch(String pattern, String parse, ZoneOffset expected) throws Exception {
    OffsetIdPrinterParser pp = new OffsetIdPrinterParser("Z", pattern);
    int result = pp.parse(parseContext, "OTHER" + parse + ":OTHER", 5);
    assertEquals(result, parse.length() + 5);
    assertParsed(expected);
}
 
Example #22
Source File: TestStandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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);
}
 
Example #23
Source File: StandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<ZoneOffset> getValidOffsets(LocalDateTime localDateTime) {
    // should probably be optimized
    Object info = getOffsetInfo(localDateTime);
    if (info instanceof ZoneOffsetTransition) {
        return ((ZoneOffsetTransition) info).getValidOffsets();
    }
    return Collections.singletonList((ZoneOffset) info);
}
 
Example #24
Source File: TestZoneOffsetParser.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void test_parse_caseSensitiveUTC_matchedCase() throws Exception {
    parseContext.setCaseSensitive(true);
    OffsetIdPrinterParser pp = new OffsetIdPrinterParser("Z", "+HH:MM:ss");
    int result = pp.parse(parseContext, "Z", 0);
    assertEquals(result, 1);
    assertParsed(ZoneOffset.UTC);
}
 
Example #25
Source File: TestDateTimeFormatters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="sample_isoTime")
public void test_parse_isoTime(
        Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId,
        String input, Class<?> invalid) {
    if (input != null) {
        Expected expected = createTime(hour, min, sec, nano);
        if (offsetId != null) {
            expected.fieldValues.put(OFFSET_SECONDS, (long) ZoneOffset.of(offsetId).getTotalSeconds());
        }
        assertParseMatch(DateTimeFormatter.ISO_TIME.parseUnresolved(input, new ParsePosition(0)), expected);
    }
}
 
Example #26
Source File: TestZoneOffsetParser.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="offsets")
public void test_parse_midStringMatch_EmptyUTC(String pattern, String parse, ZoneOffset expected) throws Exception {
    OffsetIdPrinterParser pp = new OffsetIdPrinterParser("", pattern);
    int result = pp.parse(parseContext, "OTHER" + parse + ":OTHER", 5);
    assertEquals(result, parse.length() + 5);
    assertParsed(expected);
}
 
Example #27
Source File: TestZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void assertGap(ZoneRules test, int y, int m, int d, int hr, int min, ZoneOffset before, ZoneOffset after) {
    LocalDateTime dt = dateTime(y, m, d, hr, min);
    ZoneOffsetTransition zot = test.getTransition(dt);
    assertNotNull(zot);
    assertEquals(zot.isGap(), true);
    assertEquals(zot.getOffsetBefore(), before);
    assertEquals(zot.getOffsetAfter(), after);
}
 
Example #28
Source File: ZoneOffsetTransition.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Reads the state from the stream.
 *
 * @param in  the input stream, not null
 * @return the created object, not null
 * @throws IOException if an error occurs
 */
static ZoneOffsetTransition readExternal(DataInput in) throws IOException {
    long epochSecond = Ser.readEpochSec(in);
    ZoneOffset before = Ser.readOffset(in);
    ZoneOffset after = Ser.readOffset(in);
    if (before.equals(after)) {
        throw new IllegalArgumentException("Offsets must not be equal");
    }
    return new ZoneOffsetTransition(epochSecond, before, after);
}
 
Example #29
Source File: TestDateTimeParsing.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@DataProvider(name = "instantNoZone")
Object[][] data_instantNoZone() {
    return new Object[][] {
        {INSTANT, "2014-06-30T01:02:03Z", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).toInstant()},
        {INSTANTSECONDS, "86402", Instant.ofEpochSecond(86402)},
        {INSTANTSECONDS_NOS, "86402.123456789", Instant.ofEpochSecond(86402, 123456789)},
    };
}
 
Example #30
Source File: Ser.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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);
    }
}