org.threeten.bp.LocalTime Java Examples

The following examples show how to use org.threeten.bp.LocalTime. 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: ZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
ZoneRulesBuilder addRuleToWindow(
        int startYear,
        int endYear,
        Month month,
        int dayOfMonthIndicator,
        DayOfWeek dayOfWeek,
        LocalTime time,
        int adjustDays,
        TimeDefinition timeDefinition,
        int savingAmountSecs) {
    Jdk8Methods.requireNonNull(month, "month");
    Jdk8Methods.requireNonNull(timeDefinition, "timeDefinition");
    YEAR.checkValidValue(startYear);
    YEAR.checkValidValue(endYear);
    if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {
        throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
    }
    if (windowList.isEmpty()) {
        throw new IllegalStateException("Must add a window before adding a rule");
    }
    TZWindow window = windowList.get(windowList.size() - 1);
    window.addRule(startYear, endYear, month, dayOfMonthIndicator, dayOfWeek, time, adjustDays, timeDefinition, savingAmountSecs);
    return this;
}
 
Example #2
Source File: TestMinguoChronology.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unused")
@Test(dataProvider="samples")
public void test_MinguoDate(ChronoLocalDate minguoDate, LocalDate iso) {
    ChronoLocalDate hd = minguoDate;
    ChronoLocalDateTime<?> hdt = hd.atTime(LocalTime.NOON);
    ZoneOffset zo = ZoneOffset.ofHours(1);
    ChronoZonedDateTime<?> hzdt = hdt.atZone(zo);
    hdt = hdt.plus(1, ChronoUnit.YEARS);
    hdt = hdt.plus(1, ChronoUnit.MONTHS);
    hdt = hdt.plus(1, ChronoUnit.DAYS);
    hdt = hdt.plus(1, ChronoUnit.HOURS);
    hdt = hdt.plus(1, ChronoUnit.MINUTES);
    hdt = hdt.plus(1, ChronoUnit.SECONDS);
    hdt = hdt.plus(1, ChronoUnit.NANOS);
    ChronoLocalDateTime<?> a2 = hzdt.toLocalDateTime();
    ChronoLocalDate a3 = a2.toLocalDate();
    ChronoLocalDate a5 = hzdt.toLocalDate();
    //System.out.printf(" d: %s, dt: %s; odt: %s; zodt: %s; a4: %s%n", date, hdt, hodt, hzdt, a5);
}
 
Example #3
Source File: TestStandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
}
 
Example #4
Source File: TestChronoLocalDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badDateTimeFieldChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(1900, 1, 1);
    ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalField adjuster = new FixedDateTimeField(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.with(adjuster, 1);
                Assert.fail("DateTimeField doSet should have thrown a ClassCastException" + cdt.getClass()
                        + ", can not be cast to " + cdt2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.with(adjuster, 1);
            assertEquals(result, cdt2, "DateTimeField doSet failed to replace date");
        }
    }
}
 
Example #5
Source File: TestChronoLocalDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badPlusPeriodUnitChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(1900, 1, 1);
    ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalUnit adjuster = new FixedPeriodUnit(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.plus(1, adjuster);
                Assert.fail("PeriodUnit.doAdd plus should have thrown a ClassCastException" + cdt
                        + ", can not be cast to " + cdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.plus(1, adjuster);
            assertEquals(result, cdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #6
Source File: TestChronoLocalDateTime.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);
    ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalAmount adjuster = new FixedAdjuster(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.minus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + cdt + ", supplied: " + cdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.minus(adjuster);
            assertEquals(result, cdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #7
Source File: TestChronoLocalDateTime.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);
    ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalAmount adjuster = new FixedAdjuster(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.plus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + cdt + ", supplied: " + cdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.plus(adjuster);
            assertEquals(result, cdt2, "WithAdjuster failed to replace date time");
        }
    }
}
 
Example #8
Source File: TestChronoLocalDateTime.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);
    ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalAdjuster adjuster = new FixedAdjuster(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.with(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + cdt + ", supplied: " + cdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.with(adjuster);
            assertEquals(result, cdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #9
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_badDateTimeFieldChrono(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);
        TemporalField adjuster = new FixedDateTimeField(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.with(adjuster, 1);
                Assert.fail("DateTimeField adjustInto() should have thrown a ClassCastException, " + czdt.getClass()
                        + " can not be cast to " + czdt2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.with(adjuster, 1);
            assertEquals(result, czdt2, "DateTimeField adjustInto() failed to replace date");
        }
    }
}
 
Example #10
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_badMinusPeriodUnitChrono(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);
        TemporalUnit adjuster = new FixedPeriodUnit(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.minus(1, adjuster);
                Assert.fail("PeriodUnit.doPlus minus should have thrown a ClassCastException, " + czdt.getClass()
                        + " can not be cast to " + czdt2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.minus(1, adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
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: ChronoLocalDateTimeImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private ChronoLocalDateTimeImpl<D> plusWithOverflow(D newDate, long hours, long minutes, long seconds, long nanos) {
    // 9223372036854775808 long, 2147483648 int
    if ((hours | minutes | seconds | nanos) == 0) {
        return with(newDate, time);
    }
    long totDays = nanos / NANOS_PER_DAY +             //   max/24*60*60*1B
            seconds / SECONDS_PER_DAY +                //   max/24*60*60
            minutes / MINUTES_PER_DAY +                //   max/24*60
            hours / HOURS_PER_DAY;                     //   max/24
    long totNanos = nanos % NANOS_PER_DAY +                    //   max  86400000000000
            (seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND +   //   max  86400000000000
            (minutes % MINUTES_PER_DAY) * NANOS_PER_MINUTE +   //   max  86400000000000
            (hours % HOURS_PER_DAY) * NANOS_PER_HOUR;          //   max  86400000000000
    long curNoD = time.toNanoOfDay();                          //   max  86400000000000
    totNanos = totNanos + curNoD;                              // total 432000000000000
    totDays += Jdk8Methods.floorDiv(totNanos, NANOS_PER_DAY);
    long newNoD = Jdk8Methods.floorMod(totNanos, NANOS_PER_DAY);
    LocalTime newTime = (newNoD == curNoD ? time : LocalTime.ofNanoOfDay(newNoD));
    return with(newDate.plus(totDays, ChronoUnit.DAYS), newTime);
}
 
Example #13
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 #14
Source File: ZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Obtains an instance defining the yearly rule to create transitions between two offsets.
 * <p>
 * Applications should normally obtain an instance from {@link ZoneRules}.
 * This factory is only intended for use when creating {@link ZoneRules}.
 *
 * @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 timeEndOfDay  whether the time is midnight at the end of day
 * @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
 * @return the rule, 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
 */
public static ZoneOffsetTransitionRule of(
        Month month,
        int dayOfMonthIndicator,
        DayOfWeek dayOfWeek,
        LocalTime time,
        boolean timeEndOfDay,
        TimeDefinition timeDefnition,
        ZoneOffset standardOffset,
        ZoneOffset offsetBefore,
        ZoneOffset offsetAfter) {
    Jdk8Methods.requireNonNull(month, "month");
    Jdk8Methods.requireNonNull(time, "time");
    Jdk8Methods.requireNonNull(timeDefnition, "timeDefnition");
    Jdk8Methods.requireNonNull(standardOffset, "standardOffset");
    Jdk8Methods.requireNonNull(offsetBefore, "offsetBefore");
    Jdk8Methods.requireNonNull(offsetAfter, "offsetAfter");
    if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {
        throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
    }
    if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) {
        throw new IllegalArgumentException("Time must be midnight when end of day flag is true");
    }
    return new ZoneOffsetTransitionRule(month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay ? 1 : 0, timeDefnition, standardOffset, offsetBefore, offsetAfter);
}
 
Example #15
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 #16
Source File: ZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 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 ZoneOffsetTransitionRule readExternal(DataInput in) throws IOException {
    int data = in.readInt();
    Month month = Month.of(data >>> 28);
    int dom = ((data & (63 << 22)) >>> 22) - 32;
    int dowByte = (data & (7 << 19)) >>> 19;
    DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte);
    int timeByte = (data & (31 << 14)) >>> 14;
    TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12];
    int stdByte = (data & (255 << 4)) >>> 4;
    int beforeByte = (data & (3 << 2)) >>> 2;
    int afterByte = (data & 3);
    int timeOfDaysSecs = (timeByte == 31 ? in.readInt() : timeByte * 3600);
    ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900));
    ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800));
    ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800));
    // only bit of validation that we need to copy from public of() method
    if (dom < -28 || dom > 31 || dom == 0) {
        throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
    }
    LocalTime time = LocalTime.ofSecondOfDay(Jdk8Methods.floorMod(timeOfDaysSecs, SECS_PER_DAY));
    int adjustDays = Jdk8Methods.floorDiv(timeOfDaysSecs, SECS_PER_DAY);
    return new ZoneOffsetTransitionRule(month, dom, dow, time, adjustDays, defn, std, before, after);
}
 
Example #17
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_badPlusPeriodUnitChrono(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);
        TemporalUnit adjuster = new FixedPeriodUnit(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.plus(1, adjuster);
                Assert.fail("PeriodUnit.doPlus plus should have thrown a ClassCastException, " + czdt
                        + " can not be cast to " + czdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.plus(1, adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #18
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 #19
Source File: TestChronoLocalDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badMinusPeriodUnitChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(1900, 1, 1);
    ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalUnit adjuster = new FixedPeriodUnit(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.minus(1, adjuster);
                Assert.fail("PeriodUnit.doAdd minus should have thrown a ClassCastException" + cdt.getClass()
                        + ", can not be cast to " + cdt2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.minus(1, adjuster);
            assertEquals(result, cdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #20
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_serialization_unusualTime() throws Exception {
    ZoneOffsetTransitionRule test = ZoneOffsetTransitionRule.of(
            Month.MARCH, 20, DayOfWeek.WEDNESDAY, LocalTime.of(13, 34, 56), false, TimeDefinition.STANDARD,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertSerializable(test);
}
 
Example #21
Source File: TripModel.java    From live-app-android with MIT License 5 votes vote down vote up
void update(@Nullable Trip trip) {
    if (trip == null || !tripId.equals(trip.getTripId())) return;
    mTrip = trip;
    tripReceived = LocalTime.now();
    mRemainingDuration = trip.getEstimate() == null
            ? null
            : (trip.getEstimate().getRoute() == null
                ? null
                : trip.getEstimate().getRoute().getRemainingDuration()
            );
}
 
Example #22
Source File: TestTzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_parseMonthDayTime_maySatAfter50220u() throws Exception {
    TzdbZoneRulesCompiler test = new TzdbZoneRulesCompiler("2010c", new ArrayList<File>(), null, false);
    TZDBRule mdt = parseMonthDayTime(test, "May Sat>=5 2:20u");
    assertEquals(mdt.month, Month.MAY);
    assertEquals(mdt.dayOfWeek, DayOfWeek.SATURDAY);
    assertEquals(mdt.dayOfMonth, 5);
    assertEquals(mdt.adjustForwards, true);
    assertEquals(mdt.time, LocalTime.of(2, 20));
    assertEquals(mdt.adjustDays, 0);
    assertEquals(mdt.timeDefinition, TimeDefinition.UTC);
}
 
Example #23
Source File: TestTzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_parseMonthDayTime_maySatBefore50220u() throws Exception {
    TzdbZoneRulesCompiler test = new TzdbZoneRulesCompiler("2010c", new ArrayList<File>(), null, false);
    TZDBRule mdt = parseMonthDayTime(test, "May Sat<=5 24:00g");
    assertEquals(mdt.month, Month.MAY);
    assertEquals(mdt.dayOfWeek, DayOfWeek.SATURDAY);
    assertEquals(mdt.dayOfMonth, 5);
    assertEquals(mdt.adjustForwards, false);
    assertEquals(mdt.time, LocalTime.of(0, 0));
    assertEquals(mdt.adjustDays, 1);
    assertEquals(mdt.timeDefinition, TimeDefinition.UTC);
}
 
Example #24
Source File: TestTzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_parseMonthDayTime_maySatBefore15Dash() throws Exception {
    TzdbZoneRulesCompiler test = new TzdbZoneRulesCompiler("2010c", new ArrayList<File>(), null, false);
    TZDBRule mdt = parseMonthDayTime(test, "May Sat<=15 -");
    assertEquals(mdt.month, Month.MAY);
    assertEquals(mdt.dayOfWeek, DayOfWeek.SATURDAY);
    assertEquals(mdt.dayOfMonth, 15);
    assertEquals(mdt.adjustForwards, false);
    assertEquals(mdt.time, LocalTime.of(0, 0));
    assertEquals(mdt.adjustDays, 0);
    assertEquals(mdt.timeDefinition, TimeDefinition.WALL);
}
 
Example #25
Source File: TestTzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_parseMonthDayTime_maylastSunShortTime() throws Exception {
    TzdbZoneRulesCompiler test = new TzdbZoneRulesCompiler("2010c", new ArrayList<File>(), null, false);
    TZDBRule mdt = parseMonthDayTime(test, "May lastSun 3z");
    assertEquals(mdt.month, Month.MAY);
    assertEquals(mdt.dayOfWeek, DayOfWeek.SUNDAY);
    assertEquals(mdt.dayOfMonth, -1);
    assertEquals(mdt.adjustForwards, false);
    assertEquals(mdt.time, LocalTime.of(3, 0));
    assertEquals(mdt.adjustDays, 0);
    assertEquals(mdt.timeDefinition, TimeDefinition.UTC);
}
 
Example #26
Source File: TestTzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_parseMonthDayTime_sepSatAfter82500() throws Exception {
    TzdbZoneRulesCompiler test = new TzdbZoneRulesCompiler("2018f", new ArrayList<File>(), null, false);
    TZDBRule mdt = parseMonthDayTime(test, "Sep Sat>=8 25:00");
    assertEquals(mdt.month, Month.SEPTEMBER);
    assertEquals(mdt.dayOfWeek, DayOfWeek.SATURDAY);
    assertEquals(mdt.dayOfMonth, 8);
    assertEquals(mdt.adjustForwards, true);
    assertEquals(mdt.time, LocalTime.of(1, 0));
    assertEquals(mdt.adjustDays, 1);
    assertEquals(mdt.timeDefinition, TimeDefinition.WALL);
}
 
Example #27
Source File: TestFractionPrinterParser.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void test_print_append() throws Exception {
    printContext.setDateTime(LocalTime.of(12, 30, 40, 3));
    FractionPrinterParser pp = new FractionPrinterParser(NANO_OF_SECOND, 0, 9, true);
    buf.append("EXISTING");
    pp.print(printContext, buf);
    assertEquals(buf.toString(), "EXISTING.000000003");
}
 
Example #28
Source File: TzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Parses a Rule line.
 *
 * @param st  the tokenizer, not null
 * @param mdt  the object to parse into, not null
 */
private void parseMonthDayTime(StringTokenizer st, TZDBMonthDayTime mdt) {
    mdt.month = parseMonth(st.nextToken());
    if (st.hasMoreTokens()) {
        String dayRule = st.nextToken();
        if (dayRule.startsWith("last")) {
            mdt.dayOfMonth = -1;
            mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(4));
            mdt.adjustForwards = false;
        } else {
            int index = dayRule.indexOf(">=");
            if (index > 0) {
                mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(0, index));
                dayRule = dayRule.substring(index + 2);
            } else {
                index = dayRule.indexOf("<=");
                if (index > 0) {
                    mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(0, index));
                    mdt.adjustForwards = false;
                    dayRule = dayRule.substring(index + 2);
                }
            }
            mdt.dayOfMonth = Integer.parseInt(dayRule);
        }
        if (st.hasMoreTokens()) {
            String timeStr = st.nextToken();
            int timeOfDaySecs = parseSecs(timeStr);
            LocalTime time = deduplicate(LocalTime.ofSecondOfDay(Jdk8Methods.floorMod(timeOfDaySecs, 86400)));
            mdt.time = time;
            mdt.adjustDays = Jdk8Methods.floorDiv(timeOfDaySecs, 86400);
            mdt.timeDefinition = parseTimeDefinition(timeStr.charAt(timeStr.length() - 1));
        }
    }
}
 
Example #29
Source File: ZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param year  the year
 * @param month  the month, not null
 * @param dayOfMonthIndicator  the day-of-month of the transition, adjusted by dayOfWeek,
 *   from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month
 * @param dayOfWeek  the day-of-week, null if day-of-month is exact
 * @param time  the time, not null
 * @param adjustDays  the time day adjustment
 * @param timeDefinition  the time definition, not null
 * @param savingAfterSecs  the savings amount in seconds
 */
TZRule(int year, Month month, int dayOfMonthIndicator,
        DayOfWeek dayOfWeek, LocalTime time, int adjustDays,
        TimeDefinition timeDefinition, int savingAfterSecs) {
    super();
    this.year = year;
    this.month = month;
    this.dayOfMonthIndicator = dayOfMonthIndicator;
    this.dayOfWeek = dayOfWeek;
    this.time= time;
    this.adjustDays= adjustDays;
    this.timeDefinition = timeDefinition;
    this.savingAmountSecs = savingAfterSecs;
}
 
Example #30
Source File: TestTzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_parseMonthDayTime_marLastSun0220() throws Exception {
    TzdbZoneRulesCompiler test = new TzdbZoneRulesCompiler("2010c", new ArrayList<File>(), null, false);
    TZDBRule mdt = parseMonthDayTime(test, "Mar lastSun 2:20");
    assertEquals(mdt.month, Month.MARCH);
    assertEquals(mdt.dayOfWeek, DayOfWeek.SUNDAY);
    assertEquals(mdt.dayOfMonth, -1);
    assertEquals(mdt.adjustForwards, false);
    assertEquals(mdt.time, LocalTime.of(2, 20));
    assertEquals(mdt.adjustDays, 0);
    assertEquals(mdt.timeDefinition, TimeDefinition.WALL);
}