예제 #1
0
 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_adjuster_Year()
 {
     $ldt = LocalDateTime::of(2008, 6, 30, 23, 30, 59, 0);
     $base = ZonedDateTime::ofDateTime($ldt, self::ZONE_0100());
     $test = $base->adjust(Year::of(2007));
     $this->assertEquals($test, ZonedDateTime::ofDateTime($ldt->withYear(2007), self::ZONE_0100()));
 }
 /**
  * @dataProvider provider_sampleDates
  */
 public function test_getDOY($y, $m, $d)
 {
     $a = LocalDateTime::of($y, $m, $d, 12, 30);
     $total = 0;
     for ($i = 1; $i < $m; $i++) {
         $total += Month::of($i)->length(Year::isLeapYear($y));
     }
     $doy = $total + $d;
     $this->assertEquals($a->getDayOfYear(), $doy);
 }
예제 #4
0
 public function test_with_Year_noChange_equal()
 {
     $test = YearMonth::of(2008, 6);
     $this->assertEquals($test->adjust(Year::of(2008)), $test);
 }
예제 #5
0
 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);
 }
예제 #6
0
 /**
  * Calculates the amount of time until another year in terms of the specified unit.
  * <p>
  * This calculates the amount of time between two {@code Year}
  * objects in terms of a single {@code TemporalUnit}.
  * The start and end points are {@code this} and the specified year.
  * The result will be negative if the end is before the start.
  * The {@code Temporal} passed to this method is converted to a
  * {@code Year} using {@link #from(TemporalAccessor)}.
  * For example, the amount in decades between two year can be calculated
  * using {@code startYear.until(endYear, DECADES)}.
  * <p>
  * The calculation returns a whole number, representing the number of
  * complete units between the two years.
  * For example, the amount in decades between 2012 and 2031
  * will only be one decade as it is one year short of two decades.
  * <p>
  * There are two equivalent ways of using this method.
  * The first is to invoke this method.
  * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
  * <pre>
  *   // these two lines are equivalent
  *   amount = start.until(end, YEARS);
  *   amount = YEARS.between(start, end);
  * </pre>
  * The choice should be made based on which makes the code more readable.
  * <p>
  * The calculation is implemented in this method for {@link ChronoUnit}.
  * The units {@code YEARS}, {@code DECADES}, {@code CENTURIES},
  * {@code MILLENNIA} and {@code ERAS} are supported.
  * Other {@code ChronoUnit} values will throw an exception.
  * <p>
  * If the unit is not a {@code ChronoUnit}, then the result of this method
  * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
  * passing {@code this} as the first argument and the converted input temporal
  * as the second argument.
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param Temporal $endExclusive the end date, exclusive, which is converted to a {@code Year}, not null
  * @param TemporalUnit $unit the unit to measure the amount in, not null
  * @return int the amount of time between this year and the end year
  * @throws DateTimeException if the amount cannot be calculated, or the end
  *  temporal cannot be converted to a {@code Year}
  * @throws UnsupportedTemporalTypeException if the unit is not supported
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function until(Temporal $endExclusive, TemporalUnit $unit)
 {
     $end = Year::from($endExclusive);
     if ($unit instanceof ChronoUnit) {
         $yearsUntil = $end->year - $this->year;
         // no overflow
         switch ($unit) {
             case ChronoUnit::YEARS():
                 return $yearsUntil;
             case ChronoUnit::DECADES():
                 return Math::div($yearsUntil, 10);
             case ChronoUnit::CENTURIES():
                 return Math::div($yearsUntil, 100);
             case ChronoUnit::MILLENNIA():
                 return Math::div($yearsUntil, 1000);
             case ChronoUnit::ERAS():
                 return $end->getLong(ChronoField::ERA()) - $this->getLong(ChronoField::ERA());
         }
         throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit);
     }
     return $unit->between($this, $end);
 }
예제 #7
0
 /**
  * Checks if the year is valid for this month-day.
  * <p>
  * This method checks whether this month and day and the input year form
  * a valid date. This can only return false for February 29th.
  *
  * @param int $year the year to validate
  * @return bool true if the year is valid for this month-day
  * @see Year#isValidMonthDay(MonthDay)
  */
 public function isValidYear($year)
 {
     return ($this->day == 29 && $this->month == 2 && Year::isLeapYear($year) == false) == false;
 }
예제 #8
0
 public function test_toString()
 {
     for ($i = -4; $i <= 2104; $i++) {
         $a = Year::of($i);
         $this->assertEquals($a->__toString(), "" . $i);
     }
 }
 /**
  * @expectedException \Celest\DateTimeException
  */
 public function test_print_isoOrdinalDate_missingField()
 {
     $test = Year::of(2008);
     DateTimeFormatter::ISO_ORDINAL_DATE()->format($test);
 }