/** * Return a DatePeriod instance from the provided options. * * @param mixed $from * @param mixed $to * @return \FreeAgent\Bitter\Date\DatePeriod */ public function periodForYear(DateTime $from, DateTime $to) { $from = $from instanceof DateTime ? $from : new Carbon($from); $to = $to instanceof DateTime ? $to : new Carbon($to); return DatePeriod::createForYear($from, $to); }
public function bitDateRange($key, $destKey, DateTime $from, DateTime $to) { if ($from > $to) { throw new Exception("DateTime from (" . $from->format('Y-m-d H:i:s') . ") must be anterior to DateTime to (" . $to->format('Y-m-d H:i:s') . ")."); } $from = clone $from; $to = clone $to; $this->getRedisClient()->del($this->prefixTempKey . $destKey); // Minutes $minutesFrom = DatePeriod::createForMinute($from, $to, DatePeriod::CREATE_FROM); foreach ($minutesFrom as $date) { $this->bitOpOr($destKey, new Hour($key, $date), $destKey); } $minutesTo = DatePeriod::createForMinute($from, $to, DatePeriod::CREATE_TO); if (array_diff($minutesTo->toArray(true), $minutesFrom->toArray(true)) !== array_diff($minutesFrom->toArray(true), $minutesTo->toArray(true))) { foreach ($minutesTo as $date) { $this->bitOpOr($destKey, new Hour($key, $date), $destKey); } } // Hours $hoursFrom = DatePeriod::createForHour($from, $to, DatePeriod::CREATE_FROM); foreach ($hoursFrom as $date) { $this->bitOpOr($destKey, new Hour($key, $date), $destKey); } $hoursTo = DatePeriod::createForHour($from, $to, DatePeriod::CREATE_TO); if (array_diff($hoursTo->toArray(true), $hoursFrom->toArray(true)) !== array_diff($hoursFrom->toArray(true), $hoursTo->toArray(true))) { foreach ($hoursTo as $date) { $this->bitOpOr($destKey, new Hour($key, $date), $destKey); } } // Days $daysFrom = DatePeriod::createForDay($from, $to, DatePeriod::CREATE_FROM); foreach ($daysFrom as $date) { $this->bitOpOr($destKey, new Day($key, $date), $destKey); } $daysTo = DatePeriod::createForDay($from, $to, DatePeriod::CREATE_TO); if (array_diff($daysTo->toArray(true), $daysFrom->toArray(true)) !== array_diff($daysFrom->toArray(true), $daysTo->toArray(true))) { foreach ($daysTo as $date) { $this->bitOpOr($destKey, new Day($key, $date), $destKey); } } // Months $monthsFrom = DatePeriod::createForMonth($from, $to, DatePeriod::CREATE_FROM); foreach ($monthsFrom as $date) { $this->bitOpOr($destKey, new Month($key, $date), $destKey); } $monthsTo = DatePeriod::createForMonth($from, $to, DatePeriod::CREATE_TO); if (array_diff($monthsTo->toArray(true), $monthsFrom->toArray(true)) !== array_diff($monthsFrom->toArray(true), $monthsTo->toArray(true))) { foreach ($monthsTo as $date) { $this->bitOpOr($destKey, new Month($key, $date), $destKey); } } // Years $years = DatePeriod::createForYear($from, $to); foreach ($years as $date) { $this->bitOpOr($destKey, new Year($key, $date), $destKey); } $this->getRedisClient()->sadd($this->prefixTempKey . 'keys', $destKey); $this->getRedisClient()->expire($destKey, $this->expireTimeout); return $this; }
public function testCreateForYear() { $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-13 10:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-13 15:30:45'); $datePeriodArray = array(); $this->if($datePeriod = TestedDatePeriod::createForYear($from, $to))->array($datePeriod->toArray(true))->isEqualTo($datePeriodArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-10 10:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-13 15:30:45'); $datePeriodArray = array(); $this->if($datePeriod = TestedDatePeriod::createForYear($from, $to))->array($datePeriod->toArray(true))->isEqualTo($datePeriodArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-10-10 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-15 10:30:45'); $datePeriodArray = array(); $this->if($datePeriod = TestedDatePeriod::createForYear($from, $to))->array($datePeriod->toArray(true))->isEqualTo($datePeriodArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-09-10 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-15 10:30:45'); $datePeriodArray = array(); $this->if($datePeriod = TestedDatePeriod::createForYear($from, $to))->array($datePeriod->toArray(true))->isEqualTo($datePeriodArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-08-20 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-10 10:30:45'); $datePeriodArray = array(); $this->if($datePeriod = TestedDatePeriod::createForYear($from, $to))->array($datePeriod->toArray(true))->isEqualTo($datePeriodArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2011-08-20 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-10 10:30:45'); $datePeriodArray = array(); $this->if($datePeriod = TestedDatePeriod::createForYear($from, $to))->array($datePeriod->toArray(true))->isEqualTo($datePeriodArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2010-08-20 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-10 10:30:45'); $datePeriodArray = array('2011-01-01 00:00:00'); $this->if($datePeriod = TestedDatePeriod::createForYear($from, $to))->array($datePeriod->toArray(true))->isEqualTo($datePeriodArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2009-08-20 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-10 10:30:45'); $datePeriodArray = array('2010-01-01 00:00:00', '2011-01-01 00:00:00'); $this->if($datePeriod = TestedDatePeriod::createForYear($from, $to))->array($datePeriod->toArray(true))->isEqualTo($datePeriodArray); }