Exemplo n.º 1
0
 /**
  * @param integer      $dayOfYear The day-of-year to check, validated as an integer.
  * @param integer|null $year      An optional year to check against, fully validated.
  *
  * @return void
  *
  * @throws DateTimeException If the day-of-year is not valid.
  */
 public static function check($dayOfYear, $year = null)
 {
     if ($dayOfYear < 1 || $dayOfYear > 366) {
         throw DateTimeException::fieldNotInRange(self::NAME, $dayOfYear, 1, 366);
     }
     if ($dayOfYear === 366 && $year !== null) {
         if (!Year::isLeap($year)) {
             throw new DateTimeException("Invalid day-of-year 366 as {$year} is not a leap year");
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Returns the length of the given month-of-year.
  *
  * If no year is given, the highest value (29) is returned for the month of February.
  *
  * @param integer      $monthOfYear The month-of-year, fully validated.
  * @param integer|null $year        An optional year the month-of-year belongs to, fully validated.
  *
  * @return integer
  */
 public static function getLength($monthOfYear, $year = null)
 {
     switch ($monthOfYear) {
         case 2:
             return $year === null || Year::isLeap($year) ? 29 : 28;
         case 4:
         case 6:
         case 9:
         case 11:
             return 30;
         default:
             return 31;
     }
 }
Exemplo n.º 3
0
 /**
  * Checks if the year is a leap year, according to the ISO proleptic calendar system rules.
  *
  * @return boolean
  */
 public function isLeapYear()
 {
     return Field\Year::isLeap($this->year);
 }
Exemplo n.º 4
0
 /**
  * Returns a copy of this YearMonth with the year altered.
  *
  * @param integer $year
  *
  * @return YearMonth
  */
 public function withYear($year)
 {
     $year = Cast::toInteger($year);
     if ($year === $this->year) {
         return $this;
     }
     Field\Year::check($year);
     return new YearMonth($year, $this->month);
 }
Exemplo n.º 5
0
 /**
  * Returns whether the given year is valid for this month-day.
  *
  * 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 integer $year
  *
  * @return boolean
  */
 public function isValidYear($year)
 {
     return $this->month !== 2 || $this->day !== 29 || Field\Year::isLeap($year);
 }
Exemplo n.º 6
0
 /**
  * Returns a copy of this year with the specified number of years subtracted.
  *
  * This instance is immutable and unaffected by this method call.
  *
  * @param integer $years The years to subtract, may be negative.
  *
  * @return Year A Year based on this year with the period subtracted.
  *
  * @throws DateTimeException If the resulting year exceeds the supported range.
  */
 public function minus($years)
 {
     $years = Cast::toInteger($years);
     if ($years === 0) {
         return $this;
     }
     $year = $this->year - $years;
     Field\Year::check($year);
     return new Year($year);
 }