Commit cb3eb9c6 by David Renty

Added support for inverted intervals (future time) and unit tests

parent b5cd0a6f
...@@ -468,7 +468,7 @@ class Formatter extends Component ...@@ -468,7 +468,7 @@ class Formatter extends Component
* *
* @return string the formatted result * @return string the formatted result
*/ */
public function asElapsedTime($value) public function asRelativeTime($value, $referenceTime=null)
{ {
if ($value === null) { if ($value === null) {
return $this->nullDisplay; return $this->nullDisplay;
...@@ -491,40 +491,59 @@ class Formatter extends Component ...@@ -491,40 +491,59 @@ class Formatter extends Component
} else { } else {
$timezone = new \DateTimeZone($this->timeZone); $timezone = new \DateTimeZone($this->timeZone);
if ($referenceTime === null) {
$dateNow = new DateTime('now', $timezone); $dateNow = new DateTime('now', $timezone);
} else {
$referenceTime = $this->normalizeDatetimeValue($referenceTime);
$dateNow = new DateTime(null, $timezone);
$dateNow->setTimestamp($referenceTime);
}
$dateThen = new DateTime(null, $timezone); $dateThen = new DateTime(null, $timezone);
$dateThen->setTimestamp($timestamp); $dateThen->setTimestamp($timestamp);
$interval = $dateNow->diff($dateThen); $interval = $dateThen->diff($dateNow);
var_dump($dateThen);
var_dump($dateNow);
} }
} }
if ($interval->invert) {
if ($interval->y >= 1) { if ($interval->y >= 1) {
$delta = $interval->y; return Yii::t('yii', 'in {delta, plural, =1{a year} other{# years}}', ['delta' => $interval->y]);
return Yii::t('yii', '{delta, plural, =1{a year} other{# years}} ago', ['delta' => $delta]);
} }
if ($interval->m >= 1) { if ($interval->m >= 1) {
$delta = $interval->m; return Yii::t('yii', 'in {delta, plural, =1{a month} other{# months}}', ['delta' => $interval->m]);
return Yii::t('yii', '{delta, plural, =1{a month} other{# months}} ago', ['delta' => $delta]); }
if ($interval->d >= 1) {
return Yii::t('yii', 'in {delta, plural, =1{a day} other{# days}}', ['delta' => $interval->d]);
}
if ($interval->h >= 1) {
return Yii::t('yii', 'in {delta, plural, =1{an hour} other{# hours}}', ['delta' => $interval->h]);
} }
if ($interval->d >= 7) { if ($interval->i >= 1) {
$delta = floor($interval->d / 7); return Yii::t('yii', 'in {delta, plural, =1{a minute} other{# minutes}}', ['delta' => $interval->i]);
return Yii::t('yii', '{delta, plural, =1{a week} other{# weeks}} ago', ['delta' => $delta]); }
return Yii::t('yii', 'in {delta, plural, =1{a second} other{# seconds}}', ['delta' => $interval->s]);
} else {
if ($interval->y >= 1) {
return Yii::t('yii', '{delta, plural, =1{a year} other{# years}} ago', ['delta' => $interval->y]);
}
if ($interval->m >= 1) {
return Yii::t('yii', '{delta, plural, =1{a month} other{# months}} ago', ['delta' => $interval->m]);
} }
if ($interval->d >= 1) { if ($interval->d >= 1) {
$delta = $interval->d; return Yii::t('yii', '{delta, plural, =1{a day} other{# days}} ago', ['delta' => $interval->d]);
return Yii::t('yii', '{delta, plural, =1{yesterday} other{# days ago}}', ['delta' => $delta]);
} }
if ($interval->h >= 1) { if ($interval->h >= 1) {
$delta = $interval->h; return Yii::t('yii', '{delta, plural, =1{an hour} other{# hours}} ago', ['delta' => $interval->h]);
return Yii::t('yii', '{delta, plural, =1{an hour} other{# hours}} ago', ['delta' => $delta]);
} }
if ($interval->i >= 1) { if ($interval->i >= 1) {
$delta = $interval->i; return Yii::t('yii', '{delta, plural, =1{a minute} other{# minutes}} ago', ['delta' => $interval->i]);
return Yii::t('yii', '{delta, plural, =1{a minute} other{# minutes}} ago', ['delta' => $delta]);
} }
$delta = $interval->s; return Yii::t('yii', '{delta, plural, =1{a second} other{# seconds}} ago', ['delta' => $interval->s]);
return Yii::t('yii', '{delta, plural, =1{a second} other{# seconds}} ago', ['delta' => $delta]); }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment