function assertDateTimeEqual($date_time1, $date_time2, $delta = 1, $msg = null) { $ts1 = (new SimpleDateTime($date_time1))->gmtTimestamp(); $ts2 = (new SimpleDateTime($date_time2))->gmtTimestamp(); if (!$delta instanceof SimpleTimeInterval) { $delta = new SimpleTimeInterval($delta); } $diff = new SimpleTimeInterval(abs($ts1 - $ts2)); $test = $diff->seconds() <= $delta->seconds(); if (is_null($msg) && !$test) { $diff_str = self::build_time_interval_string($diff); $delta_str = self::build_time_interval_string($delta); $msg = "Timestamps should be equal, but differ by {$diff_str} >= " . "{$delta_str} seconds."; } $this->assertTrue($test, $msg); }
$ti4 = new SimpleTimeInterval(0); $results = array($ti1->toNatural(), $ti2->toNatural(), $ti3->toNatural(), $ti4->toNatural('long', 'h')); return $results == $expected; }; $unitTests['Greatest Exact Unit'] = function () { $expected = array('s', 'm', 'h', 'd', 'w', 'M', 'y', 'days', 'years', 'years'); $tiS = new SimpleTimeInterval(array('s' => 5)); $tiMin = new SimpleTimeInterval(array('m' => 5)); $tiH = new SimpleTimeInterval(array('h' => 5)); $tiD = new SimpleTimeInterval(array('d' => 5)); $tiW = new SimpleTimeInterval(array('w' => 5)); $tiM = new SimpleTimeInterval(array('M' => 5)); $tiY = new SimpleTimeInterval(array('y' => 5)); $tiCase1 = new SimpleTimeInterval(259200); $tiCase2 = new SimpleTimeInterval(array('y' => 1)); $tiCase3 = new SimpleTimeInterval(array('y' => 5)); $results = array($tiS->greatestExactUnit('short'), $tiMin->greatestExactUnit('short'), $tiH->greatestExactUnit('short'), $tiD->greatestExactUnit('short'), $tiW->greatestExactUnit('short'), $tiM->greatestExactUnit('short'), $tiY->greatestExactUnit('short'), $tiCase1->greatestExactUnit('long'), $tiCase2->greatestExactUnit('long'), $tiCase3->greatestExactUnit('long')); return $results == $expected; }; $unitTests['All Units in All Formats Present'] = function () { $expected = array('short' => array('s', 'm', 'h', 'd', 'w', 'M', 'y'), 'abbrev' => array('secs.', 'mins.', 'hours', 'days', 'weeks', 'months', 'years'), 'long' => array('seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years')); foreach (array('short', 'abbrev', 'long') as $format) { if (SimpleTimeInterval::units($format) != $expected[$format]) { return false; } } return true; }; $unitTests['Singularize Units'] = function () { $expected = array('short' => array('s', 'm', 'h', 'd', 'w', 'M', 'y'), 'abbrev' => array('sec.', 'min.', 'hour', 'day', 'week', 'month', 'year'), 'long' => array('second', 'minute', 'hour', 'day', 'week', 'month', 'year')); foreach (array('short', 'abbrev', 'long') as $format) {