Skip to content

Commit

Permalink
the beginnings of the fake DateInterval class work as expected, going…
Browse files Browse the repository at this point in the history
… to refactor next;
  • Loading branch information
caseysoftware committed Aug 28, 2011
1 parent 372151b commit 3f54fb8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
77 changes: 77 additions & 0 deletions includes/backcompat_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,81 @@ function date_diff2(DateTime $date1, DateTime $date2, $units = 'D') {

return round($difference / $factor, 0);
}

/**
* DateInterval
* Alternative to DateInterval if we're using PHP pre-5.3.0
* http://www.php.net/manual/en/class.dateinterval.php
*
* @todo Deprecate this as soon as possible.. web2project v4.0?
*/
class DateInterval2 {
public $y = 0;
public $m = 0;
public $d = 0;
public $h = 0;
public $i = 0;
public $s = 0;
public $invert = 0;
public $days = 0;

public function __construct($interval_spec) {
if ('P' != $interval_spec[0] ) {
// is this an exception?
}
$interval_spec = strtolower($interval_spec);
$units = array_filter(preg_split("/[\d+.]/", $interval_spec));
$units = array_merge(array(), $units);
$values = preg_split("/[\D+.]/", $interval_spec);

$pastMonth = false;
foreach ($units as $k => $unit) {
switch($unit) {
case 'p':
//do nothing
break;
case 'y':
$this->y = $values[$k];
break;
case 'm':
(!$pastMonth) ? $this->m = $values[$k] : $this->i = $values[$k];
$pastMonth = true;
break;
case 'w':
$this->d = 7 * $values[$k];
$pastMonth = true;
break;
case 'd':
$this->d = $values[$k];
$pastMonth = true;
break;
case 'h':
$this->h = $values[$k];
$pastMonth = true;
break;
case 's':
$this->s = $values[$k];
$pastMonth = true;
break;
default:
break;
}
}
/*
$interval = new DateInterval2('P2Y4D6H8M');
$interval = new DateInterval2('P3WT6H8M');
$interval = new DateInterval2('P1Y1D');
$interval = new DateInterval2('P1Y1M37D500M');
*/
}
public function _asArray() {

}
public function createFromDateString() {

}
public function format() {

}
}
}
28 changes: 25 additions & 3 deletions unit_tests/includes/backcompat_functions.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
* @subpackage unit_tests
*/
class BackCompat_Functions_Test extends PHPUnit_Framework_TestCase {



public function test_date_diff() {
$today = new DateTime('now');
$plus7_days = new DateTime('+7 days');
Expand All @@ -45,5 +44,28 @@ public function test_date_diff() {
$this->assertEquals(20 , date_diff2($year2010, $year2030, 'Y'));
$this->assertEquals(-20, date_diff2($year2030, $year2010, 'Y'));
}


public function test_DateInterval_constructor() {

$interval = new DateInterval2('P2Y4D6H8M');
$this->assertEquals(
array('y' => 2, 'm' => 0, 'd' => 4, 'h' => 6, 'i' => 8,
's' => 0, 'invert' => 0, 'days' => 0), get_object_vars($interval));

$interval = new DateInterval2('P3W6H8M');
$this->assertEquals(
array('y' => 0, 'm' => 0, 'd' => 21, 'h' => 6, 'i' => 8,
's' => 0, 'invert' => 0, 'days' => 0), get_object_vars($interval));

$interval = new DateInterval2('P1Y1D');
$this->assertEquals(
array('y' => 1, 'm' => 0, 'd' => 1, 'h' => 0, 'i' => 0,
's' => 0, 'invert' => 0, 'days' => 0), get_object_vars($interval));

$interval = new DateInterval2('P1Y1M37D500M');
$this->assertEquals(
array('y' => 1, 'm' => 1, 'd' => 37, 'h' => 0, 'i' => 500,
's' => 0, 'invert' => 0, 'days' => 0), get_object_vars($interval));
//TODO: test days & invert
}
}

0 comments on commit 3f54fb8

Please sign in to comment.