Ejemplo n.º 1
0
 public function __construct(Date $base, $weekStart = Timestamp::WEEKDAY_MONDAY)
 {
     $firstDayOfMonth = Date::create($base->getYear() . '-' . $base->getMonth() . '-01');
     $lastDayOfMonth = Date::create($base->getYear() . '-' . $base->getMonth() . '-' . date('t', $base->toStamp()));
     $start = $firstDayOfMonth->getFirstDayOfWeek($weekStart);
     $end = $lastDayOfMonth->getLastDayOfWeek($weekStart);
     $this->monthRange = DateRange::create()->lazySet($firstDayOfMonth, $lastDayOfMonth);
     $this->fullRange = DateRange::create()->lazySet($start, $end);
     $rawDays = $this->fullRange->split();
     $this->fullLength = 0;
     foreach ($rawDays as $rawDay) {
         $day = CalendarDay::create($rawDay->toStamp());
         if ($this->monthRange->contains($day)) {
             $day->setOutside(false);
         } else {
             $day->setOutside(true);
         }
         $this->days[$day->toDate()] = $day;
         $weekNumber = floor($this->fullLength / 7);
         if (!isset($this->weeks[$weekNumber])) {
             $this->weeks[$weekNumber] = CalendarWeek::create();
         }
         $this->weeks[$weekNumber]->addDay($day);
         ++$this->fullLength;
     }
     ++$this->fullLength;
 }
Ejemplo n.º 2
0
 public static function makeDatesListByRange(DateRange $range, IntervalUnit $unit, $hash = true)
 {
     $date = $unit->truncate($range->getStart());
     if ('Date' == get_class($range->getStart())) {
         $date = Date::create($date->toStamp());
     }
     $dates = [];
     do {
         if ($hash) {
             $dates[$date->toString()] = $date;
         } else {
             $dates[] = $date;
         }
         $date = $date->spawn('+ 1' . $unit->getName());
     } while ($range->getEnd()->toStamp() >= $date->toStamp());
     return $dates;
 }
Ejemplo n.º 3
0
 public function countInRange(DateRange $range, $overlappedBounds = true)
 {
     $range = $range->toTimestampRange();
     $start = $this->truncate($range->getStart(), !$overlappedBounds);
     $end = $this->truncate($range->getEnd(), $overlappedBounds);
     if ($this->seconds) {
         $result = ($end->toStamp() - $start->toStamp()) / $this->seconds;
     } elseif ($this->days) {
         $epochStartTruncated = Date::create('1970-01-05');
         $startDifference = Date::dayDifference($epochStartTruncated, Date::create($start->toDate()));
         $endDifference = Date::dayDifference($epochStartTruncated, Date::create($end->toDate()));
         $result = ($endDifference - $startDifference) / $this->days;
     } elseif ($this->months) {
         $startMonthsCount = $start->getYear() * 12 + ($start->getMonth() - 1);
         $endMonthsCount = $end->getYear() * 12 + ($end->getMonth() - 1);
         $result = ($endMonthsCount - $startMonthsCount) / $this->months;
     }
     Assert::isEqual($result, (int) $result, 'floating point mistake, arguments: ' . $this->name . ', ' . $start->toStamp() . ', ' . $end->toStamp() . ', ' . 'result: ' . var_export($result, true));
     return (int) $result;
 }