From 8b5be46c252920f304d8b23e4f0c00db27485bf8 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Fri, 29 Oct 2010 02:57:37 +0200 Subject: [PATCH] use PHP 5.2+ DateTimeZone objects if available to get active daylight state Signed-off-by: Tanguy Pruvot --- blocks/SG_iCal_VTimeZone.php | 37 +++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/blocks/SG_iCal_VTimeZone.php b/blocks/SG_iCal_VTimeZone.php index 506f90a..77af852 100755 --- a/blocks/SG_iCal_VTimeZone.php +++ b/blocks/SG_iCal_VTimeZone.php @@ -65,20 +65,31 @@ class SG_iCal_VTimeZone { * @return string standard|daylight */ private function getActive( $ts ) { - if( isset($this->cache[$ts]) ) { + + if (class_exists('DateTimeZone')) { + + //PHP >= 5.2 + $tz = new DateTimeZone( $this->tzid ); + $date = new DateTime("@$ts", $tz); + return ($date->format('%I') == 1) ? 'daylight' : 'standard'; + + } else { + + if( isset($this->cache[$ts]) ) { + return $this->cache[$ts]; + } + + $daylight_freq = new SG_iCal_Freq($this->daylight['rrule'], strtotime($this->daylight['dtstart'])); + $standard_freq = new SG_iCal_Freq($this->standard['rrule'], strtotime($this->standard['dtstart'])); + $last_standard = $standard_freq->previousOccurrence($ts); + $last_dst = $daylight_freq->previousOccurrence($ts); + if( $last_dst > $last_standard ) { + $this->cache[$ts] = 'daylight'; + } else { + $this->cache[$ts] = 'standard'; + } + return $this->cache[$ts]; } - - $daylight_freq = new SG_iCal_Freq($this->daylight['rrule'], strtotime($this->daylight['dtstart'])); - $standard_freq = new SG_iCal_Freq($this->standard['rrule'], strtotime($this->standard['dtstart'])); - $last_standard = $standard_freq->previousOccurrence($ts); - $last_dst = $daylight_freq->previousOccurrence($ts); - if( $last_dst > $last_standard ) { - $this->cache[$ts] = 'daylight'; - } else { - $this->cache[$ts] = 'standard'; - } - - return $this->cache[$ts]; } }