move static methods to a Utils class
authorChuck Hagenbuch <chuck@horde.org>
Tue, 2 Jun 2009 14:48:18 +0000 (10:48 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Tue, 2 Jun 2009 14:48:18 +0000 (10:48 -0400)
framework/Date/lib/Horde/Date.php
framework/Date/lib/Horde/Date/Recurrence.php
framework/Date/lib/Horde/Date/Utils.php [new file with mode: 0644]
framework/Date/test/Horde/Date/UtilsTest.php

index 6612c6a..bb011e4 100644 (file)
@@ -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);
             }
         }
     }
index b899c1a..5917e95 100644 (file)
@@ -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 (file)
index 0000000..2329b56
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+/**
+ * Horde Date wrapper/logic class, including some calculation
+ * functions.
+ *
+ * @category Horde
+ * @package  Horde_Date
+ */
+
+
+/**
+ * @category Horde
+ * @package  Horde_Date
+ */
+class Horde_Date_Utils
+{
+    /**
+     * Returns whether a year is a leap year.
+     *
+     * @param integer $year  The year.
+     *
+     * @return boolean  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];
+    }
+
+}
index d3f31da..7efeb9c 100644 (file)
@@ -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'));
     }
 
 }