private function toDateTime($year) { $this->adjustToFowards($year); if ($this->dayOfMonth === -1) { $dayOfMonth = $this->month->length(Year::isLeapYear($year)); $date = LocalDate::ofMonth($year, $this->month, $dayOfMonth); if ($this->dayOfWeek !== null) { $date = $date->adjust(TemporalAdjusters::previousOrSame($this->dayOfWeek)); } } else { $date = LocalDate::ofMonth($year, $this->month, $this->dayOfMonth); if ($this->dayOfWeek !== null) { $date = $date->adjust(TemporalAdjusters::nextOrSame($this->dayOfWeek)); } } $ldt = LocalDateTime::ofDateAndTime($date, $this->time); if ($this->endOfDay) { $ldt = $ldt->plusDays(1); } return $ldt; }
public function test_with_adjustment() { $sample = LocalDateTime::of(2012, 3, 4, 23, 5); $adjuster = TemporalAdjusters::fromCallable(function ($s) use($sample) { return $sample; }); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->adjust($adjuster), $sample); }
public function test_with_adjustment() { $sample = OffsetDateTime::ofDateAndTime(LocalDate::of(2012, 3, 4), LocalTime::of(23, 5), self::OFFSET_PONE()); $adjuster = TemporalAdjusters::fromCallable(function () use($sample) { return $sample; }); $this->assertEquals(self::TEST_2008_6_30_11_30_59_000000500()->adjust($adjuster), $sample); }
public function test_with_adjustment() { $sample = LocalDate::of(2012, 3, 4); $adjuster = TemporalAdjusters::fromCallable(function () use($sample) { return $sample; }); $this->assertEquals(self::TEST_2007_07_15()->adjust($adjuster), $sample); }
protected function resolveAligned(ChronoLocalDate $base, $months, $weeks, $dow) { $date = $base->plus($months, ChronoUnit::MONTHS())->plus($weeks, ChronoUnit::WEEKS()); if ($dow > 7) { $date = $date->plus(($dow - 1) / 7, ChronoUnit::WEEKS()); $dow = ($dow - 1) % 7 + 1; } else { if ($dow < 1) { $date = $date->plus(Math::subtractExact($dow, 7) / 7, ChronoUnit::WEEKS()); $dow = ($dow + 6) % 7 + 1; } } return $date->adjust(TemporalAdjusters::nextOrSame(DayOfWeek::of((int) $dow))); }
/** * @return LocalDate */ private function toLocalDate() { if ($this->dayOfMonthIndicator < 0) { $monthLen = $this->month->length(IsoChronology::INSTANCE()->isLeapYear($this->year)); $date = LocalDate::ofMonth($this->year, $this->month, $monthLen + 1 + $this->dayOfMonthIndicator); if ($this->dayOfWeek !== null) { $date = $date->adjust(TemporalAdjusters::previousOrSame($this->dayOfWeek)); } } else { $date = LocalDate::ofMonth($this->year, $this->month, $this->dayOfMonthIndicator); if ($this->dayOfWeek != null) { $date = $date->adjust(TemporalAdjusters::nextOrSame($this->dayOfWeek)); } } if ($this->timeEndOfDay) { $date = $date->plusDays(1); } return $date; }
/** * Creates a transition instance for the specified year. * <p> * Calculations are performed using the ISO-8601 chronology. * * @param int $year the year to create a transition for, not null * @return ZoneOffsetTransition the transition instance, not null */ public function createTransition($year) { if ($this->dom < 0) { $date = LocalDate::ofMonth($year, $this->month, $this->month->length(IsoChronology::INSTANCE()->isLeapYear($year)) + 1 + $this->dom); if ($this->dow !== null) { $date = $date->adjust(TemporalAdjusters::previousOrSame($this->dow)); } } else { $date = LocalDate::ofMonth($year, $this->month, $this->dom); if ($this->dow !== null) { $date = $date->adjust(TemporalAdjusters::nextOrSame($this->dow)); } } if ($this->timeEndOfDay) { $date = $date->plusDays(1); } $localDT = LocalDateTime::ofDateAndTime($date, $this->time); $transition = $this->timeDefinition->createDateTime($localDT, $this->standardOffset, $this->offsetBefore); return ZoneOffsetTransition::of($transition, $this->offsetBefore, $this->offsetAfter); }
/** * Obtains a {@code TemporalAdjuster} that wraps a date adjuster. * <p> * The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface. * This method allows an adjustment from {@code LocalDate} to {@code LocalDate} * to be wrapped to match the temporal-based interface. * This is provided for convenience to make user-written adjusters simpler. * <p> * In general, user-written adjusters should be static constants: * <pre>{@code * static TemporalAdjuster TWO_DAYS_LATER = * TemporalAdjusters.ofDateAdjuster(date -> date.plusDays(2)); * }</pre> * * @param $dateBasedAdjuster callable the date-based adjuster, not null * @return TemporalAdjuster the temporal adjuster wrapping on the date adjuster, not null */ public static function ofDateAdjuster(callable $dateBasedAdjuster) { return TemporalAdjusters::fromCallable(function (Temporal $temporal) use($dateBasedAdjuster) { $input = LocalDate::from($temporal); $output = $dateBasedAdjuster($input); return $temporal->adjust($output); }); }
public function test_with_adjustment() { $sample = LocalTime::of(23, 5); $adjuster = TemporalAdjusters::fromCallable(function (Temporal $dateTime) use($sample) { return $sample; }); $this->assertEquals(self::TEST_123040987654321()->adjust($adjuster), $sample); }
public function test_with_adjustment_AmPm() { $adjuster = TemporalAdjusters::fromCallable(function (Temporal $dateTime) { return $dateTime->with(CF::HOUR_OF_DAY(), 23); }); $test = self::TEST_11_30_59_500_PONE()->adjust($adjuster); $this->assertEquals($test, OffsetTime::of(23, 30, 59, 500, self::OFFSET_PONE())); }
public function test_previousOrCurrent() { foreach (Month::values() as $month) { for ($i = 1; $i <= $month->length(false); $i++) { $date = self::date(2007, $month, $i); foreach (DayOfWeek::values() as $dow) { $test = TemporalAdjusters::previousOrSame($dow)->adjustInto($date); $this->assertSame($test->getDayOfWeek(), $dow); if ($test->getYear() == 2007) { $dayDiff = $test->getDayOfYear() - $date->getDayOfYear(); $this->assertTrue($dayDiff <= 0 && $dayDiff > -7); $this->assertEquals($date->equals($test), $date->getDayOfWeek() == $dow); } else { $this->assertFalse($date->getDayOfWeek() == $dow); $this->assertSame($month, Month::JANUARY()); $this->assertTrue($date->getDayOfMonth() < 7); $this->assertEquals($test->getYear(), 2006); $this->assertSame($test->getMonth(), Month::DECEMBER()); $this->assertTrue($test->getDayOfMonth() > 25); } } } } }