-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from defrag/precise-diff
Precise diffs
- Loading branch information
Showing
8 changed files
with
227 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/Coduo/PHPHumanizer/DateTime/Difference/CompoundResult.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPHumanizer\DateTime\Difference; | ||
|
||
use Coduo\PHPHumanizer\DateTime\Unit; | ||
|
||
class CompoundResult | ||
{ | ||
/** | ||
* @var \Coduo\PHPHumanizer\DateTime\Unit | ||
*/ | ||
private $unit; | ||
private $quantity; | ||
|
||
public function __construct(Unit $unit, $quantity) | ||
{ | ||
$this->unit = $unit; | ||
$this->quantity = $quantity; | ||
} | ||
|
||
/** | ||
* @param mixed $quantity | ||
*/ | ||
public function setQuantity($quantity) | ||
{ | ||
$this->quantity = $quantity; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getQuantity() | ||
{ | ||
return $this->quantity; | ||
} | ||
|
||
/** | ||
* @param \Coduo\PHPHumanizer\DateTime\Unit $unit | ||
*/ | ||
public function setUnit($unit) | ||
{ | ||
$this->unit = $unit; | ||
} | ||
|
||
/** | ||
* @return \Coduo\PHPHumanizer\DateTime\Unit | ||
*/ | ||
public function getUnit() | ||
{ | ||
return $this->unit; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPHumanizer\DateTime; | ||
|
||
use Coduo\PHPHumanizer\DateTime\Unit\Day; | ||
use Coduo\PHPHumanizer\DateTime\Unit\Hour; | ||
use Coduo\PHPHumanizer\DateTime\Unit\JustNow; | ||
use Coduo\PHPHumanizer\DateTime\Unit\Minute; | ||
use Coduo\PHPHumanizer\DateTime\Unit\Month; | ||
use Coduo\PHPHumanizer\DateTime\Unit\Second; | ||
use Coduo\PHPHumanizer\DateTime\Unit\Week; | ||
use Coduo\PHPHumanizer\DateTime\Unit\Year; | ||
use Coduo\PHPHumanizer\DateTime\Difference\CompoundResult; | ||
|
||
class PreciseDifference | ||
{ | ||
/** | ||
* @var \DateTime | ||
*/ | ||
private $fromDate; | ||
|
||
/** | ||
* @var \DateTime | ||
*/ | ||
private $toDate; | ||
|
||
/** | ||
* @var \Coduo\PHPHumanizer\DateTime\Unit[] | ||
*/ | ||
private $units; | ||
|
||
/** | ||
* @var \Coduo\PHPHumanizer\DateTime\Difference\CompoundResult[] | ||
*/ | ||
private $compoundResults; | ||
|
||
public function __construct(\DateTime $fromDate, \DateTime $toDate) | ||
{ | ||
$this->fromDate = $fromDate; | ||
$this->toDate = $toDate; | ||
$this->calculate(); | ||
} | ||
|
||
/** | ||
* @return \Coduo\PHPHumanizer\DateTime\Difference\CompoundResult[] | ||
*/ | ||
public function getCompoundResults() | ||
{ | ||
return $this->compoundResults; | ||
} | ||
|
||
private function calculate() | ||
{ | ||
/* @var $units \Coduo\PHPHumanizer\DateTime\Unit[] */ | ||
$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; | ||
|
||
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; | ||
} | ||
|
||
$this->compoundResults[] = new CompoundResult($unit, $quantity); | ||
$absoluteMilliSecondsDiff -= ($quantity * $unit->getMilliseconds()); | ||
} | ||
|
||
} | ||
|
||
public function isPast() | ||
{ | ||
$diff = $this->toDate->getTimestamp() - $this->fromDate->getTimestamp(); | ||
return ($diff > 0) ? false : true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPHumanizer\DateTime; | ||
|
||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
class PreciseFormatter | ||
{ | ||
/** | ||
* @var \Symfony\Component\Translation\TranslatorInterface | ||
*/ | ||
private $translator; | ||
|
||
/** | ||
* @param TranslatorInterface $translator | ||
*/ | ||
public function __construct(TranslatorInterface $translator) | ||
{ | ||
$this->translator = $translator; | ||
} | ||
|
||
/** | ||
* @param PreciseDifference $difference | ||
* @param string $locale | ||
* @return string | ||
*/ | ||
public function formatDifference(PreciseDifference $difference, $locale = 'en') | ||
{ | ||
$diff = array(); | ||
|
||
foreach ($difference->getCompoundResults() as $result) { | ||
|
||
$diff[] = $this->translator->transChoice( | ||
"compound." . $result->getUnit()->getName(), | ||
$result->getQuantity(), | ||
array('%count%' => $result->getQuantity()), | ||
'difference', | ||
$locale | ||
); | ||
|
||
} | ||
$suffix = $difference->isPast() ? 'compound.ago' : 'compound.from_now'; | ||
return join(", ", $diff) . ' '. $this->translator->trans($suffix, array(), 'difference', $locale); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters