/** * @dataProvider data_plusDays */ public function test_minus_TemporalAmount_Period_days(ZonedDateTime $base, $amount, ZonedDateTime $expected) { $this->assertEquals($base->minusAmount(Period::ofDays(-$amount)), $expected); }
function data_minus_TemporalAmount() { return [[YearMonth::of(1, 1), Period::ofYears(1), YearMonth::of(0, 1), null], [YearMonth::of(1, 1), Period::ofYears(-12), YearMonth::of(13, 1), null], [YearMonth::of(1, 1), Period::ofYears(0), YearMonth::of(1, 1), null], [YearMonth::of(999999999, 12), Period::ofYears(0), YearMonth::of(999999999, 12), null], [YearMonth::of(-999999999, 1), Period::ofYears(0), YearMonth::of(-999999999, 1), null], [YearMonth::of(0, 1), Period::ofYears(999999999), YearMonth::of(-999999999, 1), null], [YearMonth::of(0, 12), Period::ofYears(-999999999), YearMonth::of(999999999, 12), null], [YearMonth::of(1, 1), Period::ofMonths(1), YearMonth::of(0, 12), null], [YearMonth::of(1, 1), Period::ofMonths(-12), YearMonth::of(2, 1), null], [YearMonth::of(1, 1), Period::ofMonths(121), YearMonth::of(-10, 12), null], [YearMonth::of(1, 1), Period::ofMonths(0), YearMonth::of(1, 1), null], [YearMonth::of(999999999, 12), Period::ofMonths(0), YearMonth::of(999999999, 12), null], [YearMonth::of(-999999999, 1), Period::ofMonths(0), YearMonth::of(-999999999, 1), null], [YearMonth::of(-999999999, 2), Period::ofMonths(1), YearMonth::of(-999999999, 1), null], [YearMonth::of(999999999, 11), Period::ofMonths(-1), YearMonth::of(999999999, 12), null], [YearMonth::of(1, 1), Period::ofYears(1)->withMonths(2), YearMonth::of(-1, 11), null], [YearMonth::of(1, 1), Period::ofYears(-12)->withMonths(-1), YearMonth::of(13, 2), null], [YearMonth::of(1, 1), Period::ofMonths(2)->withYears(1), YearMonth::of(-1, 11), null], [YearMonth::of(1, 1), Period::ofMonths(-1)->withYears(-12), YearMonth::of(13, 2), null], [YearMonth::of(1, 1), Period::ofDays(365), null, DateTimeException::class], [YearMonth::of(1, 1), Duration::ofDays(365), null, DateTimeException::class], [YearMonth::of(1, 1), Duration::ofHours(365 * 24), null, DateTimeException::class], [YearMonth::of(1, 1), Duration::ofMinutes(365 * 24 * 60), null, DateTimeException::class], [YearMonth::of(1, 1), Duration::ofSeconds(365 * 24 * 3600), null, DateTimeException::class], [YearMonth::of(1, 1), Duration::ofNanos(365 * 24 * 3600 * 1000000000), null, DateTimeException::class]]; }
function data_toString() { return [[Period::ZERO(), "P0D"], [Period::ofDays(0), "P0D"], [Period::ofYears(1), "P1Y"], [Period::ofMonths(1), "P1M"], [Period::ofDays(1), "P1D"], [Period::of(1, 2, 0), "P1Y2M"], [Period::of(0, 2, 3), "P2M3D"], [Period::of(1, 2, 3), "P1Y2M3D"]]; }
private function resolveTime($hod, $moh, $som, $nos) { if ($this->resolverStyle == ResolverStyle::LENIENT()) { $totalNanos = Math::multiplyExact($hod, 3600000000000); $totalNanos = Math::addExact($totalNanos, Math::multiplyExact($moh, 60000000000)); $totalNanos = Math::addExact($totalNanos, Math::multiplyExact($som, 1000000000)); $totalNanos = Math::addExact($totalNanos, $nos); $excessDays = (int) Math::floorDiv($totalNanos, 86400000000000); // safe int cast $nod = Math::floorMod($totalNanos, 86400000000000); $this->updateCheckConflict(LocalTime::ofNanoOfDay($nod), Period::ofDays($excessDays)); } else { // STRICT or SMART $mohVal = CF::MINUTE_OF_HOUR()->checkValidIntValue($moh); $nosVal = CF::NANO_OF_SECOND()->checkValidIntValue($nos); // handle 24:00 end of day if ($this->resolverStyle == ResolverStyle::SMART() && $hod == 24 && $mohVal == 0 && $som == 0 && $nosVal == 0) { $this->updateCheckConflict(LocalTime::MIDNIGHT(), Period::ofDays(1)); } else { $hodVal = CF::HOUR_OF_DAY()->checkValidIntValue($hod); $somVal = CF::SECOND_OF_MINUTE()->checkValidIntValue($som); $this->updateCheckConflict(LocalTime::of($hodVal, $mohVal, $somVal, $nosVal), Period::ZERO()); } } }
/** * @dataProvider data_resolveClockHourOfDay */ public function test_resolveClockHourOfDay(ResolverStyle $style, $value, $expectedHour, $expectedDays) { $str = strval($value); $f = (new DateTimeFormatterBuilder())->appendValue(ChronoField::CLOCK_HOUR_OF_DAY())->toFormatter(); if ($expectedHour !== null) { $accessor = $f->withResolverStyle($style)->parse($str); $this->assertEquals($accessor->query(TemporalQueries::localDate()), null); $this->assertEquals($accessor->query(TemporalQueries::localTime()), LocalTime::of($expectedHour, 0)); $this->assertEquals($accessor->query(DateTimeFormatter::parsedExcessDays()), Period::ofDays($expectedDays)); } else { try { $f->withResolverStyle($style)->parse($str); $this->fail(); } catch (DateTimeParseException $ex) { // $expected } } }