Ejemplo n.º 1
0
 /**
  * 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);
 }
Ejemplo n.º 3
0
 /**
  * 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;
 }
 /**
  * Now test that registering the timezone prevents the exception
  */
 public function testCustomTimezoneRegister()
 {
     // now we register the timezone so that we can use it
     $timezone = new qCal_Timezone("Custom", "5400", "CSTM", false);
     qCal_Timezone::register($timezone);
     // Create a new time with the custom timezone. The time should now be 12:00:00 in the custom timezone...
     $time = new qCal_Time(0, 0, 0, "Custom");
     $this->assertEqual($time->getTimezone(), $timezone);
     $this->assertEqual($time->getTimezone()->getOffsetSeconds(), "5400");
     // $this->assertEqual($time->getTimestamp(), "5400");
     $this->assertEqual($time->__toString(), "00:00:00");
 }
 /**
  * Register a timezone object so that it can be referenced by name and qCal
  * components will be able to figure out how to apply its offset.
  * @param qCal_Timezone $timezone The timezone you want to register
  * @access public
  * @return void
  * @static
  */
 public static function register(qCal_Timezone $timezone)
 {
     self::$timezones[$timezone->getName()] = $timezone;
 }
 public function testTimezoneToStringOutput()
 {
     $tz = new qCal_Timezone("America/Los_Angeles", -28800, "PST");
     $tz->setFormat("T P");
     $this->assertEqual($tz->__toString(), "PST -08:00");
     $tz->setFormat("Z");
     $this->assertEqual($tz->__toString(), "-28800");
 }
 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');
 }