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

svn at limb-project.com svn at limb-project.com
Mon May 7 17:44:24 MSD 2007


Author: pachanga
Date: 2007-05-07 17:44:24 +0400 (Mon, 07 May 2007)
New Revision: 5824
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5824

Modified:
   3.x/trunk/limb/datetime/src/lmbDate.class.php
   3.x/trunk/limb/datetime/tests/cases/lmbDateTest.class.php
Log:
-- static lmbDate :: isValidDateString($str) added 

Modified: 3.x/trunk/limb/datetime/src/lmbDate.class.php
===================================================================
--- 3.x/trunk/limb/datetime/src/lmbDate.class.php	2007-05-07 12:17:02 UTC (rev 5823)
+++ 3.x/trunk/limb/datetime/src/lmbDate.class.php	2007-05-07 13:44:24 UTC (rev 5824)
@@ -14,7 +14,7 @@
 class lmbDate extends lmbObject
 {
   //YYYY-MM-DD HH:MM:SS timezone
-  const DATE_STRING_REGEX = '~^(([0-9]{4})-([0-9]{2})-([0-9]{2}))?((?(1)\s+)([0-9]{2}):([0-9]{2}):?([0-9]{2})?)?$~';
+  const DATE_ISO_REGEX = '~^(([0-9]{4})-([0-9]{2})-([0-9]{2}))?((?(1)\s+)([0-9]{2}):([0-9]{2}):?([0-9]{2})?)?$~';
 
   protected $year = 0;
   protected $month = 0;
@@ -102,9 +102,22 @@
                      $this->year ? $this->year : 1);
   }
 
+  static function isValidDateString($value)
+  {
+    try
+    {
+      new lmbDate((string)$value);
+      return true;
+    }
+    catch(lmbException $e)
+    {
+      return false;
+    }
+  }
+
   protected function _setByString($string)
   {
-    if(!preg_match(self :: DATE_STRING_REGEX, trim($string), $regs))
+    if(!preg_match(self :: DATE_ISO_REGEX, trim($string), $regs))
       throw new lmbException("Could not setup date using string '$string'");
 
     if(isset($regs[1]))

Modified: 3.x/trunk/limb/datetime/tests/cases/lmbDateTest.class.php
===================================================================
--- 3.x/trunk/limb/datetime/tests/cases/lmbDateTest.class.php	2007-05-07 12:17:02 UTC (rev 5823)
+++ 3.x/trunk/limb/datetime/tests/cases/lmbDateTest.class.php	2007-05-07 13:44:24 UTC (rev 5824)
@@ -44,6 +44,22 @@
     catch(lmbException $e){}
   }
 
+  function testIsValidDateString()
+  {
+    $this->assertTrue(lmbDate :: isValidDateString('2005-12-01 12:45:12'));
+    $this->assertTrue(lmbDate :: isValidDateString('2005-12-01 12:45'));
+    $this->assertTrue(lmbDate :: isValidDateString('2005-12-01'));
+    $this->assertTrue(lmbDate :: isValidDateString('12:45:12'));
+    $this->assertTrue(lmbDate :: isValidDateString('12:45'));
+    $this->assertTrue(lmbDate :: isValidDateString(' 12:45:12 '));
+  }
+
+  function testIsValidDateStringFalse()
+  {
+    $this->assertFalse(lmbDate :: isValidDateString('baba-duba'));
+    $this->assertFalse(lmbDate :: isValidDateString('2005-12-01 12.'));
+  }
+
   function testCreate()
   {
     $date = new lmbDate(12, 45, 12, 1, 12 ,2005);



More information about the limb-svn mailing list