Skip to content

Commit

Permalink
Фикс неверного подсчёта секунд (теперь не нужна лишняя секунда) #92
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed Sep 30, 2020
1 parent fcb9ba1 commit 424bb4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/TimeSpeller.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@ public static function spellInterval(DateInterval $interval, $options = 0, $limi
*/
public static function spellDifference($dateTime, $options = 0, $limit = 0)
{
$now = new DateTime();
if (is_numeric($dateTime) || is_string($dateTime)) {
$dateTime = new DateTime(is_numeric($dateTime)
$now = new DateTime('@'.time());

if ($dateTime instanceof DateTime) {
$interval = $dateTime->diff($now);
} else if (is_numeric($dateTime) || is_string($dateTime)) {
$date_time = new DateTime(is_numeric($dateTime)
? '@' . $dateTime
: $dateTime);
} else if(!($dateTime instanceof DateTime)) {
$interval = $date_time->diff($now);
} else {
throw new InvalidArgumentException('dateTime argument should be unix timestamp (int) or date time (string) or DateTime instance');
}

return static::spellInterval($dateTime->diff($now), $options, $limit);
return static::spellInterval($interval, $options, $limit);
}
}
7 changes: 4 additions & 3 deletions tests/Russian/TimeSpellerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function intervalsProvider()
['P1Y5M10D', TimeSpeller::SEPARATE, '1 год, 5 месяцев и 10 дней'],
['P10Y1MT5H', TimeSpeller::DIRECTION, '10 лет 1 месяц 5 часов назад'],
['P3DT1H2M', TimeSpeller::SEPARATE | TimeSpeller::DIRECTION, '3 дня, 1 час и 2 минуты назад'],
['PT1M1S', TimeSpeller::SEPARATE | TimeSpeller::DIRECTION, '1 минуту и 1 секунду назад'],
['P10MT40M30S', TimeSpeller::SEPARATE, '10 месяцев, 40 минут и 30 секунд'],
];
}
Expand Down Expand Up @@ -71,7 +72,7 @@ public function timeUnitsProvider()
*/
public function testSpellDifference($dateTime, $limit)
{
$diff = (new DateTime())->diff(new DateTime(is_numeric($dateTime) ? '@'.$dateTime : $dateTime));
$diff = (new DateTime('@'.time()))->diff(new DateTime(is_numeric($dateTime) ? '@'.$dateTime : $dateTime));
$this->assertEquals(TimeSpeller::spellInterval($diff, 0, $limit),
TimeSpeller::spellDifference($dateTime, 0, $limit));
}
Expand All @@ -81,8 +82,8 @@ public function differencesProvides()
return
[
['+30 minutes 2 seconds', 1],
[time() + 3601, 1],
[time() + 86401, 1],
[time() + 3600, 1],
[time() + 86400, 1],
];
}

Expand Down

0 comments on commit 424bb4d

Please sign in to comment.