Skip to content

Commit 43ba95d

Browse files
committed
Merge pull request #7 from defrag/precise-date-fix
Fixing precise date calculations
2 parents 08b9d1b + 4b32ce6 commit 43ba95d

File tree

11 files changed

+54
-17
lines changed

11 files changed

+54
-17
lines changed

spec/Coduo/PHPHumanizer/DateTimeSpec.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ function it_humanizes_precise_difference_between_dates()
7070
array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minute, 45 seconds ago'),
7171
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 hour, 40 minutes ago'),
7272
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 day, 15 minutes from now'),
73-
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '1 week, 2 hours from now'),
74-
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 year, 1 week, 4 days, 4 hours from now'),
73+
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 days, 2 hours from now'),
74+
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 year, 2 days, 4 hours from now'),
7575
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 days, 10 hours from now'),
7676
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 day, 1 hour, 40 minutes ago'),
77+
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 years, 1 day from now'),
7778
);
7879

7980
foreach ($examples as $example) {

src/Coduo/PHPHumanizer/DateTime/PreciseDifference.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,21 @@ private function calculate()
5555
$units = array(
5656
new Year(),
5757
new Month(),
58-
new Week(),
5958
new Day(),
6059
new Hour(),
6160
new Minute(),
6261
new Second(),
6362
);
6463

65-
$absoluteMilliSecondsDiff = abs($this->toDate->getTimestamp() - $this->fromDate->getTimestamp()) * 1000;
64+
$diff = $this->fromDate->diff($this->toDate);
6665

6766
foreach ($units as $unit) {
68-
if ($absoluteMilliSecondsDiff >= $unit->getMilliseconds()) {
69-
$this->units[] = $unit;
70-
}
71-
}
72-
73-
foreach ($this->units as $unit) {
74-
$quantity = (int) floor($absoluteMilliSecondsDiff / $unit->getMilliseconds());
7567

76-
if ($quantity === 0) {
77-
continue;
68+
if ($diff->{$unit->getDateIntervalSymbol()} > 0) {
69+
$this->units[] = $unit;
70+
$this->compoundResults[] = new CompoundResult($unit, $diff->{$unit->getDateIntervalSymbol()});
7871
}
79-
80-
$this->compoundResults[] = new CompoundResult($unit, $quantity);
81-
$absoluteMilliSecondsDiff -= ($quantity * $unit->getMilliseconds());
8272
}
83-
8473
}
8574

8675
public function isPast()

src/Coduo/PHPHumanizer/DateTime/Unit.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ public function getName();
1515
* @return int
1616
*/
1717
public function getMilliseconds();
18+
19+
/**
20+
* Returns symbol of \DateInterval equivalent
21+
*
22+
* @return string
23+
*/
24+
public function getDateIntervalSymbol();
1825
}

src/Coduo/PHPHumanizer/DateTime/Unit/Day.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function getMilliseconds()
1919
$hour = new Hour();
2020
return $hour->getMilliseconds() * 24;
2121
}
22+
23+
public function getDateIntervalSymbol()
24+
{
25+
return 'd';
26+
}
2227
}

src/Coduo/PHPHumanizer/DateTime/Unit/Hour.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function getMilliseconds()
1919
$minute = new Minute();
2020
return $minute->getMilliseconds() * 60;
2121
}
22+
23+
public function getDateIntervalSymbol()
24+
{
25+
return 'h';
26+
}
2227
}

src/Coduo/PHPHumanizer/DateTime/Unit/JustNow.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public function getMilliseconds()
1818
{
1919
return 0;
2020
}
21+
22+
public function getDateIntervalSymbol()
23+
{
24+
throw new \RuntimeException("JustNow doesn't have date interval symbol equivalent");
25+
}
2126
}

src/Coduo/PHPHumanizer/DateTime/Unit/Minute.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public function getMilliseconds()
2020

2121
return $second->getMilliseconds() * 60;
2222
}
23+
24+
public function getDateIntervalSymbol()
25+
{
26+
return 'i';
27+
}
2328
}

src/Coduo/PHPHumanizer/DateTime/Unit/Month.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function getMilliseconds()
1919
$day = new Day();
2020
return $day->getMilliseconds() * 30;
2121
}
22+
23+
public function getDateIntervalSymbol()
24+
{
25+
return 'm';
26+
}
2227
}

src/Coduo/PHPHumanizer/DateTime/Unit/Second.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public function getMilliseconds()
1818
{
1919
return 1000;
2020
}
21+
22+
public function getDateIntervalSymbol()
23+
{
24+
return 's';
25+
}
2126
}

src/Coduo/PHPHumanizer/DateTime/Unit/Week.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function getMilliseconds()
1919
$day = new Day();
2020
return $day->getMilliseconds() * 7;
2121
}
22+
23+
public function getDateIntervalSymbol()
24+
{
25+
throw new \RuntimeException("Week doesn't have date interval symbol equivalent");
26+
}
2227
}

0 commit comments

Comments
 (0)