/** * The base date for reduced value parsing. * @return LocalDate */ public static function BASE_DATE() { if (self::$BASE_DATE === null) { self::$BASE_DATE = LocalDate::of(2000, 1, 1); } return self::$BASE_DATE; }
public function test__print_fullDateTime() { $buf = ''; $this->builder->padNext2(3, '-')->appendLiteral('Z'); $this->getFormatter()->formatTo(LocalDate::of(2008, 12, 3), $buf); $this->assertEquals($buf, "--Z"); }
public function resolve(FieldValues $fieldValues, TemporalAccessor $partialTemporal, ResolverStyle $resolverStyle) { $wbyLong = $fieldValues->get(IsoFields::WEEK_BASED_YEAR()); $dowLong = $fieldValues->get(ChronoField::DAY_OF_WEEK()); if ($wbyLong === null || $dowLong === null) { return null; } $wby = IsoFields::WEEK_BASED_YEAR()->range()->checkValidIntValue($wbyLong, IsoFields::WEEK_BASED_YEAR()); // always validate $wowby = $fieldValues->get(IsoFields::WEEK_OF_WEEK_BASED_YEAR()); IsoFields::ensureIso($partialTemporal); $date = LocalDate::of($wby, 1, 4); if ($resolverStyle == ResolverStyle::LENIENT()) { $dow = $dowLong; // unvalidated if ($dow > 7) { $date = $date->plusWeeks(Math::div($dow - 1, 7)); $dow = ($dow - 1) % 7 + 1; } else { if ($dow < 1) { $date = $date->plusWeeks(Math::div(Math::subtractExact($dow, 7), 7)); $dow = ($dow + 6) % 7 + 1; } } $date = $date->plusWeeks(Math::subtractExact($wowby, 1))->with(ChronoField::DAY_OF_WEEK(), $dow); } else { $dow = ChronoField::DAY_OF_WEEK()->checkValidIntValue($dowLong); // validated if ($wowby < 1 || $wowby > 52) { if ($resolverStyle == ResolverStyle::STRICT()) { IsoFields::getWeekRange($date)->checkValidValue($wowby, $this); // only allow exact range } else { // SMART $this->range()->checkValidValue($wowby, $this); // allow 1-53 rolling into next year } } $date = $date->plusWeeks($wowby - 1)->with(ChronoField::DAY_OF_WEEK(), $dow); } $fieldValues->remove($this); $fieldValues->remove(IsoFields::WEEK_BASED_YEAR()); $fieldValues->remove(ChronoField::DAY_OF_WEEK()); return $date; }
public function adjustInto(Temporal $temporal, $newValue) { if ($this->isSupportedBy($temporal) === false) { throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear"); } $newWby = $this->range()->checkValidIntValue($newValue, IsoFields::WEEK_BASED_YEAR()); // strict check $date = LocalDate::from($temporal); $dow = $date->get(ChronoField::DAY_OF_WEEK()); $week = IsoFields::getWeek($date); if ($week == 53 && IsoFields::getWeekRangeInt($newWby) == 52) { $week = 52; } $resolved = LocalDate::of($newWby, 1, 4); // 4th is guaranteed to be in week one $days = $dow - $resolved->get(ChronoField::DAY_OF_WEEK()) + ($week - 1) * 7; $resolved = $resolved->plusDays($days); return $temporal->adjust($resolved); }
/** * Obtains a local date in Thai Buddhist calendar system from the * proleptic-year, month-of-year and day-of-month fields. * * @param int $prolepticYear the proleptic-year * @param int $month the month-of-year * @param int $dayOfMonth the day-of-month * @return ThaiBuddhistDate the Thai Buddhist local date, not null * @throws DateTimeException if unable to create the date */ public function date($prolepticYear, $month, $dayOfMonth) { return ThaiBuddhistDate::ofIsoDate(LocalDate::of($prolepticYear - self::YEARS_DIFFERENCE, $month, $dayOfMonth)); }
public function test_parseBest_secondOption() { $test = DateTimeFormatter::ofPattern("yyyy-MM-dd[ HH:mm[XXX]]"); $result = $test->parseBest("2011-06-30", TemporalQueries::fromCallable([ZonedDateTime::class, "from"]), TemporalQueries::fromCallable([LocalDate::class, 'from'])); $this->assertEquals($result, LocalDate::of(2011, 6, 30)); }
public static function init() { self::$MIN = LocalDate::of(Year::MIN_VALUE, 1, 1); self::$MAX = LocalDate::of(Year::MAX_VALUE, 12, 31); }
/** * @group long */ public function test_comparisons() { $this->comparisons_LocalDateTime([LocalDate::of(Year::MIN_VALUE, 1, 1), LocalDate::of(Year::MIN_VALUE, 12, 31), LocalDate::of(-1, 1, 1), LocalDate::of(-1, 12, 31), LocalDate::of(0, 1, 1), LocalDate::of(0, 12, 31), LocalDate::of(1, 1, 1), LocalDate::of(1, 12, 31), LocalDate::of(2008, 1, 1), LocalDate::of(2008, 2, 29), LocalDate::of(2008, 12, 31), LocalDate::of(Year::MAX_VALUE, 1, 1), LocalDate::of(Year::MAX_VALUE, 12, 31)]); }
function data_atEndOfMonth() { return [[YearMonth::of(2008, 1), LocalDate::of(2008, 1, 31)], [YearMonth::of(2008, 2), LocalDate::of(2008, 2, 29)], [YearMonth::of(2008, 3), LocalDate::of(2008, 3, 31)], [YearMonth::of(2008, 4), LocalDate::of(2008, 4, 30)], [YearMonth::of(2008, 5), LocalDate::of(2008, 5, 31)], [YearMonth::of(2008, 6), LocalDate::of(2008, 6, 30)], [YearMonth::of(2008, 12), LocalDate::of(2008, 12, 31)], [YearMonth::of(2009, 1), LocalDate::of(2009, 1, 31)], [YearMonth::of(2009, 2), LocalDate::of(2009, 2, 28)], [YearMonth::of(2009, 3), LocalDate::of(2009, 3, 31)], [YearMonth::of(2009, 4), LocalDate::of(2009, 4, 30)], [YearMonth::of(2009, 5), LocalDate::of(2009, 5, 31)], [YearMonth::of(2009, 6), LocalDate::of(2009, 6, 30)], [YearMonth::of(2009, 12), LocalDate::of(2009, 12, 31)]]; }
/** * @expectedException \Celest\DateTimeException */ public function test_factory_between_TemporalTemporal_invalidMixedTypes() { $start = Instant::ofEpochSecond(1); $end = LocalDate::of(2010, 6, 20); Duration::between($start, $end); }
public function test_atYear_int_leapYearAdjust() { $test = MonthDay::of(2, 29); $this->assertEquals($test->atYear(2005), LocalDate::of(2005, 2, 28)); }
public function test_adjustInto() { $this->assertEquals(DayOfWeek::MONDAY()->adjustInto(LocalDate::of(2012, 9, 2)), LocalDate::of(2012, 8, 27)); $this->assertEquals(DayOfWeek::MONDAY()->adjustInto(LocalDate::of(2012, 9, 3)), LocalDate::of(2012, 9, 3)); $this->assertEquals(DayOfWeek::MONDAY()->adjustInto(LocalDate::of(2012, 9, 4)), LocalDate::of(2012, 9, 3)); $this->assertEquals(DayOfWeek::MONDAY()->adjustInto(LocalDate::of(2012, 9, 10)), LocalDate::of(2012, 9, 10)); $this->assertEquals(DayOfWeek::MONDAY()->adjustInto(LocalDate::of(2012, 9, 11)), LocalDate::of(2012, 9, 10)); }
public function test_print_french_short() { $buf = ''; $this->getFormatterFieldStyle(ChronoField::MONTH_OF_YEAR(), TextStyle::SHORT())->withLocale(Locale::FRENCH())->formatTo(LocalDate::of(2012, 1, 1), $buf); $this->assertEquals("janv.", $buf); }
/** * Obtains a local date in Minguo calendar system from the * proleptic-year, month-of-year and day-of-month fields. * * @param int $prolepticYear the proleptic-year * @param int $month the month-of-year * @param int $dayOfMonth the day-of-month * @return MinguoDate the Minguo local date, not null * @throws DateTimeException if unable to create the date */ public function date($prolepticYear, $month, $dayOfMonth) { return MinguoDate::ofIsoDate(LocalDate::of($prolepticYear + self::YEARS_DIFFERENCE, $month, $dayOfMonth)); }
function data_week() { return [[LocalDate::of(1969, 12, 29), DayOfWeek::MONDAY(), 1, 1970], [LocalDate::of(2012, 12, 23), DayOfWeek::SUNDAY(), 51, 2012], [LocalDate::of(2012, 12, 24), DayOfWeek::MONDAY(), 52, 2012], [LocalDate::of(2012, 12, 27), DayOfWeek::THURSDAY(), 52, 2012], [LocalDate::of(2012, 12, 28), DayOfWeek::FRIDAY(), 52, 2012], [LocalDate::of(2012, 12, 29), DayOfWeek::SATURDAY(), 52, 2012], [LocalDate::of(2012, 12, 30), DayOfWeek::SUNDAY(), 52, 2012], [LocalDate::of(2012, 12, 31), DayOfWeek::MONDAY(), 1, 2013], [LocalDate::of(2013, 1, 1), DayOfWeek::TUESDAY(), 1, 2013], [LocalDate::of(2013, 1, 2), DayOfWeek::WEDNESDAY(), 1, 2013], [LocalDate::of(2013, 1, 6), DayOfWeek::SUNDAY(), 1, 2013], [LocalDate::of(2013, 1, 7), DayOfWeek::MONDAY(), 2, 2013]]; }
/** * Combines this month-day with a year to create a {@code LocalDate}. * <p> * This returns a {@code LocalDate} formed from this month-day and the specified year. * <p> * A month-day of February 29th will be adjusted to February 28th in the resulting * date if the year is not a leap year. * <p> * This instance is immutable and unaffected by this method call. * * @param int $year the year to use, from MIN_YEAR to MAX_YEAR * @return LocalDate the local date formed from this month-day and the specified year, not null * @throws DateTimeException if the year is outside the valid range of years */ public function atYear($year) { return LocalDate::of($year, $this->month, self::isValidYear($year) ? $this->day : 28); }
public function data_signStyle() { return [[LocalDate::of(0, 10, 2), SignStyle::ALWAYS(), null, "+00"], [LocalDate::of(2001, 10, 2), SignStyle::ALWAYS(), null, "+2001"], [LocalDate::of(-2001, 10, 2), SignStyle::ALWAYS(), null, "-2001"], [LocalDate::of(2001, 10, 2), SignStyle::NORMAL(), null, "2001"], [LocalDate::of(-2001, 10, 2), SignStyle::NORMAL(), null, "-2001"], [LocalDate::of(2001, 10, 2), SignStyle::NEVER(), null, "2001"], [LocalDate::of(-2001, 10, 2), SignStyle::NEVER(), null, "2001"], [LocalDate::of(2001, 10, 2), SignStyle::NOT_NEGATIVE(), null, "2001"], [LocalDate::of(-2001, 10, 2), SignStyle::NOT_NEGATIVE(), DateTimeException::class, ""], [LocalDate::of(0, 10, 2), SignStyle::EXCEEDS_PAD(), null, "00"], [LocalDate::of(1, 10, 2), SignStyle::EXCEEDS_PAD(), null, "01"], [LocalDate::of(-1, 10, 2), SignStyle::EXCEEDS_PAD(), null, "-01"], [LocalDate::of(20001, 10, 2), SignStyle::ALWAYS(), DateTimeException::class, ""], [LocalDate::of(20001, 10, 2), SignStyle::NORMAL(), DateTimeException::class, ""], [LocalDate::of(20001, 10, 2), SignStyle::NEVER(), DateTimeException::class, ""], [LocalDate::of(20001, 10, 2), SignStyle::EXCEEDS_PAD(), DateTimeException::class, ""], [LocalDate::of(20001, 10, 2), SignStyle::NOT_NEGATIVE(), DateTimeException::class, ""]]; }
/** * @group long */ public function test_loop() { // loop round at least one 400 $year cycle, including before 1970 $date = LocalDate::of(1960, 1, 5); // Tuseday of $week 1 1960 $year = 1960; $wby = 1960; $weekLen = 52; $week = 1; while ($date->getYear() < 2400) { $loopDow = $date->getDayOfWeek(); if ($date->getYear() != $year) { $year = $date->getYear(); } if ($loopDow == DayOfWeek::MONDAY()) { $week++; if ($week == 53 && $weekLen == 52 || $week == 54) { $week = 1; $firstDayOfWeekBasedYear = $date->plusDays(14)->withDayOfYear(1); $firstDay = $firstDayOfWeekBasedYear->getDayOfWeek(); $weekLen = $firstDay == DayOfWeek::THURSDAY() || $firstDay == DayOfWeek::WEDNESDAY() && $firstDayOfWeekBasedYear->isLeapYear() ? 53 : 52; $wby++; } } $this->assertEquals(IsoFields::WEEK_OF_WEEK_BASED_YEAR()->rangeRefinedBy($date), ValueRange::of(1, $weekLen), "Failed on " . $date . " " . $date->getDayOfWeek()); $this->assertEquals(IsoFields::WEEK_OF_WEEK_BASED_YEAR()->getFrom($date), $week, "Failed on " . $date . " " . $date->getDayOfWeek()); $this->assertEquals($date->get(IsoFields::WEEK_OF_WEEK_BASED_YEAR()), $week, "Failed on " . $date . " " . $date->getDayOfWeek()); $this->assertEquals(IsoFields::WEEK_BASED_YEAR()->getFrom($date), $wby, "Failed on " . $date . " " . $date->getDayOfWeek()); $this->assertEquals($date->get(IsoFields::WEEK_BASED_YEAR()), $wby, "Failed on " . $date . " " . $date->getDayOfWeek()); $date = $date->plusDays(1); } }
function resolveYMD(FieldValues $fieldValues, ResolverStyle $resolverStyle) { $y = CF::YEAR()->checkValidIntValue($fieldValues->remove(CF::YEAR())); if ($resolverStyle == ResolverStyle::LENIENT()) { $months = Math::subtractExact($fieldValues->remove(CF::MONTH_OF_YEAR()), 1); $days = Math::subtractExact($fieldValues->remove(CF::DAY_OF_MONTH()), 1); return LocalDate::of($y, 1, 1)->plusMonths($months)->plusDays($days); } $moy = CF::MONTH_OF_YEAR()->checkValidIntValue($fieldValues->remove(CF::MONTH_OF_YEAR())); $dom = CF::DAY_OF_MONTH()->checkValidIntValue($fieldValues->remove(CF::DAY_OF_MONTH())); if ($resolverStyle == ResolverStyle::SMART()) { // previous valid if ($moy == 4 || $moy == 6 || $moy == 9 || $moy == 11) { $dom = Math::min($dom, 30); } else { if ($moy == 2) { $dom = Math::min($dom, Month::FEBRUARY()->length(Year::isLeapYear($y))); } } } return LocalDate::of($y, $moy, $dom); }
function provider_patternLocalWeekBasedYearDate() { return [["e w Y", "6 29 2012", LocalDate::of(2012, 7, 20)], ["'Date: 'Y', day-of-week: 'e', week-of-year: 'w", "Date: 2012, day-of-week: 6, week-of-year: 29", LocalDate::of(2012, 7, 20)], ["Y-ww-ee", "2008-01-01", LocalDate::of(2007, 12, 30)], ["Y-w-e", "2008-52-1", LocalDate::of(2008, 12, 21)], ["Y-w-e", "2008-52-7", LocalDate::of(2008, 12, 27)], ["Y-ww-e", "2009-01-1", LocalDate::of(2008, 12, 28)], ["Y-w-e", "2009-1-4", LocalDate::of(2008, 12, 31)], ["Y-w-e", "2009-1-5", LocalDate::of(2009, 1, 1)], ["YYYYYYYYY-w-e", "000002009-1-5", LocalDate::of(2009, 1, 1)]]; }
/** * Obtains a {@code ThaiBuddhistDate} representing a date in the Thai Buddhist calendar * system from the proleptic-year, month-of-year and day-of-month fields. * <p> * This returns a {@code ThaiBuddhistDate} with the specified fields. * The day must be valid for the year and month, otherwise an exception will be thrown. * * @param int $prolepticYear the Thai Buddhist proleptic-year * @param int $month the Thai Buddhist month-of-year, from 1 to 12 * @param int $dayOfMonth the Thai Buddhist day-of-month, from 1 to 31 * @return ThaiBuddhistDate the date in Thai Buddhist calendar system, not null * @throws DateTimeException if the value of any field is out of range, * or if the day-of-month is invalid for the month-year */ public static function of($prolepticYear, $month, $dayOfMonth) { return new ThaiBuddhistDate(LocalDate::of($prolepticYear - ThaiBuddhistChronology::YEARS_DIFFERENCE, $month, $dayOfMonth)); }
public function resolve(FieldValues $fieldValues, TemporalAccessor $partialTemporal, ResolverStyle $resolverStyle) { $yearLong = $fieldValues->get(ChronoField::YEAR()); $qoyLong = $fieldValues->get(IsoFields::QUARTER_OF_YEAR()); if ($yearLong === null || $qoyLong === null) { return null; } $y = ChronoField::YEAR()->checkValidIntValue($yearLong); // always validate $doq = $fieldValues->get(IsoFields::DAY_OF_QUARTER()); IsoFields::ensureIso($partialTemporal); if ($resolverStyle == ResolverStyle::LENIENT()) { $date = LocalDate::of($y, 1, 1)->plusMonths(Math::multiplyExact(Math::subtractExact($qoyLong, 1), 3)); $doq = Math::subtractExact($doq, 1); } else { $qoy = IsoFields::QUARTER_OF_YEAR()->range()->checkValidIntValue($qoyLong, IsoFields::QUARTER_OF_YEAR()); // validated $date = LocalDate::of($y, ($qoy - 1) * 3 + 1, 1); if ($doq < 1 || $doq > 90) { if ($resolverStyle == ResolverStyle::STRICT()) { $this->rangeRefinedBy($date)->checkValidValue($doq, $this); // only allow exact range } else { // SMART $this->range()->checkValidValue($doq, $this); // allow 1-92 rolling into next quarter } } $doq--; } $fieldValues->remove($this); $fieldValues->remove(ChronoField::YEAR()); $fieldValues->remove(IsoFields::QUARTER_OF_YEAR()); return $date->plusDays($doq); }
public function test_until_parisLondon() { $midnightLondon = LocalDate::of(2012, 6, 28)->atStartOfDayWithZone(self::ZONE_LONDON()); $midnightParis1 = LocalDate::of(2012, 6, 29)->atStartOfDayWithZone(self::ZONE_PARIS()); $oneAm1 = LocalDateTime::of(2012, 6, 29, 1, 0)->atZone(self::ZONE_PARIS()); $midnightParis2 = LocalDate::of(2012, 6, 30)->atStartOfDayWithZone(self::ZONE_PARIS()); $this->assertEquals($midnightLondon->until($midnightParis1, CU::HOURS()), 23); $this->assertEquals($midnightLondon->until($oneAm1, CU::HOURS()), 24); $this->assertEquals($midnightLondon->until($midnightParis2, CU::HOURS()), 23 + 24); $this->assertEquals($midnightLondon->until($midnightParis1, CU::DAYS()), 0); $this->assertEquals($midnightLondon->until($oneAm1, CU::DAYS()), 1); $this->assertEquals($midnightLondon->until($midnightParis2, CU::DAYS()), 1); }
/** * @internal * @param int * @return int */ public static function getWeekRangeInt($wby) { $date = LocalDate::of($wby, 1, 1); // 53 weeks if standard year starts on Thursday, or Wed in a leap year if ($date->getDayOfWeek() == DayOfWeek::THURSDAY() || $date->getDayOfWeek() == DayOfWeek::WEDNESDAY() && $date->isLeapYear()) { return 53; } return 52; }
public function test_print_append() { $buf = "EXISTING"; $this->getFormatterWidth(ChronoField::DAY_OF_MONTH(), 1, 2, SignStyle::NEVER())->formatTo(LocalDate::of(2012, 1, 3), $buf); $this->assertEquals($buf, "EXISTING3"); }
private function date($year, $month, $day) { return LocalDate::of($year, $month, $day); }
public function test_Apia_jumpForwardOverInternationalDateLine_P12_to_M12() { // transition occurred at 1879-07-04T00:00+12:33:04 $test = $this->pacificApia(); $instantBefore = LocalDate::of(1879, 7, 2)->atStartOfDayWithZone(ZoneOffset::UTC())->toInstant(); $trans = $test->nextTransition($instantBefore); $this->assertEquals($trans->getDateTimeBefore(), LocalDateTime::of(1879, 7, 5, 0, 0)); $this->assertEquals($trans->getDateTimeAfter(), LocalDateTime::of(1879, 7, 4, 0, 0)); $this->assertEquals($trans->isGap(), false); $this->assertEquals($trans->isOverlap(), true); $this->assertEquals($trans->isValidOffset(ZoneOffset::ofHoursMinutesSeconds(+12, 33, 4)), true); $this->assertEquals($trans->isValidOffset(ZoneOffset::ofHoursMinutesSeconds(-11, -26, -56)), true); $this->assertEquals($trans->getDuration(), Duration::ofHours(-24)); $this->assertEquals($trans->getInstant(), LocalDateTime::of(1879, 7, 4, 0, 0)->toInstant(ZoneOffset::ofHoursMinutesSeconds(-11, -26, -56))); $zdt = ZonedDateTime::of(1879, 7, 4, 23, 0, 0, 0, ZoneId::of("Pacific/Apia")); $this->assertEquals($zdt->plusHours(2)->toLocalDateTime(), LocalDateTime::of(1879, 7, 4, 1, 0, 0)); }
public function test_range() { $this->assertEquals(CF::MONTH_OF_YEAR()->range(), ValueRange::of(1, 12)); $this->assertEquals(CF::MONTH_OF_YEAR()->rangeRefinedBy(LocalDate::of(2000, 2, 29)), ValueRange::of(1, 12)); $this->assertEquals(CF::DAY_OF_MONTH()->range(), ValueRange::ofVariable(1, 28, 31)); $this->assertEquals(CF::DAY_OF_MONTH()->rangeRefinedBy(LocalDate::of(2000, 2, 29)), ValueRange::of(1, 29)); }
/** * @expectedException \Celest\DateTimeException */ public function test_adjustInto_dateOnly() { $base = ZoneOffset::ofHoursMinutesSeconds(1, 1, 1); $base->adjustInto(LocalDate::of(1909, 2, 2)); }
public function test_withNanoOfSecond_normal() { $base = OffsetDateTime::ofDateAndTime(LocalDate::of(2008, 6, 30), LocalTime::of(11, 30, 59, 1), self::OFFSET_PONE()); $test = $base->withNano(15); $this->assertEquals($test, OffsetDateTime::ofDateAndTime(LocalDate::of(2008, 6, 30), LocalTime::of(11, 30, 59, 15), self::OFFSET_PONE())); }