[limb-svn] r5939 - in 3.x/trunk/limb/datetime: src tests/cases

svn at limb-project.com svn at limb-project.com
Tue Jun 5 18:50:20 MSD 2007


Author: pachanga
Date: 2007-06-05 18:50:20 +0400 (Tue, 05 Jun 2007)
New Revision: 5939
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5939

Modified:
   3.x/trunk/limb/datetime/src/lmbDate.class.php
   3.x/trunk/limb/datetime/src/lmbMonth.class.php
   3.x/trunk/limb/datetime/tests/cases/lmbDateTest.class.php
Log:
-- possible BC breakage: lmbDate :: getDayOfWeek() takes into account current value of static $first_day_week variable
-- lmbDate :: getPhpDayOfWeek() added, it returns immutable day of week with Sunday at 0 index
-- lmbDate :: getIntlDayOfWeek() added, it returns immutable day of week with Monday at 0 index

Modified: 3.x/trunk/limb/datetime/src/lmbDate.class.php
===================================================================
--- 3.x/trunk/limb/datetime/src/lmbDate.class.php	2007-06-05 11:38:45 UTC (rev 5938)
+++ 3.x/trunk/limb/datetime/src/lmbDate.class.php	2007-06-05 14:50:20 UTC (rev 5939)
@@ -1,13 +1,13 @@
 <?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- *
- * @copyright  Copyright &copy; 2004-2007 BIT
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- * @version    $Id$
- * @package    $package$
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    $package$
  */
 lmb_require('limb/core/src/lmbObject.class.php');
 
@@ -16,6 +16,11 @@
   //YYYY-MM-DD HH:MM:SS timezone
   const DATE_ISO_REGEX = '~^(([0-9]{4})-([0-9]{2})-([0-9]{2}))?((?(1)\s+)([0-9]{2}):([0-9]{2}):?([0-9]{2})?)?$~';
 
+  /**
+   * Defines what day starts the week.
+   * Monday (1) is the international standard, Sunday (0) is used in US.
+   * @see setFirstDayOfWeek()
+   */
   static protected $first_day_week = 1;
 
   protected $year = 0;
@@ -345,6 +350,16 @@
 
   function getDayOfWeek()
   {
+    return $this->_correctDayOfWeek($this->getPhpDayOfWeek(), self :: $first_day_week);
+  }
+
+  function getIntlDayOfWeek()
+  {
+    return $this->_correctDayOfWeek($this->getPhpDayOfWeek(), 1);
+  }
+
+  function getPhpDayOfWeek()
+  {
     $year = $this->year;
     $month = $this->month;
     $day = $this->day;
@@ -352,11 +367,6 @@
     if(1901 < $year && $year < 2038)
       return (int)date('w', mktime(0, 0, 0, $month, $day, $year));
 
-    //gregorian correction
-    $correction = 0;
-    if(($year < 1582) || (($year == 1582) || (($month < 10) || (($month == 10) || ($day < 15)))))
-      $correction = 3;
-
     if($month > 2)
     {
       $month -= 2;
@@ -367,11 +377,25 @@
       $year--;
     }
 
-    $day  = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4);
-    $day += floor(($year / 100) / 4) - 2 * floor($year / 100) + 77 + $correction;
-    return (int)($day - 7 * floor($day / 7));
+    $day = (floor((13 * $month - 1) / 5) +
+            $day + ($year % 100) +
+            floor(($year % 100) / 4) +
+            floor(($year / 100) / 4) - 2 *
+            floor($year / 100) + 77);
+
+    return $day - 7 * floor($day / 7);
   }
 
+  protected function _correctDayOfWeek($dow, $first_day_week)
+  {
+    if($first_day_week == 0)
+      return $dow;
+
+    if($dow == 0)
+      return 6;
+    return $dow - 1;
+  }
+
   function getBeginOfDay()
   {
     return new lmbDate($this->year, $this->month, $this->day, $this->tz);
@@ -384,14 +408,14 @@
 
   function getBeginOfWeek()
   {
-    $this_weekday = $this->getDayOfWeek();
+    $this_weekday = $this->getPhpDayOfWeek();
     $interval = (7 - self :: $first_day_week + $this_weekday) % 7;
     return lmbDate :: createByDays($this->getDateDays() - $interval);
   }
 
   function getEndOfWeek()
   {
-    $this_weekday = $this->getDayOfWeek();
+    $this_weekday = $this->getPhpDayOfWeek();
     $interval = (6 + self :: $first_day_week - $this_weekday) % 7;
     return lmbDate :: createByDays($this->getDateDays() + $interval);
   }
@@ -430,19 +454,19 @@
       return $res;
     }
 
-    $dayofweek = $this->getDayOfWeek();
-    $firstday  = self :: create($year, 1, 1)->getDayOfWeek();
+    $dayofweek = $this->getPhpDayOfWeek();
+    $firstday  = self :: create($year, 1, 1)->getPhpDayOfWeek();
     if(($month == 1) && (($firstday < 1) || ($firstday > 4)) && ($day < 4))
     {
-      $firstday  = self :: create($year - 1, 1, 1)->getDayOfWeek();
+      $firstday  = self :: create($year - 1, 1, 1)->getPhpDayOfWeek();
       $month     = 12;
       $day       = 31;
     }
-    elseif(($month == 12) && ((self :: create($year + 1, 1, 1)->getDayOfWeek() < 5) &&
-            (self :: create($year + 1, 1, 1)->getDayOfWeek() > 0)))
+    elseif(($month == 12) && ((self :: create($year + 1, 1, 1)->getPhpDayOfWeek() < 5) &&
+            (self :: create($year + 1, 1, 1)->getPhpDayOfWeek() > 0)))
         return 1;
 
-    return intval(((self :: create($year, 1, 1)->getDayOfWeek() < 5) && (self :: create($year, 1, 1)->getDayOfWeek() > 0)) +
+    return intval(((self :: create($year, 1, 1)->getPhpDayOfWeek() < 5) && (self :: create($year, 1, 1)->getPhpDayOfWeek() > 0)) +
            4 * ($month - 1) + (2 * ($month - 1) + ($day - 1) + $firstday - $dayofweek + 6) * 36 / 256);
   }
 

Modified: 3.x/trunk/limb/datetime/src/lmbMonth.class.php
===================================================================
--- 3.x/trunk/limb/datetime/src/lmbMonth.class.php	2007-06-05 11:38:45 UTC (rev 5938)
+++ 3.x/trunk/limb/datetime/src/lmbMonth.class.php	2007-06-05 14:50:20 UTC (rev 5939)
@@ -1,13 +1,13 @@
 <?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- *
- * @copyright  Copyright &copy; 2004-2007 BIT
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- * @version    $Id$
- * @package    $package$
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    $package$
  */
 lmb_require('limb/datetime/src/lmbDate.class.php');
 
@@ -66,20 +66,21 @@
 
   function getNumberOfWeeks()
   {
-    $dof = $this->start_date->getDayOfWeek();
-    if(lmbDate :: getFirstDayOfWeek() == 1 && $dof == 0)
+    $dow = $this->start_date->getPhpDayOfWeek();
+
+    if(lmbDate :: getFirstDayOfWeek() == 1 && $dow == 0)
     {
-      $first_week_days = 7 - $dof + lmbDate :: getFirstDayOfWeek();
+      $first_week_days = 7 - $dow + lmbDate :: getFirstDayOfWeek();
       $weeks = 1;
     }
-    elseif(lmbDate :: getFirstDayOfWeek() == 0 && $dof == 6)
+    elseif(lmbDate :: getFirstDayOfWeek() == 0 && $dow == 6)
     {
-      $first_week_days = 7 - $dof + lmbDate :: getFirstDayOfWeek();
+      $first_week_days = 7 - $dow + lmbDate :: getFirstDayOfWeek();
       $weeks = 1;
     }
     else
     {
-      $first_week_days = lmbDate :: getFirstDayOfWeek() - $dof;
+      $first_week_days = lmbDate :: getFirstDayOfWeek() - $dow;
       $weeks = 0;
     }
 

Modified: 3.x/trunk/limb/datetime/tests/cases/lmbDateTest.class.php
===================================================================
--- 3.x/trunk/limb/datetime/tests/cases/lmbDateTest.class.php	2007-06-05 11:38:45 UTC (rev 5938)
+++ 3.x/trunk/limb/datetime/tests/cases/lmbDateTest.class.php	2007-06-05 14:50:20 UTC (rev 5939)
@@ -1,13 +1,13 @@
 <?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- *
- * @copyright  Copyright &copy; 2004-2007 BIT
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- * @version    $Id$
- * @package    $package$
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    $package$
  */
 lmb_require('limb/datetime/src/lmbDate.class.php');
 lmb_require('limb/datetime/src/lmbDateTimeZone.class.php');
@@ -225,36 +225,54 @@
     $this->assertEqual($date->getStamp(), $stamp);
   }
 
-  function testGetDayOfWeekForSunday()
+  function testGetPhpDayOfWeekForSunday()
   {
     $date = new lmbDate('2005-01-16');
-    $this->assertEqual($date->getDayOfWeek(), 0);
+    $this->assertEqual($date->getPhpDayOfWeek(), 0);
   }
 
-  function testGetDayOfWeekForMonday()
+  function testGetIntlDayOfWeekForSunday()
   {
+    $date = new lmbDate('2005-01-16');
+    $this->assertEqual($date->getIntlDayOfWeek(), 6);
+  }
+
+  function testGetPhpDayOfWeekForMonday()
+  {
     $date = new lmbDate('2005-01-17');
-    $this->assertEqual($date->getDayOfWeek(), 1);
+    $this->assertEqual($date->getPhpDayOfWeek(), 1);
   }
 
-  function testGetDayOfWeekForSuturday()
+  function testGetIntlDayOfWeekForMonday()
   {
+    $date = new lmbDate('2005-01-17');
+    $this->assertEqual($date->getIntlDayOfWeek(), 0);
+  }
+
+  function testGetPhpDayOfWeekForSuturday()
+  {
     $date = new lmbDate('2005-01-15');
-    $this->assertEqual($date->getDayOfWeek(), 6);
+    $this->assertEqual($date->getPhpDayOfWeek(), 6);
   }
 
+  function testGetIntlDayOfWeekForSuturday()
+  {
+    $date = new lmbDate('2005-01-15');
+    $this->assertEqual($date->getIntlDayOfWeek(), 5);
+  }
+
   //in the two tests below we're testing a boundary situtation
   //for day of the week which happens in February
-  function testGetDayOfWeekMonthBeforeFebruary()
+  function testGetPhpDayOfWeekMonthBeforeFebruary()
   {
     $date = new lmbDate('2005-01-20');
-    $this->assertEqual($date->getDayOfWeek(), 4);
+    $this->assertEqual($date->getPhpDayOfWeek(), 4);
   }
 
-  function testGetDayOfWeekMonthAfterFebruary()
+  function testGetPhpDayOfWeekMonthAfterFebruary()
   {
     $date = new lmbDate('2005-08-20');
-    $this->assertEqual($date->getDayOfWeek(), 6);
+    $this->assertEqual($date->getPhpDayOfWeek(), 6);
   }
 
   function testGetBeginOfDay()
@@ -385,10 +403,6 @@
     $this->assertEqual($new_date->toString(), '2005-01-08 00:00:00');//???
   }
 
-  function TODO_testSetDayOfWeek()
-  {
-  }
-
   function testSetTimeZone()
   {
     $date = new lmbDate('2005-01-01', 'Europe/Moscow');



More information about the limb-svn mailing list