Skip to content

Commit

Permalink
Merge pull request #7 from defrag/precise-date-fix
Browse files Browse the repository at this point in the history
Fixing precise date calculations
  • Loading branch information
norberttech committed May 11, 2014
2 parents 08b9d1b + 4b32ce6 commit 43ba95d
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 17 deletions.
5 changes: 3 additions & 2 deletions spec/Coduo/PHPHumanizer/DateTimeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ function it_humanizes_precise_difference_between_dates()
array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minute, 45 seconds ago'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 hour, 40 minutes ago'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 day, 15 minutes from now'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '1 week, 2 hours from now'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 year, 1 week, 4 days, 4 hours from now'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 days, 2 hours from now'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 year, 2 days, 4 hours from now'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 days, 10 hours from now'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 day, 1 hour, 40 minutes ago'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 years, 1 day from now'),
);

foreach ($examples as $example) {
Expand Down
19 changes: 4 additions & 15 deletions src/Coduo/PHPHumanizer/DateTime/PreciseDifference.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,21 @@ private function calculate()
$units = array(
new Year(),
new Month(),
new Week(),
new Day(),
new Hour(),
new Minute(),
new Second(),
);

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

foreach ($units as $unit) {
if ($absoluteMilliSecondsDiff >= $unit->getMilliseconds()) {
$this->units[] = $unit;
}
}

foreach ($this->units as $unit) {
$quantity = (int) floor($absoluteMilliSecondsDiff / $unit->getMilliseconds());

if ($quantity === 0) {
continue;
if ($diff->{$unit->getDateIntervalSymbol()} > 0) {
$this->units[] = $unit;
$this->compoundResults[] = new CompoundResult($unit, $diff->{$unit->getDateIntervalSymbol()});
}

$this->compoundResults[] = new CompoundResult($unit, $quantity);
$absoluteMilliSecondsDiff -= ($quantity * $unit->getMilliseconds());
}

}

public function isPast()
Expand Down
7 changes: 7 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public function getName();
* @return int
*/
public function getMilliseconds();

/**
* Returns symbol of \DateInterval equivalent
*
* @return string
*/
public function getDateIntervalSymbol();
}
5 changes: 5 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function getMilliseconds()
$hour = new Hour();
return $hour->getMilliseconds() * 24;
}

public function getDateIntervalSymbol()
{
return 'd';
}
}
5 changes: 5 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit/Hour.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function getMilliseconds()
$minute = new Minute();
return $minute->getMilliseconds() * 60;
}

public function getDateIntervalSymbol()
{
return 'h';
}
}
5 changes: 5 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit/JustNow.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public function getMilliseconds()
{
return 0;
}

public function getDateIntervalSymbol()
{
throw new \RuntimeException("JustNow doesn't have date interval symbol equivalent");
}
}
5 changes: 5 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit/Minute.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function getMilliseconds()

return $second->getMilliseconds() * 60;
}

public function getDateIntervalSymbol()
{
return 'i';
}
}
5 changes: 5 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function getMilliseconds()
$day = new Day();
return $day->getMilliseconds() * 30;
}

public function getDateIntervalSymbol()
{
return 'm';
}
}
5 changes: 5 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit/Second.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public function getMilliseconds()
{
return 1000;
}

public function getDateIntervalSymbol()
{
return 's';
}
}
5 changes: 5 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit/Week.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function getMilliseconds()
$day = new Day();
return $day->getMilliseconds() * 7;
}

public function getDateIntervalSymbol()
{
throw new \RuntimeException("Week doesn't have date interval symbol equivalent");
}
}
5 changes: 5 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/Unit/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function getMilliseconds()
$day = new Day();
return $day->getMilliseconds() * 356;
}

public function getDateIntervalSymbol()
{
return 'y';
}
}

0 comments on commit 43ba95d

Please sign in to comment.