From 5806719d752788e28e0c2ba07ae8b2f35f501c86 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Tue, 2 Jun 2009 10:48:18 -0400 Subject: [PATCH] move static methods to a Utils class --- framework/Date/lib/Horde/Date.php | 54 ++--------------------- framework/Date/lib/Horde/Date/Recurrence.php | 56 +++++++++++++----------- framework/Date/lib/Horde/Date/Utils.php | 65 ++++++++++++++++++++++++++++ framework/Date/test/Horde/Date/UtilsTest.php | 8 ++-- 4 files changed, 103 insertions(+), 80 deletions(-) create mode 100644 framework/Date/lib/Horde/Date/Utils.php diff --git a/framework/Date/lib/Horde/Date.php b/framework/Date/lib/Horde/Date.php index 6612c6aa1..bb011e421 100644 --- a/framework/Date/lib/Horde/Date.php +++ b/framework/Date/lib/Horde/Date.php @@ -518,54 +518,6 @@ class Horde_Date } /** - * Returns whether a year is a leap year. - * - * @param integer $year The year. - * - * @return boolan True if the year is a leap year. - */ - public static function isLeapYear($year) - { - if (strlen($year) != 4 || preg_match('/\D/', $year)) { - return false; - } - - return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0); - } - - /** - * Returns the date of the year that corresponds to the first day of the - * given week. - * - * @param integer $week The week of the year to find the first day of. - * @param integer $year The year to calculate for. - * - * @return Horde_Date The date of the first day of the given week. - */ - public static function firstDayOfWeek($week, $year) - { - return new Horde_Date(sprintf('%04dW%02d', $year, $week)); - } - - /** - * Returns the number of days in the specified month. - * - * @param integer $month The month - * @param integer $year The year. - * - * @return integer The number of days in the month. - */ - public static function daysInMonth($month, $year) - { - static $cache = array(); - if (!isset($cache[$year][$month])) { - $date = new DateTime(sprintf('%04d-%02d-01', $year, $month)); - $cache[$year][$month] = $date->format('t'); - } - return $cache[$year][$month]; - } - - /** * Returns the day of the week (0 = Sunday, 6 = Saturday) of this date. * * @return integer The day of the week. @@ -999,15 +951,15 @@ class Horde_Date if ($mask & self::MASK_DAY) { while ($this->_mday > 28 && - $this->_mday > Horde_Date::daysInMonth($this->_month, $this->_year)) { - $this->_mday -= Horde_Date::daysInMonth($this->_month, $this->_year); + $this->_mday > Horde_Date_Utils::daysInMonth($this->_month, $this->_year)) { + $this->_mday -= Horde_Date_Utils::daysInMonth($this->_month, $this->_year); ++$this->_month; $this->_correct(self::MASK_MONTH); } while ($this->_mday < 1) { --$this->_month; $this->_correct(self::MASK_MONTH); - $this->_mday += Horde_Date::daysInMonth($this->_month, $this->_year); + $this->_mday += Horde_Date_Utils::daysInMonth($this->_month, $this->_year); } } } diff --git a/framework/Date/lib/Horde/Date/Recurrence.php b/framework/Date/lib/Horde/Date/Recurrence.php index b899c1ac6..5917e95f6 100644 --- a/framework/Date/lib/Horde/Date/Recurrence.php +++ b/framework/Date/lib/Horde/Date/Recurrence.php @@ -367,8 +367,8 @@ class Horde_Date_Recurrence return false; } - $start_week = Horde_Date::firstDayOfWeek($this->start->format('W'), - $this->start->year); + $start_week = Horde_Date_Utils::firstDayOfWeek($this->start->format('W'), + $this->start->year); $start_week->hour = $this->start->hour; $start_week->min = $this->start->min; $start_week->sec = $this->start->sec; @@ -381,7 +381,7 @@ class Horde_Date_Recurrence } else { $theYear = $after->year; } - $after_week = Horde_Date::firstDayOfWeek($week, $theYear); + $after_week = Horde_Date_Utils::firstDayOfWeek($week, $theYear); $after_week_end = clone $after_week; $after_week_end->mday += 7; @@ -530,7 +530,7 @@ class Horde_Date_Recurrence // Seperate case here for February 29th if ($estart->month == 2 && $estart->mday == 29) { - while (!Horde_Date::isLeapYear($after->year)) { + while (!Horde_Date_Utils::isLeapYear($after->year)) { ++$after->year; } } @@ -823,13 +823,15 @@ class Horde_Date_Recurrence case 'W': $this->setRecurType(self::RECUR_WEEKLY); if (!empty($remainder)) { - $maskdays = array('SU' => Horde_Date::MASK_SUNDAY, - 'MO' => Horde_Date::MASK_MONDAY, - 'TU' => Horde_Date::MASK_TUESDAY, - 'WE' => Horde_Date::MASK_WEDNESDAY, - 'TH' => Horde_Date::MASK_THURSDAY, - 'FR' => Horde_Date::MASK_FRIDAY, - 'SA' => Horde_Date::MASK_SATURDAY); + $maskdays = array( + 'SU' => Horde_Date::MASK_SUNDAY, + 'MO' => Horde_Date::MASK_MONDAY, + 'TU' => Horde_Date::MASK_TUESDAY, + 'WE' => Horde_Date::MASK_WEDNESDAY, + 'TH' => Horde_Date::MASK_THURSDAY, + 'FR' => Horde_Date::MASK_FRIDAY, + 'SA' => Horde_Date::MASK_SATURDAY, + ); $mask = 0; while (preg_match('/^ ?[A-Z]{2} ?/', $remainder, $matches)) { $day = trim($matches[0]); @@ -839,13 +841,15 @@ class Horde_Date_Recurrence $this->setRecurOnDay($mask); } else { // Recur on the day of the week of the original recurrence. - $maskdays = array(Horde_Date::DATE_SUNDAY => Horde_Date::MASK_SUNDAY, - Horde_Date::DATE_MONDAY => Horde_Date::MASK_MONDAY, - Horde_Date::DATE_TUESDAY => Horde_Date::MASK_TUESDAY, - Horde_Date::DATE_WEDNESDAY => Horde_Date::MASK_WEDNESDAY, - Horde_Date::DATE_THURSDAY => Horde_Date::MASK_THURSDAY, - Horde_Date::DATE_FRIDAY => Horde_Date::MASK_FRIDAY, - Horde_Date::DATE_SATURDAY => Horde_Date::MASK_SATURDAY); + $maskdays = array( + Horde_Date::DATE_SUNDAY => Horde_Date::MASK_SUNDAY, + Horde_Date::DATE_MONDAY => Horde_Date::MASK_MONDAY, + Horde_Date::DATE_TUESDAY => Horde_Date::MASK_TUESDAY, + Horde_Date::DATE_WEDNESDAY => Horde_Date::MASK_WEDNESDAY, + Horde_Date::DATE_THURSDAY => Horde_Date::MASK_THURSDAY, + Horde_Date::DATE_FRIDAY => Horde_Date::MASK_FRIDAY, + Horde_Date::DATE_SATURDAY => Horde_Date::MASK_SATURDAY, + ); $this->setRecurOnDay($maskdays[$this->start->dayOfWeek()]); } break; @@ -987,13 +991,15 @@ class Horde_Date_Recurrence case 'WEEKLY': $this->setRecurType(self::RECUR_WEEKLY); if (isset($rdata['BYDAY'])) { - $maskdays = array('SU' => Horde_Date::MASK_SUNDAY, - 'MO' => Horde_Date::MASK_MONDAY, - 'TU' => Horde_Date::MASK_TUESDAY, - 'WE' => Horde_Date::MASK_WEDNESDAY, - 'TH' => Horde_Date::MASK_THURSDAY, - 'FR' => Horde_Date::MASK_FRIDAY, - 'SA' => Horde_Date::MASK_SATURDAY); + $maskdays = array( + 'SU' => Horde_Date::MASK_SUNDAY, + 'MO' => Horde_Date::MASK_MONDAY, + 'TU' => Horde_Date::MASK_TUESDAY, + 'WE' => Horde_Date::MASK_WEDNESDAY, + 'TH' => Horde_Date::MASK_THURSDAY, + 'FR' => Horde_Date::MASK_FRIDAY, + 'SA' => Horde_Date::MASK_SATURDAY, + ); $days = explode(',', $rdata['BYDAY']); $mask = 0; foreach ($days as $day) { diff --git a/framework/Date/lib/Horde/Date/Utils.php b/framework/Date/lib/Horde/Date/Utils.php new file mode 100644 index 000000000..2329b567c --- /dev/null +++ b/framework/Date/lib/Horde/Date/Utils.php @@ -0,0 +1,65 @@ +format('t'); + } + return $cache[$year][$month]; + } + +} diff --git a/framework/Date/test/Horde/Date/UtilsTest.php b/framework/Date/test/Horde/Date/UtilsTest.php index d3f31da60..7efeb9c8a 100644 --- a/framework/Date/test/Horde/Date/UtilsTest.php +++ b/framework/Date/test/Horde/Date/UtilsTest.php @@ -14,10 +14,10 @@ class Horde_Date_UtilsTest extends PHPUnit_Framework_TestCase { public function testFirstDayOfWeek() { - $this->assertEquals('2006-01-02', Horde_Date::firstDayOfWeek(1, 2006)->format('Y-m-d')); - $this->assertEquals('2007-01-01', Horde_Date::firstDayOfWeek(1, 2007)->format('Y-m-d')); - $this->assertEquals('2007-12-31', Horde_Date::firstDayOfWeek(1, 2008)->format('Y-m-d')); - $this->assertEquals('2010-01-04', Horde_Date::firstDayOfWeek(1, 2010)->format('Y-m-d')); + $this->assertEquals('2006-01-02', Horde_Date_Utils::firstDayOfWeek(1, 2006)->format('Y-m-d')); + $this->assertEquals('2007-01-01', Horde_Date_Utils::firstDayOfWeek(1, 2007)->format('Y-m-d')); + $this->assertEquals('2007-12-31', Horde_Date_Utils::firstDayOfWeek(1, 2008)->format('Y-m-d')); + $this->assertEquals('2010-01-04', Horde_Date_Utils::firstDayOfWeek(1, 2010)->format('Y-m-d')); } } -- 2.11.0