Esempio n. 1
0
 /**
  * create (very simple) timezone and standard/daylight components
  *
  * Result when 'Europe/Stockholm' is used as timezone:
  *
  * BEGIN:VTIMEZONE
  * TZID:Europe/Stockholm
  * BEGIN:STANDARD
  * DTSTART:20101031T020000
  * TZOFFSETFROM:+0200
  * TZOFFSETTO:+0100
  * RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
  * TZNAME:CET
  * END:STANDARD
  * BEGIN:DAYLIGHT
  * DTSTART:20100328T030000
  * TZOFFSETFROM:+0100
  * TZOFFSETTO:+0200
  * RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
  * TZNAME:CEST
  * END:DAYLIGHT
  * END:VTIMEZONE
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.10.3 - 2011-08-01
  * @param object $calendar, reference to an iCalcreator calendar instance
  * @param string $timezone, a PHP5 (DateTimeZone) valid timezone
  * @param array $xProp, *[x-propName => x-propValue], optional
  * @return bool
  */
 public static function createTimezone(&$calendar, $timezone, $xProp = array())
 {
     if (!class_exists('DateTimeZone')) {
         return FALSE;
     }
     if (empty($timezone)) {
         return FALSE;
     }
     try {
         $dtz = new DateTimeZone($timezone);
     } catch (Exception $e) {
         return FALSE;
     }
     $stdDTSTART = $stdTZOFFSETTO = $stdTZOFFSETFROM = $stdTZNAME = $dlghtDTSTART = $dlghtTZOFFSETTO = $dlghtTZOFFSETFROM = $dlghtTZNAME = FALSE;
     $dateNow = new DateTime();
     $transitions = $dtz->getTransitions();
     foreach ($transitions as $trans) {
         if (FALSE === ($date = DateTime::createFromFormat('Y-m-d', substr($trans['time'], 0, 10)))) {
             continue;
         }
         if ($date > $dateNow) {
             break;
         }
         if (TRUE !== $trans['isdst']) {
             $stdDTSTART = $trans['time'];
             $stdTZOFFSETTO = $dlghtTZOFFSETFROM = iCal_UtilityFunctions::offsetSec2His($trans['offset']);
             $stdTZNAME = $trans['abbr'];
         } else {
             $dlghtDTSTART = $trans['time'];
             $dlghtTZOFFSETTO = $stdTZOFFSETFROM = iCal_UtilityFunctions::offsetSec2His($trans['offset']);
             $dlghtTZNAME = $trans['abbr'];
         }
     }
     if (!$stdDTSTART || !$stdTZOFFSETTO || !$stdTZOFFSETFROM) {
         return FALSE;
     }
     $tz =& $calendar->newComponent('vtimezone');
     $tz->setproperty('tzid', $timezone);
     if (!empty($xProp)) {
         foreach ($xProp as $xPropName => $xPropValue) {
             if ('x-' == strtolower(substr($xPropName, 0, 2))) {
                 $tz->setproperty($xPropName, $xPropValue);
             }
         }
     }
     $std =& $tz->newComponent('standard');
     $std->setProperty('dtstart', $stdDTSTART);
     if ($stdTZNAME) {
         $std->setProperty('tzname', $stdTZNAME);
     }
     $std->setProperty('tzoffsetto', $stdTZOFFSETTO);
     $std->setProperty('tzoffsetfrom', $stdTZOFFSETFROM);
     if ($stdTZOFFSETTO != $stdTZOFFSETFROM && FALSE === iCal_UtilityFunctions::_setTZrrule($std)) {
         $std->setProperty('RRULE', array('FREQ' => 'YEARLY', 'BYDAY' => array('-1', 'DAY' => 'SU'), 'BYMONTH' => 10));
     }
     if (!$dlghtDTSTART || !$dlghtTZOFFSETTO || !$dlghtTZOFFSETFROM || $dlghtTZOFFSETTO == $dlghtTZOFFSETFROM) {
         return TRUE;
     }
     $dlght =& $tz->newComponent('daylight');
     $dlght->setProperty('dtstart', $dlghtDTSTART);
     if ($dlghtTZNAME) {
         $dlght->setProperty('tzname', $dlghtTZNAME);
     }
     $dlght->setProperty('tzoffsetto', $dlghtTZOFFSETTO);
     $dlght->setProperty('tzoffsetfrom', $dlghtTZOFFSETFROM);
     if (FALSE === iCal_UtilityFunctions::_setTZrrule($dlght)) {
         $dlght->setProperty('RRULE', array('FREQ' => 'YEARLY', 'BYDAY' => array('-1', 'DAY' => 'SU'), 'BYMONTH' => 3));
     }
     return TRUE;
 }