/** * Provided a date/time object, use this recurrence's rules to determine * all of the recurrence times for the date and return them in an array. * @param qCal_Date The date object to find time recurrences for * @return array A list of time recurrences for the specified date/time * @access protected * @todo I don't really like the way this is done. Definitely a code smell here. * Each of the rules should do their own logic. Something like: * $seconds = $bySecond->getTimeInstances(); * $minutes = $byMinute->getTimeInstances($seconds); * $hours = $byHour->getTimeInstances($minutes); */ protected function findTimeRecurrences(qCal_Date $date) { // find all of the bySeconds $seconds = array(); if ($this->hasRule('qCal_DateTime_Recur_Rule_BySecond')) { $seconds = $this->getRule('qCal_DateTime_Recur_Rule_BySecond')->getValues(); sort($seconds); } else { $seconds = array($this->getStart()->getTime()->getSecond()); } // find all of the byMinutes $minutes = array(); if ($this->hasRule('qCal_DateTime_Recur_Rule_ByMinute')) { $minutesRules = $this->getRule('qCal_DateTime_Recur_Rule_ByMinute')->getValues(); sort($minutesRules); } else { $minutesRules = array($this->getStart()->getTime()->getMinute()); } foreach ($minutesRules as $minute) { $minutes[$minute] = $seconds; } // find all of the byHours $hours = array(); if ($this->hasRule('qCal_DateTime_Recur_Rule_ByHour')) { $hoursRules = $this->getRule('qCal_DateTime_Recur_Rule_ByHour')->getValues(); sort($hoursRules); } else { $hoursRules = array($this->getStart()->getTime()->getHour()); } foreach ($hoursRules as $hour) { $hours[$hour] = $minutes; } // create an array to store times $times = array(); foreach ($hours as $hour => $minutes) { foreach ($minutes as $minute => $seconds) { foreach ($seconds as $second) { try { // try to build a date/time object $datetime = new qCal_DateTime($date->getYear(), $date->getMonth(), $date->getDay(), $hour, $minute, $second); $times[$datetime->format('YmdHis')] = $datetime; } catch (qCal_DateTime_Exception_InvalidTime $e) { // if the date/time object instantiation fails, this exception will be thrown // @todo Recover from this error and report it. Maybe catch the error and pass it to a log or something? // qCal_Log::logException($e, get_class($this)); throw $e; } } } } return $times; }
/** * Test that format method allows date() function's meta-characters */ public function testDateTimeFormat() { $dt = new qCal_DateTime(2000, 10, 1, 5, 0, 0); $this->assertEqual($dt->format("m/d/Y H:i:s"), "10/01/2000 05:00:00"); }
public function testDateTimeConvertToString() { $datetime = new qCal_DateTime(2010, 1, 12, 4, 30, 0, "America/Los_Angeles"); $this->assertEqual($datetime->__toString(), "2010-01-12T04:30:00-08:00"); // will output "2010-01-12T04:00:00-08:00" $datetime = new qCal_DateTime(2010, 12, 10, 15, 30, 0, "GMT"); $datetime->setFormat('m/d/Y \\a\\t g:ia'); $this->assertEqual($datetime->__toString(), "12/10/2010 at 3:30pm"); // outputs "12/10/2010 at 3:30pm" $datetime->setFormat("H"); $this->assertEqual($datetime->__toString(), "15"); // outputs "15" $datetime = new qCal_DateTime(2010, 11, 10, 6, 30, 0, "GMT"); $string = $datetime->format("H:i"); $this->assertEqual($datetime->__toString(), "2010-11-10T06:30:00+00:00"); // still outputs "2010-11-10T06:30:00+00:00" because we did not call setFormat() $this->assertEqual($string, "06:30"); // outputs "06:30" }