~ubuntu-branches/ubuntu/vivid/php-horde-icalendar/vivid-proposed

« back to all changes in this revision

Viewing changes to Horde_Icalendar-2.0.5/lib/Horde/Icalendar/Vtimezone.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-05-14 20:46:43 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130514204643-xq5x2ezjku3om1um
Tags: 2.0.5-1
New upstream version 2.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Class representing vTimezones.
 
4
 *
 
5
 * Copyright 2003-2013 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 array();
 
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 array();
 
61
        }
 
62
        $result['to'] = ($t['hour'] * 60 * 60 + $t['minute'] * 60) * ($t['ahead'] ? 1 : -1);
 
63
 
 
64
        try {
 
65
            $start = $child->getAttribute('DTSTART');
 
66
        } catch (Horde_Icalendar_Exception $e) {
 
67
            return array();
 
68
        }
 
69
        if (!is_int($start)) {
 
70
            return array();
 
71
        }
 
72
        $start = getdate($start);
 
73
        if ($start['year'] > $year) {
 
74
            return array();
 
75
        }
 
76
 
 
77
        $results = array();
 
78
        try {
 
79
            $rdates = $child->getAttributeValues('RDATE');
 
80
            foreach ($rdates as $rdate) {
 
81
                if ($rdate['year'] == $year || $rdate['year'] == $year - 1) {
 
82
                    $result['time'] = gmmktime(
 
83
                    $start['hours'], $start['minutes'], $start['seconds'],
 
84
                    $rdate['month'], $rdate['mday'], $rdate['year']);
 
85
                    $results[] = $result;
 
86
                }
 
87
            }
 
88
        } catch (Horde_Icalendar_Exception $e) {
 
89
        }
 
90
 
 
91
        try {
 
92
            $rrules = $child->getAttribute('RRULE');
 
93
        } catch (Horde_Icalendar_Exception $e) {
 
94
            if (!$results) {
 
95
                $result['time'] = $start[0];
 
96
                $results[] = $result;
 
97
            }
 
98
            return $results;
 
99
        }
 
100
 
 
101
        $rrules = explode(';', $rrules);
 
102
        foreach ($rrules as $rrule) {
 
103
            $t = explode('=', $rrule);
 
104
            switch ($t[0]) {
 
105
            case 'FREQ':
 
106
                if ($t[1] != 'YEARLY') {
 
107
                    return array();
 
108
                }
 
109
                break;
 
110
 
 
111
            case 'INTERVAL':
 
112
                if ($t[1] != '1') {
 
113
                    return array();
 
114
                }
 
115
                break;
 
116
 
 
117
            case 'BYMONTH':
 
118
                $month = intval($t[1]);
 
119
                break;
 
120
 
 
121
            case 'BYDAY':
 
122
                $len = strspn($t[1], '1234567890-+');
 
123
                if ($len == 0) {
 
124
                    return array();
 
125
                }
 
126
                $weekday = substr($t[1], $len);
 
127
                $weekdays = array(
 
128
                    'SU' => 0,
 
129
                    'MO' => 1,
 
130
                    'TU' => 2,
 
131
                    'WE' => 3,
 
132
                    'TH' => 4,
 
133
                    'FR' => 5,
 
134
                    'SA' => 6
 
135
                );
 
136
                $weekday = $weekdays[$weekday];
 
137
                $which = intval(substr($t[1], 0, $len));
 
138
                break;
 
139
 
 
140
            case 'UNTIL':
 
141
                if (intval($year) > intval(substr($t[1], 0, 4))) {
 
142
                    return array();
 
143
                }
 
144
                break;
 
145
            }
 
146
        }
 
147
 
 
148
        if (empty($month) || !isset($weekday)) {
 
149
            return array();
 
150
        }
 
151
 
 
152
        // Get the timestamp for the first day of $month.
 
153
        $when = gmmktime($start['hours'], $start['minutes'], $start['seconds'],
 
154
                         $month, 1, $year);
 
155
        // Get the day of the week for the first day of $month.
 
156
        $first_of_month_weekday = intval(gmstrftime('%w', $when));
 
157
 
 
158
        // Go to the first $weekday before first day of $month.
 
159
        if ($weekday >= $first_of_month_weekday) {
 
160
            $weekday -= 7;
 
161
        }
 
162
        $when -= ($first_of_month_weekday - $weekday) * 60 * 60 * 24;
 
163
 
 
164
        // If going backwards go to the first $weekday after last day
 
165
        // of $month.
 
166
        if ($which < 0) {
 
167
            do {
 
168
                $when += 60*60*24*7;
 
169
            } while (intval(gmstrftime('%m', $when)) == $month);
 
170
        }
 
171
 
 
172
        // Calculate $weekday number $which.
 
173
        $when += $which * 60 * 60 * 24 * 7;
 
174
 
 
175
        $result['time'] = $when;
 
176
        $results[] = $result;
 
177
 
 
178
        return $results;
 
179
    }
 
180
 
 
181
}