public function adjustInto(Temporal $temporal, $newValue) { // calls getFrom() to check if supported $curValue = $this->getFrom($temporal); $this->range()->checkValidValue($newValue, $this); // leniently check from 1 to 92 TODO: check return $temporal->with(ChronoField::DAY_OF_YEAR(), $temporal->getLong(ChronoField::DAY_OF_YEAR()) + ($newValue - $curValue)); }
function data_fieldAndAccessor() { return [[CF::YEAR(), LocalDate::of(2000, 2, 29), true, 2000], [CF::YEAR(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 2000], [CF::MONTH_OF_YEAR(), LocalDate::of(2000, 2, 29), true, 2], [CF::MONTH_OF_YEAR(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 2], [CF::DAY_OF_MONTH(), LocalDate::of(2000, 2, 29), true, 29], [CF::DAY_OF_MONTH(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 29], [CF::DAY_OF_YEAR(), LocalDate::of(2000, 2, 29), true, 60], [CF::DAY_OF_YEAR(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 60], [CF::HOUR_OF_DAY(), LocalTime::of(5, 4, 3, 200), true, 5], [CF::HOUR_OF_DAY(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 5], [CF::MINUTE_OF_DAY(), LocalTime::of(5, 4, 3, 200), true, 5 * 60 + 4], [CF::MINUTE_OF_DAY(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 5 * 60 + 4], [CF::MINUTE_OF_HOUR(), LocalTime::of(5, 4, 3, 200), true, 4], [CF::MINUTE_OF_HOUR(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 4], [CF::SECOND_OF_DAY(), LocalTime::of(5, 4, 3, 200), true, 5 * 3600 + 4 * 60 + 3], [CF::SECOND_OF_DAY(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 5 * 3600 + 4 * 60 + 3], [CF::SECOND_OF_MINUTE(), LocalTime::of(5, 4, 3, 200), true, 3], [CF::SECOND_OF_MINUTE(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 3], [CF::NANO_OF_SECOND(), LocalTime::of(5, 4, 3, 200), true, 200], [CF::NANO_OF_SECOND(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 200], [CF::YEAR(), LocalTime::of(5, 4, 3, 200), false, -1], [CF::MONTH_OF_YEAR(), LocalTime::of(5, 4, 3, 200), false, -1], [CF::DAY_OF_MONTH(), LocalTime::of(5, 4, 3, 200), false, -1], [CF::DAY_OF_YEAR(), LocalTime::of(5, 4, 3, 200), false, -1], [CF::HOUR_OF_DAY(), LocalDate::of(2000, 2, 29), false, -1], [CF::MINUTE_OF_DAY(), LocalDate::of(2000, 2, 29), false, -1], [CF::MINUTE_OF_HOUR(), LocalDate::of(2000, 2, 29), false, -1], [CF::SECOND_OF_DAY(), LocalDate::of(2000, 2, 29), false, -1], [CF::SECOND_OF_MINUTE(), LocalDate::of(2000, 2, 29), false, -1], [CF::NANO_OF_SECOND(), LocalDate::of(2000, 2, 29), false, -1]]; }
public function test_getLong_TemporalField() { $test = LocalDate::of(2008, 6, 30); $this->assertEquals($test->getLong(CF::YEAR()), 2008); $this->assertEquals($test->getLong(CF::MONTH_OF_YEAR()), 6); $this->assertEquals($test->getLong(CF::YEAR_OF_ERA()), 2008); $this->assertEquals($test->getLong(CF::ERA()), 1); $this->assertEquals($test->getLong(CF::PROLEPTIC_MONTH()), 2008 * 12 + 6 - 1); $this->assertEquals($test->getLong(CF::DAY_OF_MONTH()), 30); $this->assertEquals($test->getLong(CF::DAY_OF_WEEK()), 1); $this->assertEquals($test->getLong(CF::DAY_OF_YEAR()), 182); }
protected function resolveYD(FieldValues $fieldValues, ResolverStyle $resolverStyle) { $y = $this->range(ChronoField::YEAR())->checkValidIntValue($fieldValues->remove(ChronoField::YEAR()), ChronoField::YEAR()); if ($resolverStyle == ResolverStyle::LENIENT()) { $days = Math::subtractExact($fieldValues->remove(ChronoField::DAY_OF_YEAR()), 1); return $this->dateYearDay($y, 1)->plus($days, ChronoUnit::DAYS()); } $doy = $this->range(ChronoField::DAY_OF_YEAR())->checkValidIntValue($fieldValues->remove(ChronoField::DAY_OF_YEAR()), ChronoField::DAY_OF_YEAR()); return $this->dateYearDay($y, $doy); // smart is same as strict }
/** * Returns a copy of this date with the specified field set to a new value. * <p> * This returns a {@code LocalDate}, based on this one, with the value * for the specified field changed. * This can be used to change any supported field, such as the year, month or day-of-month. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p> * In some cases, changing the specified field can cause the resulting date to become invalid, * such as changing the month from 31st January to February would make the day-of-month invalid. * In cases like this, the field is responsible for resolving the date. Typically it will choose * the previous valid date, which would be the last valid day of February in this example. * <p> * If the field is a {@link ChronoField} then the adjustment is implemented here. * The supported fields behave as follows: * <ul> * <li>{@code DAY_OF_WEEK} - * Returns a {@code LocalDate} with the specified day-of-week. * The date is adjusted up to 6 days forward or backward within the boundary * of a Monday to Sunday week. * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} - * Returns a {@code LocalDate} with the specified aligned-day-of-week. * The date is adjusted to the specified month-based aligned-day-of-week. * Aligned weeks are counted such that the first week of a given month starts * on the first day of that month. * This may cause the date to be moved up to 6 days into the following month. * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} - * Returns a {@code LocalDate} with the specified aligned-day-of-week. * The date is adjusted to the specified year-based aligned-day-of-week. * Aligned weeks are counted such that the first week of a given year starts * on the first day of that year. * This may cause the date to be moved up to 6 days into the following year. * <li>{@code DAY_OF_MONTH} - * Returns a {@code LocalDate} with the specified day-of-month. * The month and year will be unchanged. If the day-of-month is invalid for the * year and month, then a {@code DateTimeException} is thrown. * <li>{@code DAY_OF_YEAR} - * Returns a {@code LocalDate} with the specified day-of-year. * The year will be unchanged. If the day-of-year is invalid for the * year, then a {@code DateTimeException} is thrown. * <li>{@code EPOCH_DAY} - * Returns a {@code LocalDate} with the specified epoch-day. * This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}. * <li>{@code ALIGNED_WEEK_OF_MONTH} - * Returns a {@code LocalDate} with the specified aligned-week-of-month. * Aligned weeks are counted such that the first week of a given month starts * on the first day of that month. * This adjustment moves the date in whole week chunks to match the specified week. * The result will have the same day-of-week as this date. * This may cause the date to be moved into the following month. * <li>{@code ALIGNED_WEEK_OF_YEAR} - * Returns a {@code LocalDate} with the specified aligned-week-of-year. * Aligned weeks are counted such that the first week of a given year starts * on the first day of that year. * This adjustment moves the date in whole week chunks to match the specified week. * The result will have the same day-of-week as this date. * This may cause the date to be moved into the following year. * <li>{@code MONTH_OF_YEAR} - * Returns a {@code LocalDate} with the specified month-of-year. * The year will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code PROLEPTIC_MONTH} - * Returns a {@code LocalDate} with the specified proleptic-month. * The day-of-month will be unchanged, unless it would be invalid for the new month * and year. In that case, the day-of-month is adjusted to the maximum valid value * for the new month and year. * <li>{@code YEAR_OF_ERA} - * Returns a {@code LocalDate} with the specified year-of-era. * The era and month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code YEAR} - * Returns a {@code LocalDate} with the specified year. * The month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code ERA} - * Returns a {@code LocalDate} with the specified era. * The year-of-era and month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. * <p> * This instance is immutable and unaffected by this method call. * * @param TemporalField $field the field to set in the result, not null * @param int $newValue the new value of the field in the result * @return LocalDate a {@code LocalDate} based on {@code this} with the specified field set, not null * @throws DateTimeException if the field cannot be set * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function with(TemporalField $field, $newValue) { if ($field instanceof ChronoField) { /** @var Chronofield $f */ $f = $field; $f->checkValidValue($newValue); switch ($f) { case ChronoField::DAY_OF_WEEK(): return $this->plusDays($newValue - $this->getDayOfWeek()->getValue()); case ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(): return $this->plusDays($newValue - $this->getLong(ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH())); case ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(): return $this->plusDays($newValue - $this->getLong(ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR())); case ChronoField::DAY_OF_MONTH(): return $this->withDayOfMonth((int) $newValue); case ChronoField::DAY_OF_YEAR(): return $this->withDayOfYear((int) $newValue); case ChronoField::EPOCH_DAY(): return LocalDate::ofEpochDay($newValue); case ChronoField::ALIGNED_WEEK_OF_MONTH(): return $this->plusWeeks($newValue - $this->getLong(ChronoField::ALIGNED_WEEK_OF_MONTH())); case ChronoField::ALIGNED_WEEK_OF_YEAR(): return $this->plusWeeks($newValue - $this->getLong(ChronoField::ALIGNED_WEEK_OF_YEAR())); case ChronoField::MONTH_OF_YEAR(): return $this->withMonth((int) $newValue); case ChronoField::PROLEPTIC_MONTH(): return $this->plusMonths($newValue - $this->getProlepticMonth()); case ChronoField::YEAR_OF_ERA(): return $this->withYear((int) ($this->year >= 1 ? $newValue : 1 - $newValue)); case ChronoField::YEAR(): return $this->withYear((int) $newValue); case ChronoField::ERA(): return $this->getLong(ChronoField::ERA()) == $newValue ? $this : $this->withYear(1 - $this->year); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->adjustInto($this, $newValue); }
public function test_parse_isoOrdinalDate_largeYear() { $expected = new Expected(ChronoField::YEAR(), 123456, ChronoField::DAY_OF_YEAR(), 123); $this->assertParseMatch(DateTimeFormatter::ISO_ORDINAL_DATE()->parseUnresolved("+123456-123", new ParsePosition(0)), $expected); }
function data_resolveThreeToDate() { return [[ChronoField::YEAR(), 2012, ChronoField::MONTH_OF_YEAR(), 2, ChronoField::DAY_OF_MONTH(), 1, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::ALIGNED_WEEK_OF_YEAR(), 5, ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(), 4, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::ALIGNED_WEEK_OF_YEAR(), 5, ChronoField::DAY_OF_WEEK(), 3, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::DAY_OF_YEAR(), 32, ChronoField::DAY_OF_MONTH(), 1, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR_OF_ERA(), 2012, ChronoField::DAY_OF_YEAR(), 32, ChronoField::DAY_OF_MONTH(), 1, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::DAY_OF_YEAR(), 32, ChronoField::DAY_OF_WEEK(), 3, LocalDate::of(2012, 2, 1)], [ChronoField::PROLEPTIC_MONTH(), 2012 * 12 + (2 - 1), ChronoField::DAY_OF_MONTH(), 25, ChronoField::DAY_OF_WEEK(), 6, LocalDate::of(2012, 2, 25)]]; }
public function test_getLong_TemporalField() { $test = LocalDateTime::of(2008, 6, 30, 12, 30, 40, 987654321); $this->assertEquals($test->getLong(CF::YEAR()), 2008); $this->assertEquals($test->getLong(CF::MONTH_OF_YEAR()), 6); $this->assertEquals($test->getLong(CF::DAY_OF_MONTH()), 30); $this->assertEquals($test->getLong(CF::DAY_OF_WEEK()), 1); $this->assertEquals($test->getLong(CF::DAY_OF_YEAR()), 182); $this->assertEquals($test->getLong(CF::HOUR_OF_DAY()), 12); $this->assertEquals($test->getLong(CF::MINUTE_OF_HOUR()), 30); $this->assertEquals($test->getLong(CF::SECOND_OF_MINUTE()), 40); $this->assertEquals($test->getLong(CF::NANO_OF_SECOND()), 987654321); $this->assertEquals($test->getLong(CF::HOUR_OF_AMPM()), 0); $this->assertEquals($test->getLong(CF::AMPM_OF_DAY()), 1); }
/** * Returns the "first day of next year" adjuster, which returns a new date set to * the first day of the next year. * <p> * The ISO calendar system behaves as follows:<br> * The input 2011-01-15 will return 2012-01-01. * <p> * The behavior is suitable for use with most calendar systems. * It is equivalent to: * <pre> * temporal.adjust(DAY_OF_YEAR, 1).plus(1, YEARS); * </pre> * * @return TemporalAdjuster the first day of next month adjuster, not null */ public static function firstDayOfNextYear() { return self::fromCallable(function (Temporal $temporal) { return $temporal->with(ChronoField::DAY_OF_YEAR(), 1)->plus(1, ChronoUnit::YEARS()); }); }
/** * @dataProvider provider_parseData */ public function test_parse_textField($minWidth, $maxWidth, SignStyle $signStyle, $subsequentWidth, $text, $pos, $expectedPos, $expectedValue) { $ppos = new ParsePosition($pos); $dtf = $this->getFormatterWidth(ChronoField::DAY_OF_WEEK(), $minWidth, $maxWidth, $signStyle); if ($subsequentWidth > 0) { // hacky, to reserve space $dtf = $this->builder->appendValue2(ChronoField::DAY_OF_YEAR(), $subsequentWidth)->toFormatter2($this->locale)->withDecimalStyle($this->decimalStyle); } $parsed = $dtf->parseUnresolved($text, $ppos); if ($ppos->getErrorIndex() != -1) { $this->assertEquals($ppos->getErrorIndex(), $expectedPos); } else { $this->assertTrue($subsequentWidth >= 0); $this->assertEquals($ppos->getIndex(), $expectedPos + $subsequentWidth); $this->assertEquals($parsed->getLong(ChronoField::DAY_OF_WEEK()), $expectedValue); $this->assertEquals($parsed->query(TemporalQueries::chronology()), null); $this->assertEquals($parsed->query(TemporalQueries::zoneId()), null); } }
/** * Map the field range to a week range of a week year. * @param TemporalAccessor $temporal the temporal * @return ValueRange the ValueRange with the range adjusted to weeks. */ private function rangeWeekOfWeekBasedYear(TemporalAccessor $temporal) { if (!$temporal->isSupported(CF::DAY_OF_YEAR())) { return self::WEEK_OF_YEAR_RANGE(); } $dow = $this->localizedDayOfWeek($temporal); $doy = $temporal->get(CF::DAY_OF_YEAR()); $offset = $this->startOfWeekOffset($doy, $dow); $week = $this->computeWeek($offset, $doy); if ($week === 0) { // Day is in end of week of previous year // Recompute from the last day of the previous year $date = AbstractChronology::from($temporal)->dateFrom($temporal); $date = $date->minus($doy + 7, ChronoUnit::DAYS()); // Back down into previous year return $this->rangeWeekOfWeekBasedYear($date); } // Check if day of year is in partial week associated with next year $dayRange = $temporal->range(CF::DAY_OF_YEAR()); $yearLen = $dayRange->getMaximum(); $newYearWeek = $this->computeWeek($offset, $yearLen + $this->weekDef->getMinimalDaysInFirstWeek()); if ($week >= $newYearWeek) { // Overlaps with weeks of following year; recompute from a week in following year $date = AbstractChronology::from($temporal)->dateFrom($temporal); $date = $date->plus($yearLen - $doy + 1 + 7, ChronoUnit::DAYS()); return $this->rangeWeekOfWeekBasedYear($date); } return ValueRange::of(1, $newYearWeek - 1); }
function data_normalized() { return [[ChronoField::YEAR(), 2127, ChronoField::YEAR(), 2127], [ChronoField::MONTH_OF_YEAR(), 12, ChronoField::MONTH_OF_YEAR(), 12], [ChronoField::DAY_OF_YEAR(), 127, ChronoField::DAY_OF_YEAR(), 127], [ChronoField::DAY_OF_MONTH(), 23, ChronoField::DAY_OF_MONTH(), 23], [ChronoField::DAY_OF_WEEK(), 127, ChronoField::DAY_OF_WEEK(), 127], [ChronoField::ALIGNED_WEEK_OF_YEAR(), 23, ChronoField::ALIGNED_WEEK_OF_YEAR(), 23], [ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(), 4, ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(), 4], [ChronoField::ALIGNED_WEEK_OF_MONTH(), 4, ChronoField::ALIGNED_WEEK_OF_MONTH(), 4], [ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), 3, ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), 3], [ChronoField::PROLEPTIC_MONTH(), 27, ChronoField::PROLEPTIC_MONTH(), null], [ChronoField::PROLEPTIC_MONTH(), 27, ChronoField::YEAR(), 2], [ChronoField::PROLEPTIC_MONTH(), 27, ChronoField::MONTH_OF_YEAR(), 4]]; }
public function test_parse_fieldRangeIgnored() { $pos = new ParsePosition(0); $parsed = $this->getFormatter0(ChronoField::DAY_OF_YEAR(), 3, 10)->parseUnresolved("456", $pos); $this->assertEquals($pos->getIndex(), 3); $this->assertParsed($parsed, ChronoField::DAY_OF_YEAR(), 456); // parsed dayOfYear=456 }
public function test_isSupported_TemporalField() { // TODO $this->assertEquals(self::TEST_2008_06()->isSupported(null), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::NANO_OF_SECOND()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::NANO_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MICRO_OF_SECOND()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MICRO_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MILLI_OF_SECOND()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MILLI_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::SECOND_OF_MINUTE()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::SECOND_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MINUTE_OF_HOUR()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MINUTE_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::HOUR_OF_AMPM()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::CLOCK_HOUR_OF_AMPM()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::HOUR_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::CLOCK_HOUR_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::AMPM_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::DAY_OF_WEEK()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ALIGNED_DAY_OF_WEEK_IN_MONTH()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ALIGNED_DAY_OF_WEEK_IN_YEAR()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::DAY_OF_MONTH()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::DAY_OF_YEAR()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::EPOCH_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ALIGNED_WEEK_OF_MONTH()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ALIGNED_WEEK_OF_YEAR()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MONTH_OF_YEAR()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::PROLEPTIC_MONTH()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::YEAR()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::YEAR_OF_ERA()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ERA()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::INSTANT_SECONDS()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::OFFSET_SECONDS()), false); }
/** * @dataProvider data_resolve_yd */ public function test_resolve_yd_strict($y, $d, $expected, $smar, $strict) { $fieldValues = new FieldValues(); $fieldValues->put(ChronoField::YEAR(), $y); $fieldValues->put(ChronoField::DAY_OF_YEAR(), $d); if ($strict) { $date = IsoChronology::INSTANCE()->resolveDate($fieldValues, ResolverStyle::STRICT()); $this->assertEquals($date, $expected); $this->assertEquals($fieldValues->size(), 0); } else { try { IsoChronology::INSTANCE()->resolveDate($fieldValues, ResolverStyle::STRICT()); $this->fail("Should have failed"); } catch (DateTimeException $ex) { // $expected } } }
public function range(TemporalField $field) { if ($field instanceof ChronoField) { if ($this->isSupported($field)) { $f = $field; switch ($f) { case CF::DAY_OF_MONTH(): case CF::DAY_OF_YEAR(): case CF::ALIGNED_WEEK_OF_MONTH(): return $this->isoDate->range($field); case CF::YEAR_OF_ERA(): $range = CF::YEAR()->range(); $max = $this->getProlepticYear() <= 0 ? -($range->getMinimum() + ThaiBuddhistChronology::YEARS_DIFFERENCE) + 1 : $range->getMaximum() + ThaiBuddhistChronology::YEARS_DIFFERENCE; return ValueRange::of(1, $max); } return $this->getChronology()->range($f); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->rangeRefinedBy($this); }
/** * @dataProvider data_weekFields * @group long */ public function test_fieldRanges(DayOfWeek $firstDayOfWeek, $minDays) { $weekDef = WeekFields::of($firstDayOfWeek, $minDays); $womField = $weekDef->weekOfMonth(); $woyField = $weekDef->weekOfYear(); $day = LocalDate::of(2012, 11, 30); $endDay = LocalDate::of(2013, 1, 2); while ($day->isBefore($endDay)) { $last = $day->with(CF::DAY_OF_MONTH(), $day->lengthOfMonth()); $lastWOM = $last->get($womField); $first = $day->with(CF::DAY_OF_MONTH(), 1); $firstWOM = $first->get($womField); $rangeWOM = $day->range($womField); $this->assertEquals($rangeWOM->getMinimum(), $firstWOM, "Range min should be same as WeekOfMonth for first day of month: " . $first . ", " . $weekDef); $this->assertEquals($rangeWOM->getMaximum(), $lastWOM, "Range max should be same as WeekOfMonth for last day of month: " . $last . ", " . $weekDef); $last = $day->with(CF::DAY_OF_YEAR(), $day->lengthOfYear()); $lastWOY = $last->get($woyField); $first = $day->with(CF::DAY_OF_YEAR(), 1); $firstWOY = $first->get($woyField); $rangeWOY = $day->range($woyField); $this->assertEquals($rangeWOY->getMinimum(), $firstWOY, "Range min should be same as WeekOfYear for first day of Year: " . $day . ", " . $weekDef); $this->assertEquals($rangeWOY->getMaximum(), $lastWOY, "Range max should be same as WeekOfYear for last day of Year: " . $day . ", " . $weekDef); $day = $day->plusDays(1); } }
public function test_getLong_TemporalField() { $test = ZonedDateTime::ofDateTime(LocalDateTime::of(2008, 6, 30, 12, 30, 40, 987654321), self::ZONE_0100()); $this->assertEquals($test->getLong(CF::YEAR()), 2008); $this->assertEquals($test->getLong(CF::MONTH_OF_YEAR()), 6); $this->assertEquals($test->getLong(CF::DAY_OF_MONTH()), 30); $this->assertEquals($test->getLong(CF::DAY_OF_WEEK()), 1); $this->assertEquals($test->getLong(CF::DAY_OF_YEAR()), 182); $this->assertEquals($test->getLong(CF::HOUR_OF_DAY()), 12); $this->assertEquals($test->getLong(CF::MINUTE_OF_HOUR()), 30); $this->assertEquals($test->getLong(CF::SECOND_OF_MINUTE()), 40); $this->assertEquals($test->getLong(CF::NANO_OF_SECOND()), 987654321); $this->assertEquals($test->getLong(CF::HOUR_OF_AMPM()), 0); $this->assertEquals($test->getLong(CF::AMPM_OF_DAY()), 1); $this->assertEquals($test->getLong(CF::OFFSET_SECONDS()), 3600); $this->assertEquals($test->getLong(CF::INSTANT_SECONDS()), $test->toEpochSecond()); }
public static function ISO_ORDINAL_DATE() { return self::$ISO_ORDINAL_DATE = (new DateTimeFormatterBuilder())->parseCaseInsensitive()->appendValue3(ChronoField::YEAR(), 4, 10, SignStyle::EXCEEDS_PAD())->appendLiteral('-')->appendValue2(ChronoField::DAY_OF_YEAR(), 3)->optionalStart()->appendOffsetId()->toFormatter3(ResolverStyle::STRICT(), IsoChronology::INSTANCE()); }
public function test_resolverFields_ignoreCrossCheck() { $base = (new DateTimeFormatterBuilder())->appendValue(CF::YEAR())->appendLiteral('-')->appendValue(CF::DAY_OF_YEAR())->appendLiteral('-')->appendValue(CF::DAY_OF_WEEK())->toFormatter(); $f = $base->withResolverFields(CF::YEAR(), CF::DAY_OF_YEAR()); try { $base->parseQuery("2012-321-1", TemporalQueries::fromCallable([LocalDate::class, 'from'])); // wrong day-of-week $this->fail(); } catch (DateTimeException $ex) { // $expected, should $this->fail() in cross-check of day-of-week } $parsed = $f->parseQuery("2012-321-1", TemporalQueries::fromCallable([LocalDate::class, 'from'])); // ignored wrong day-of-week $this->assertEquals($parsed, LocalDate::of(2012, 11, 16)); }
public static function init() { self::$FIELD_MAP = ['G' => ChronoField::ERA(), 'y' => ChronoField::YEAR_OF_ERA(), 'u' => ChronoField::YEAR(), 'Q' => IsoFields::QUARTER_OF_YEAR(), 'q' => IsoFields::QUARTER_OF_YEAR(), 'M' => ChronoField::MONTH_OF_YEAR(), 'L' => ChronoField::MONTH_OF_YEAR(), 'D' => ChronoField::DAY_OF_YEAR(), 'd' => ChronoField::DAY_OF_MONTH(), 'F' => ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), 'E' => ChronoField::DAY_OF_WEEK(), 'c' => ChronoField::DAY_OF_WEEK(), 'e' => ChronoField::DAY_OF_WEEK(), 'a' => ChronoField::AMPM_OF_DAY(), 'H' => ChronoField::HOUR_OF_DAY(), 'k' => ChronoField::CLOCK_HOUR_OF_DAY(), 'K' => ChronoField::HOUR_OF_AMPM(), 'h' => ChronoField::CLOCK_HOUR_OF_AMPM(), 'm' => ChronoField::MINUTE_OF_HOUR(), 's' => ChronoField::SECOND_OF_MINUTE(), 'S' => ChronoField::NANO_OF_SECOND(), 'A' => ChronoField::MILLI_OF_DAY(), 'n' => ChronoField::NANO_OF_SECOND(), 'N' => ChronoField::NANO_OF_DAY()]; }