Overview

Packages

  • Icalendar

Classes

  • Horde_Icalendar
  • Horde_Icalendar_Daylight
  • Horde_Icalendar_Exception
  • Horde_Icalendar_Standard
  • Horde_Icalendar_Translation
  • Horde_Icalendar_Valarm
  • Horde_Icalendar_Vcard
  • Horde_Icalendar_Vevent
  • Horde_Icalendar_Vfreebusy
  • Horde_Icalendar_Vjournal
  • Horde_Icalendar_Vnote
  • Horde_Icalendar_Vtimezone
  • Horde_Icalendar_Vtodo
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Class representing vTimezones.
  4:  *
  5:  * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  *
 10:  * @author   Mike Cochrane <mike@graftonhall.co.nz>
 11:  * @category Horde
 12:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 13:  * @package  Icalendar
 14:  */
 15: class Horde_Icalendar_Vtimezone extends Horde_Icalendar
 16: {
 17:     /**
 18:      * The component type of this class.
 19:      *
 20:      * @var string
 21:      */
 22:     public $type = 'vTimeZone';
 23: 
 24:     /**
 25:      * TODO
 26:      *
 27:      * @return TODO
 28:      */
 29:     public function exportvCalendar()
 30:     {
 31:         return $this->_exportvData('VTIMEZONE');
 32:     }
 33: 
 34:     /**
 35:      * Parse child components of the vTimezone component. Returns an
 36:      * array with the exact time of the time change as well as the
 37:      * 'from' and 'to' offsets around the change. Time is arbitrarily
 38:      * based on UTC for comparison.
 39:      *
 40:      * @param &$child TODO
 41:      * @param $year TODO
 42:      *
 43:      * @return TODO
 44:      */
 45:     public function parseChild(&$child, $year)
 46:     {
 47:         // Make sure 'time' key is first for sort().
 48:         $result['time'] = 0;
 49: 
 50:         try {
 51:             $t = $child->getAttribute('TZOFFSETFROM');
 52:         } catch (Horde_Icalendar_Exception $e) {
 53:             return false;
 54:         }
 55:         $result['from'] = ($t['hour'] * 60 * 60 + $t['minute'] * 60) * ($t['ahead'] ? 1 : -1);
 56: 
 57:         try {
 58:             $t = $child->getAttribute('TZOFFSETTO');
 59:         } catch (Horde_Icalendar_Exception $e) {
 60:             return false;
 61:         }
 62:         $result['to'] = ($t['hour'] * 60 * 60 + $t['minute'] * 60) * ($t['ahead'] ? 1 : -1);
 63: 
 64:         try {
 65:             $switch_time = $child->getAttribute('DTSTART');
 66:         } catch (Horde_Icalendar_Exception $e) {
 67:             return false;
 68:         }
 69: 
 70:         try {
 71:             $rrules = $child->getAttribute('RRULE');
 72:         } catch (Horde_Icalendar_Exception $e) {
 73:             if (!is_int($switch_time)) {
 74:                 return false;
 75:             }
 76:             // Convert this timestamp from local time to UTC for
 77:             // comparison (All dates are compared as if they are UTC).
 78:             $t = getdate($switch_time);
 79:             $result['time'] = @gmmktime($t['hours'], $t['minutes'], $t['seconds'],
 80:                                         $t['mon'], $t['mday'], $t['year']);
 81:             return $result;
 82:         }
 83: 
 84:         $rrules = explode(';', $rrules);
 85:         foreach ($rrules as $rrule) {
 86:             $t = explode('=', $rrule);
 87:             switch ($t[0]) {
 88:             case 'FREQ':
 89:                 if ($t[1] != 'YEARLY') {
 90:                     return false;
 91:                 }
 92:                 break;
 93: 
 94:             case 'INTERVAL':
 95:                 if ($t[1] != '1') {
 96:                     return false;
 97:                 }
 98:                 break;
 99: 
100:             case 'BYMONTH':
101:                 $month = intval($t[1]);
102:                 break;
103: 
104:             case 'BYDAY':
105:                 $len = strspn($t[1], '1234567890-+');
106:                 if ($len == 0) {
107:                     return false;
108:                 }
109:                 $weekday = substr($t[1], $len);
110:                 $weekdays = array(
111:                     'SU' => 0,
112:                     'MO' => 1,
113:                     'TU' => 2,
114:                     'WE' => 3,
115:                     'TH' => 4,
116:                     'FR' => 5,
117:                     'SA' => 6
118:                 );
119:                 $weekday = $weekdays[$weekday];
120:                 $which = intval(substr($t[1], 0, $len));
121:                 break;
122: 
123:             case 'UNTIL':
124:                 if (intval($year) > intval(substr($t[1], 0, 4))) {
125:                     return false;
126:                 }
127:                 break;
128:             }
129:         }
130: 
131:         if (empty($month) || !isset($weekday)) {
132:             return false;
133:         }
134: 
135:         if (is_int($switch_time)) {
136:             // Was stored as localtime.
137:             $switch_time = strftime('%H:%M:%S', $switch_time);
138:             $switch_time = explode(':', $switch_time);
139:         } else {
140:             $switch_time = explode('T', $switch_time);
141:             if (count($switch_time) != 2) {
142:                 return false;
143:             }
144:             $switch_time[0] = substr($switch_time[1], 0, 2);
145:             $switch_time[2] = substr($switch_time[1], 4, 2);
146:             $switch_time[1] = substr($switch_time[1], 2, 2);
147:         }
148: 
149:         // Get the timestamp for the first day of $month.
150:         $when = gmmktime($switch_time[0], $switch_time[1], $switch_time[2],
151:                          $month, 1, $year);
152:         // Get the day of the week for the first day of $month.
153:         $first_of_month_weekday = intval(gmstrftime('%w', $when));
154: 
155:         // Go to the first $weekday before first day of $month.
156:         if ($weekday >= $first_of_month_weekday) {
157:             $weekday -= 7;
158:         }
159:         $when -= ($first_of_month_weekday - $weekday) * 60 * 60 * 24;
160: 
161:         // If going backwards go to the first $weekday after last day
162:         // of $month.
163:         if ($which < 0) {
164:             do {
165:                 $when += 60*60*24*7;
166:             } while (intval(gmstrftime('%m', $when)) == $month);
167:         }
168: 
169:         // Calculate $weekday number $which.
170:         $when += $which * 60 * 60 * 24 * 7;
171: 
172:         $result['time'] = $when;
173: 
174:         return $result;
175:     }
176: 
177: }
178: 
API documentation generated by ApiGen