/** * Generate a datetime object via string * @todo Should this accept qCal_Date and qCal_DateTime objects? */ public static function factory($datetime, $timezone = null) { if (is_null($timezone) || !$timezone instanceof qCal_Timezone) { // @todo Make sure this doesn't cause any issues // detect if we're working with a UTC string like "19970101T180000Z", where the Z means use UTC time if (strtolower(substr($datetime, -1)) == "z") { $timezone = "UTC"; } $timezone = qCal_Timezone::factory($timezone); } // get the default timezone so we can set it back to it later $tz = date_default_timezone_get(); // set the timezone to GMT temporarily date_default_timezone_set("GMT"); // handles unix timestamp if (is_integer($datetime) || ctype_digit((string) $datetime)) { $timestamp = $datetime; } else { // handles just about any string representation of date/time (strtotime) if (is_string($datetime) || empty($datetime)) { if (!($timestamp = strtotime($datetime))) { // if unix timestamp can't be created throw an exception throw new qCal_DateTime_Exception("Invalid or ambiguous date/time string passed to qCal_DateTime::factory()"); } } } if (!isset($timestamp)) { throw new qCal_DateTime_Exception("Could not generate a qCal_DateTime object."); } list($year, $month, $day, $hour, $minute, $second) = explode("|", gmdate("Y|m|d|H|i|s", $timestamp)); // set the timezone back to what it was date_default_timezone_set($tz); return new qCal_DateTime($year, $month, $day, $hour, $minute, $second, $timezone); }
/** * Time rolls over similar to how qCal_Date rolls over */ public function testTimeRollover() { $time = new qCal_Time(1, 1, 100, qCal_Timezone::factory("GMT"), true); // should rollover to 1:02:40 $this->assertEqual($time->getTimestamp(), 3760); }
/** * Generate a qCal_Time object via a string or a number of other methods */ public static function factory($time, $timezone = null) { if (is_null($timezone) || !$timezone instanceof qCal_Timezone) { $timezone = qCal_Timezone::factory($timezone); } // get the default timezone so we can set it back to it later $tz = date_default_timezone_get(); // set the timezone to GMT temporarily date_default_timezone_set("GMT"); if (is_integer($time)) { // @todo Handle timestamps // @maybe not... } if (is_string($time)) { if ($time == "now") { $time = new qCal_Time(null, null, null, $timezone); } else { $tstring = "01/01/1970 {$time}"; if (!($timestamp = strtotime($tstring))) { // if unix timestamp can't be created throw an exception throw new qCal_DateTime_Exception_InvalidTime("Invalid or ambiguous time string passed to qCal_Time::factory()"); } list($hour, $minute, $second) = explode(":", gmdate("H:i:s", $timestamp)); $time = new qCal_Time($hour, $minute, $second, $timezone); } } // set the timezone back to what it was date_default_timezone_set($tz); return $time; }
/** * GMT is an offset of zero. Test that it works as it should. */ public function testTimezoneSetToGMT() { $timezone = qCal_Timezone::factory("GMT"); $this->assertEqual($timezone->getName(), "GMT"); $this->assertEqual($timezone->getOffset(), 0); }
/** * Test qCal_Timezone examples */ public function testFactoryExamples() { $gmt = qCal_Timezone::factory("GMT"); // will result in Greenwich Mean Time $this->assertEqual($gmt->getOffsetSeconds(), 0); $la = qCal_Timezone::factory("America/Los_Angeles"); // will result in timezone for America/Los_Angeles, which is eight hours behind UTC $this->assertEqual($la->getOffsetSeconds(), -28800); $eastern = qCal_Timezone::factory("US/Eastern"); // will result in the timezone for Eastern Standard Time, which is five hours behind UTC $this->assertEqual($eastern->getOffsetSeconds(), 3600 * -5); $defaultTZ = qCal_Timezone::factory(); // will result in whatever timezone your server is currently set to $dfOffset = date("Z"); $this->assertEqual($gmt->getOffsetSeconds(), $dfOffset); }
public function testWeeklyRecurrenceRules() { /** This is a rule for the days my mom works (if she worked once every four weeks) DTSTART;TZID=US-Pacific:20100215T080000 RRULE:FREQ=WEEKLY;BYDAY=MO,TU;BYHOUR=8; INTERVAL=4 */ $start = new qCal_DateTime(2010, 2, 15, 8, 0, 0, qCal_Timezone::factory('US/Pacific')); $recur = qCal_DateTime_Recur::factory('weekly', $start); $recur->setInterval(4)->setCount(25)->addRule(new qCal_DateTime_Recur_Rule_ByDay(array('MO', 'TU')))->addRule(new qCal_DateTime_Recur_Rule_ByHour(8))->rewind(); // is this even necessary? It shouldn't be... $this->assertEqual($recur->count(), 25); $this->assertEqual($recur->current()->format('Y-m-d H:i:s'), '2008-02-15 08:00:00'); $this->assertEqual($recur->next()->format('Y-m-d H:i:s'), '2008-02-16 08:00:00'); $this->assertEqual($recur->next()->format('Y-m-d H:i:s'), '2008-03-15 08:00:00'); $this->assertEqual($recur->next()->format('Y-m-d H:i:s'), '2008-03-16 08:00:00'); $this->assertEqual($recur->next()->format('Y-m-d H:i:s'), '2008-04-12 08:00:00'); $this->assertEqual($recur->next()->format('Y-m-d H:i:s'), '2008-04-13 08:00:00'); }