Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function expand($className, $value, array $options = [])
 {
     try {
         switch ($className) {
             case Duration::class:
                 return Duration::parse($value);
             case LocalDate::class:
                 return LocalDate::parse($value);
             case LocalTime::class:
                 return LocalTime::parse($value);
             case LocalDateTime::class:
                 return LocalDateTime::parse($value);
             case TimeZoneOffset::class:
                 return TimeZoneOffset::parse($value);
             case TimeZoneRegion::class:
                 return TimeZoneRegion::parse($value);
             case YearMonth::class:
                 return YearMonth::parse($value);
             case ZonedDateTime::class:
                 return ZonedDateTime::parse($value);
         }
     } catch (DateTimeParseException $e) {
         throw new ObjectNotConvertibleException($e->getMessage(), $e->getCode(), $e);
     }
     return null;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     return LocalDate::parse($value);
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function getField($field)
 {
     $value = $this->date->getField($field);
     if ($value !== null) {
         return $value;
     }
     return $this->time->getField($field);
 }
Esempio n. 4
0
 /**
  * Returns a Period consisting of the number of years, months, and days between two dates.
  *
  * 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 split into years and months based on a 12 month year.
  * A month is considered 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 one year, two months and three days.
  *
  * The result of this method can be a negative period if the end is before the start.
  * The negative sign will be the same in each of year, month and day.
  *
  * @param LocalDate $startInclusive
  * @param LocalDate $endExclusive
  *
  * @return Period
  */
 public static function between(LocalDate $startInclusive, LocalDate $endExclusive)
 {
     return $startInclusive->until($endExclusive);
 }
Esempio n. 5
0
 /**
  * @param TimeZone $timeZone
  *
  * @return DayOfWeek
  */
 public static function now(TimeZone $timeZone)
 {
     return LocalDate::now($timeZone)->getDayOfWeek();
 }
Esempio n. 6
0
 /**
  * Returns the number of days in this range.
  *
  * @return integer The number of days, >= 1.
  */
 public function count()
 {
     return $this->endDate->toEpochDay() - $this->startDate->toEpochDay() + 1;
 }
Esempio n. 7
0
 /**
  * Returns whether this LocalDateRange contains the given date.
  *
  * @param LocalDate $date The date to check.
  *
  * @return boolean True if this range contains the given date, false otherwise.
  */
 public function contains(LocalDate $date)
 {
     return !($date->isBefore($this->startDate) || $date->isAfter($this->endDate));
 }
Esempio n. 8
0
 /**
  * Extensive testing is done in LocalDate::until().
  */
 public function testBetween()
 {
     $period = Period::between(LocalDate::of(2010, 1, 15), LocalDate::of(2011, 3, 18));
     $this->assertPeriodIs(1, 2, 3, $period);
 }
Esempio n. 9
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);
 }
Esempio n. 10
0
 public function testToString()
 {
     $this->assertSame('2008-12-31/2011-01-01', (string) LocalDateRange::of(LocalDate::of(2008, 12, 31), LocalDate::of(2011, 1, 1)));
 }
Esempio n. 11
0
 /**
  * @expectedException \Brick\DateTime\DateTimeException
  */
 public function testMaxOfZeroElementsThrowsException()
 {
     LocalDate::maxOf();
 }
Esempio n. 12
0
 /**
  * Combines this month-day with a year to create a LocalDate.
  *
  * This returns a LocalDate formed from this month-day and the specified year.
  *
  * A month-day of February 29th will be adjusted to February 28th
  * in the resulting date if the year is not a leap year.
  *
  * @param integer $year
  *
  * @return LocalDate
  *
  * @throws DateTimeException If the year is invalid.
  */
 public function atYear($year)
 {
     return LocalDate::of($year, $this->month, $this->isValidYear($year) ? $this->day : 28);
 }
Esempio n. 13
0
 /**
  * @todo belongs to LocalDate tests
  *
  * @dataProvider providerGetDayOfWeekFromLocalDate
  *
  * @param string  $localDate The local date to test, as a string.
  * @param integer $dayOfWeek The day-of-week number that matches the local date.
  */
 public function testGetDayOfWeekFromLocalDate($localDate, $dayOfWeek)
 {
     $localDate = LocalDate::parse($localDate);
     $dayOfWeek = DayOfWeek::of($dayOfWeek);
     $this->assertTrue($localDate->getDayOfWeek()->isEqualTo($dayOfWeek));
 }
Esempio n. 14
0
 public function testAtDate()
 {
     $time = LocalTime::of(12, 34, 56, 789);
     $date = LocalDate::of(2014, 11, 30);
     $this->assertLocalDateTimeIs(2014, 11, 30, 12, 34, 56, 789, $time->atDate($date));
 }
Esempio n. 15
0
 /**
  * @dataProvider providerDuration
  *
  * @param integer $ds The seconds of the duration.
  * @param integer $dn The nano adjustment of the duration.
  * @param integer $y  The expected year.
  * @param integer $m  The expected month.
  * @param integer $d  The expected day.
  * @param integer $h  The exepected hour.
  * @param integer $i  The expected minute.
  * @param integer $s  The expected second.
  * @param integer $n  The expected nano.
  */
 public function testMinusDuration($ds, $dn, $y, $m, $d, $h, $i, $s, $n)
 {
     $localDateTime = LocalDate::of(2001, 2, 3)->atTime(LocalTime::of(4, 5, 6, 123456789));
     $duration = Duration::ofSeconds(-$ds, -$dn);
     $this->assertLocalDateTimeIs($y, $m, $d, $h, $i, $s, $n, $localDateTime->minusDuration($duration));
 }
Esempio n. 16
0
 /**
  * Combines this year with a day-of-year to create a LocalDate.
  *
  * @param integer $dayOfYear The day-of-year to use, from 1 to 366.
  *
  * @return LocalDate
  *
  * @throws DateTimeException If the day-of-year is invalid for this year.
  */
 public function atDay($dayOfYear)
 {
     return LocalDate::ofYearDay($this->year, $dayOfYear);
 }
Esempio n. 17
0
 /**
  * Combines this year-month with a day-of-month to create a LocalDate.
  *
  * @param integer $day The day-of-month to use, valid for the year-month.
  *
  * @return LocalDate The date formed from this year-month and the specified day.
  *
  * @throws DateTimeException If the day is not valid for this year-month.
  */
 public function atDay($day)
 {
     return LocalDate::of($this->year, $this->month, $day);
 }
Esempio n. 18
0
 /**
  * @param integer   $year  The expected year.
  * @param integer   $month The expected month.
  * @param integer   $day   The expected day.
  * @param LocalDate $date  The local date to test.
  */
 protected function assertLocalDateIs($year, $month, $day, LocalDate $date)
 {
     $this->compare([$year, $month, $day], [$date->getYear(), $date->getMonth(), $date->getDay()]);
 }