Example #1
0
 /**
  * *@dataProvider providerToString
  *
  * @param integer $years    The number of years in the period.
  * @param integer $months   The number of months in the period.
  * @param integer $days     The number of days in the period.
  * @param string  $expected The expected string output.
  */
 public function testToString($years, $months, $days, $expected)
 {
     $this->assertSame($expected, (string) Period::of($years, $months, $days));
 }
Example #2
0
 /**
  * Calculates the period between this date and another date.
  *
  * This calculates the period between the two dates in terms of years, months and days.
  * The result will be negative if the end is before the start.
  * The negative sign will be the same in each of year, month and day.
  *
  * The start date is included, but the end date is not.
  * The period is calculated by removing complete months, then calculating
  * the remaining number of days, adjusting to ensure that both have the same sign.
  * The number of months is then normalized into years and months based on a 12 month year.
  * A month is considered to be complete if the end day-of-month is greater
  * than or equal to the start day-of-month.
  *
  * For example, from `2010-01-15` to `2011-03-18` is 1 year, 2 months and 3 days.
  *
  * @param LocalDate $endDateExclusive
  *
  * @return Period
  */
 public function until(LocalDate $endDateExclusive)
 {
     $totalMonths = $endDateExclusive->getProlepticMonth() - $this->getProlepticMonth();
     $days = $endDateExclusive->day - $this->day;
     if ($totalMonths > 0 && $days < 0) {
         $totalMonths--;
         $calcDate = $this->plusMonths($totalMonths);
         $days = $endDateExclusive->toEpochDay() - $calcDate->toEpochDay();
     } elseif ($totalMonths < 0 && $days > 0) {
         $totalMonths++;
         $days -= $endDateExclusive->getLengthOfMonth();
     }
     $years = intdiv($totalMonths, 12);
     $months = $totalMonths % 12;
     return Period::of($years, $months, $days);
 }
Example #3
0
 /**
  * @dataProvider providerPeriod
  *
  * @param integer $y  The year of the base date.
  * @param integer $m  The month of the base date.
  * @param integer $d  The day of the base date.
  * @param integer $py The number of years in the period.
  * @param integer $pm The number of months in the period.
  * @param integer $pd The number of days in the period.
  * @param integer $ey The expected year of the result date.
  * @param integer $em The expected month of the result date.
  * @param integer $ed The expected day of the result date.
  */
 public function testMinusPeriod($y, $m, $d, $py, $pm, $pd, $ey, $em, $ed)
 {
     $date = LocalDate::of($y, $m, $d);
     $period = Period::of(-$py, -$pm, -$pd);
     $this->assertLocalDateIs($ey, $em, $ed, $date->minusPeriod($period));
 }
Example #4
0
 /**
  * @dataProvider providerPeriod
  *
  * @param integer $y  The year of the base date.
  * @param integer $m  The month of the base date.
  * @param integer $d  The day of the base date.
  * @param integer $py The number of years in the period.
  * @param integer $pm The number of months in the period.
  * @param integer $pd The number of days in the period.
  * @param integer $ey The expected year of the result date.
  * @param integer $em The expected month of the result date.
  * @param integer $ed The expected day of the result date.
  */
 public function testMinusPeriod($y, $m, $d, $py, $pm, $pd, $ey, $em, $ed)
 {
     $dateTime = LocalDate::of($y, $m, $d)->atTime(LocalTime::of(12, 34, 56, 123456789));
     $period = Period::of($py, $pm, $pd);
     $this->assertLocalDateTimeIs($ey, $em, $ed, 12, 34, 56, 123456789, $dateTime->minusPeriod($period->negated()));
 }
Example #5
0
 /**
  * Returns a copy of this ZonedDateTime with the specified Period subtracted.
  *
  * @param Period $period
  *
  * @return ZonedDateTime
  */
 public function minusPeriod(Period $period)
 {
     return $this->plusPeriod($period->negated());
 }
Example #6
0
 /**
  * @param integer $years  The expected number of years in the period.
  * @param integer $months The expected number of months in the period.
  * @param integer $days   The expected number of days in the period.
  * @param Period  $period The period to test.
  */
 protected function assertPeriodIs($years, $months, $days, Period $period)
 {
     $this->compare([$years, $months, $days], [$period->getYears(), $period->getMonths(), $period->getDays()]);
 }