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

svn at limb-project.com svn at limb-project.com
Tue May 8 00:52:08 MSD 2007


Author: pachanga
Date: 2007-05-08 00:52:07 +0400 (Tue, 08 May 2007)
New Revision: 5828
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5828

Added:
   3.x/trunk/limb/datetime/src/lmbMonthCalendar.class.php
   3.x/trunk/limb/datetime/tests/cases/lmbMonthCalendarTest.class.php
Modified:
   3.x/trunk/limb/datetime/src/lmbDate.class.php
Log:
-- initial implementation of lmbMonthCalendar added

Modified: 3.x/trunk/limb/datetime/src/lmbDate.class.php
===================================================================
--- 3.x/trunk/limb/datetime/src/lmbDate.class.php	2007-05-07 18:43:11 UTC (rev 5827)
+++ 3.x/trunk/limb/datetime/src/lmbDate.class.php	2007-05-07 20:52:07 UTC (rev 5828)
@@ -244,7 +244,7 @@
 
   function isBefore($when, $use_time_zone=false)
   {
-    if ($this->compare($when, $use_time_zone) == -1)
+    if($this->compare($when, $use_time_zone) == -1)
       return true;
     else
       return false;
@@ -252,7 +252,7 @@
 
   function isAfter($when, $use_time_zone=false)
   {
-    if ($this->compare($when, $use_time_zone) == 1)
+    if($this->compare($when, $use_time_zone) == 1)
       return true;
     else
       return false;
@@ -260,7 +260,7 @@
 
   function isEqual($when, $use_time_zone=false)
   {
-    if ($this->compare($when, $use_time_zone) == 0)
+    if($this->compare($when, $use_time_zone) == 0)
       return true;
     else
       return false;
@@ -277,7 +277,7 @@
 
     $julian = ($days[$this->month - 1] + $this->day);
 
-    if ($this->month > 2 &&  $this->isLeapYear())
+    if($this->month > 2 &&  $this->isLeapYear())
       $julian++;
 
     return $julian;
@@ -323,7 +323,7 @@
     $y_1_isleap = $d->isLeapYear();
 
     $day_of_year_number = $day + $mnth[$month - 1];
-    if ($y_isleap && $month > 2)
+    if($y_isleap && $month > 2)
       $day_of_year_number++;
 
     // find Jan 1 weekday (monday = 1, sunday = 7)
@@ -335,10 +335,10 @@
     $h = $day_of_year_number + ($jan1_weekday - 1);
     $weekday = 1 + intval(($h - 1) % 7);
     // find if Y M D falls in YearNumber Y-1, WeekNumber 52 or
-    if ($day_of_year_number <= (8 - $jan1_weekday) && $jan1_weekday > 4)
+    if($day_of_year_number <= (8 - $jan1_weekday) && $jan1_weekday > 4)
     {
       $yearnumber = $year - 1;
-      if ($jan1_weekday == 5 || ($jan1_weekday == 6 && $y_1_isleap))
+      if($jan1_weekday == 5 || ($jan1_weekday == 6 && $y_1_isleap))
         $weeknumber = 53;
       else
         $weeknumber = 52;
@@ -346,25 +346,25 @@
     else
       $yearnumber = $year;
     // find if Y M D falls in YearNumber Y+1, WeekNumber 1
-    if ($yearnumber == $year)
+    if($yearnumber == $year)
     {
-      if ($y_isleap)
+      if($y_isleap)
         $i = 366;
       else
         $i = 365;
 
-      if (($i - $day_of_year_number) < (4 - $weekday))
+      if(($i - $day_of_year_number) < (4 - $weekday))
       {
         $yearnumber++;
         $weeknumber = 1;
       }
     }
     // find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
-    if ($yearnumber == $year)
+    if($yearnumber == $year)
     {
       $j = $day_of_year_number + (7 - $weekday) + ($jan1_weekday - 1);
       $weeknumber = intval($j / 7);
-      if ($jan1_weekday > 4)
+      if($jan1_weekday > 4)
         $weeknumber--;
     }
    return $weeknumber;
@@ -372,17 +372,17 @@
 
   function dateToDays()
   {
-    $century = (int) substr("{$this->year}", 0, 2);
-    $year = (int) substr("{$this->year}", 2, 2);
+    $century = (int)substr("{$this->year}", 0, 2);
+    $year = (int)substr("{$this->year}", 2, 2);
     $month = $this->month;
     $day = $this->day;
 
-    if ($month > 2)
+    if($month > 2)
       $month -= 3;
     else
     {
       $month += 9;
-      if ($year)
+      if($year)
         $year--;
       else
       {
@@ -391,9 +391,9 @@
       }
     }
     return (
-        floor(( 146097 * $century) / 4 ) +
-        floor(( 1461 * $year) / 4 ) +
-        floor(( 153 * $month + 2) / 5 ) +
+        floor((146097 * $century) / 4) +
+        floor((1461 * $year) / 4) +
+        floor((153 * $month + 2) / 5) +
         $day + 1721119);
   }
 

Added: 3.x/trunk/limb/datetime/src/lmbMonthCalendar.class.php
===================================================================
--- 3.x/trunk/limb/datetime/src/lmbMonthCalendar.class.php	                        (rev 0)
+++ 3.x/trunk/limb/datetime/src/lmbMonthCalendar.class.php	2007-05-07 20:52:07 UTC (rev 5828)
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id: lmbDate.class.php 5824 2007-05-07 13:44:24Z pachanga $
+ * @package    datetime
+ */
+lmb_require('limb/datetime/src/lmbDate.class.php');
+
+class lmbMonthCalendar
+{
+  protected $start_date;
+  protected $end_date;
+  
+  function __construct($year, $month)
+  {
+    $tmp_date = new lmbDate(0, 0, 0, 0, $month, $year);
+    $this->start_date = $tmp_date->setDay(1);
+    $this->end_date = $this->start_date->addMonth(1)->addDay(-1);
+  }
+  
+  function getStartDate()
+  {
+    return $this->start_date;
+  }
+
+  function getEndDate()
+  {
+    return $this->end_date;
+  }
+  
+  function getNumberOfDays()
+  {
+    return $this->end_date->getDay();
+  }
+  
+  function getNumberOfWeeks()
+  {    
+    $dof_start = $this->start_date->getDayOfWeek();    
+    $dof_end = $this->end_date->getDayOfWeek() + 1;//fix it???
+    
+    $days = ($this->getNumberOfDays() - (7 - $dof_start) - $dof_end);        
+    return ((int)$days / 7) + 1 + (($dof_end > 0) ? 1 : 0);
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/datetime/tests/cases/lmbMonthCalendarTest.class.php
===================================================================
--- 3.x/trunk/limb/datetime/tests/cases/lmbMonthCalendarTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/datetime/tests/cases/lmbMonthCalendarTest.class.php	2007-05-07 20:52:07 UTC (rev 5828)
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id: lmbDateTest.class.php 5824 2007-05-07 13:44:24Z pachanga $
+ * @package    datetime
+ */
+lmb_require('limb/datetime/src/lmbMonthCalendar.class.php');
+
+class lmbMonthCalendarTest extends UnitTestCase
+{
+  function testGetBoundaries()
+  {
+    $c = new lmbMonthCalendar(2007, 5);
+    $this->assertEqual(new lmbDate('2007-05-01'), $c->getStartDate());
+    $this->assertEqual(new lmbDate('2007-05-31'), $c->getEndDate());
+  }
+  
+  function testGetNumberOfDays()
+  {    
+    $c = new lmbMonthCalendar(2007, 5);
+    $this->assertEqual($c->getNumberOfDays(), 31);    
+  }
+  
+  function testGetNumberOfDaysForLeapYear()
+  {
+    $c1 = new lmbMonthCalendar(2005, 2);
+    $this->assertEqual($c1->getNumberOfDays(), 28);
+    
+    $c2 = new lmbMonthCalendar(2004, 2);
+    $this->assertEqual($c2->getNumberOfDays(), 29);    
+  }
+  
+  function testGetNumberOfWeeks()
+  {
+    $c1 = new lmbMonthCalendar(1999, 2);
+    $this->assertEqual($c1->getNumberOfWeeks(), 4);
+    
+    $c2 = new lmbMonthCalendar(2007, 2);
+    $this->assertEqual($c2->getNumberOfWeeks(), 5);
+    
+    $c3 = new lmbMonthCalendar(2010, 5);
+    $this->assertEqual($c3->getNumberOfWeeks(), 6);            
+  }  
+}
+?>
\ No newline at end of file



More information about the limb-svn mailing list