1: <?php // BUILD: Remove line
2:
3: namespace intouch\ical;
4:
5: /**
6: * The wrapper for vtimezones. Stores the timezone-id and the setup for
7: * daylight savings and standard time.
8: *
9: * @author Morten Fangel (C) 2008
10: * @author Michael Kahn (C) 2013
11: * @license http://creativecommons.org/licenses/by-sa/2.5/dk/deed.en_GB CC-BY-SA-DK
12: */
13: class VTimeZone {
14: protected $tzid;
15: protected $daylight;
16: protected $standard;
17: protected $cache = array();
18:
19: /**
20: * Constructs a new intouch\ical\VTimeZone
21: */
22: public function __construct( $data ) {
23: $this->tzid = $data['tzid'];
24: $this->daylight = $data['daylight'];
25: $this->standard = $data['standard'];
26: }
27:
28: /**
29: * Returns the timezone-id for this timezone. (Used to
30: * differentiate between different tzs in a calendar)
31: * @return string
32: */
33: public function getTimeZoneId() {
34: return $this->tzid;
35: }
36:
37: /**
38: * Returns the given offset in this timezone for the given
39: * timestamp. (eg +0200)
40: * @param int $ts
41: * @return string
42: */
43: public function getOffset( $ts ) {
44: $act = $this->getActive($ts);
45: return $this->{$act}['tzoffsetto'];
46: }
47:
48: /**
49: * Returns the timezone name for the given timestamp (eg CEST)
50: * @param int $ts
51: * @return string
52: */
53: public function getTimeZoneName($ts) {
54: $act = $this->getActive($ts);
55: return $this->{$act}['tzname'];
56: }
57:
58: /**
59: * Determines which of the daylight or standard is the active
60: * setting.
61: * The call is cached for a given timestamp, so a call to
62: * getOffset and getTimeZoneName with the same ts won't calculate
63: * the answer twice.
64: * @param int $ts
65: * @return string standard|daylight
66: */
67: private function getActive( $ts ) {
68:
69: if (class_exists('DateTimeZone')) {
70:
71: //PHP >= 5.2
72: $tz = new DateTimeZone( $this->tzid );
73: $date = new DateTime("@$ts", $tz);
74: return ($date->format('I') == 1) ? 'daylight' : 'standard';
75:
76: } else {
77:
78: if( isset($this->cache[$ts]) ) {
79: return $this->cache[$ts];
80: }
81:
82: $daylight_freq = new Freq($this->daylight['rrule'], strtotime($this->daylight['dtstart']));
83: $standard_freq = new Freq($this->standard['rrule'], strtotime($this->standard['dtstart']));
84: $last_standard = $standard_freq->previousOccurrence($ts);
85: $last_dst = $daylight_freq->previousOccurrence($ts);
86: if( $last_dst > $last_standard ) {
87: $this->cache[$ts] = 'daylight';
88: } else {
89: $this->cache[$ts] = 'standard';
90: }
91:
92: return $this->cache[$ts];
93: }
94: }
95: }
96: