mirror of
https://github.com/Shadowss/TravianZ.git
synced 2026-08-12 06:11:00 +00:00
Fix negative timer formatting (#359)
This commit is contained in:
@@ -124,24 +124,12 @@ class MyGenerator
|
|||||||
*/
|
*/
|
||||||
public function getTimeFormat($time)
|
public function getTimeFormat($time)
|
||||||
{
|
{
|
||||||
$time = (int) $time;
|
$time = max(0, (int) $time);
|
||||||
$min = 0;
|
$hr = intdiv($time, 3600);
|
||||||
$hr = 0;
|
$min = intdiv($time % 3600, 60);
|
||||||
|
$seconds = $time % 60;
|
||||||
|
|
||||||
while ($time >= 60) {
|
return sprintf('%d:%02d:%02d', $hr, $min, $seconds);
|
||||||
$time -= 60;
|
|
||||||
$min++;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ($min >= 60) {
|
|
||||||
$min -= 60;
|
|
||||||
$hr++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($min < 10) $min = "0" . $min;
|
|
||||||
if ($time < 10) $time = "0" . $time;
|
|
||||||
|
|
||||||
return $hr . ":" . $min . ":" . $time;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../../GameEngine/Generator.php';
|
||||||
|
|
||||||
|
$cases = [
|
||||||
|
-61 => '0:00:00',
|
||||||
|
-1 => '0:00:00',
|
||||||
|
0 => '0:00:00',
|
||||||
|
1 => '0:00:01',
|
||||||
|
59 => '0:00:59',
|
||||||
|
60 => '0:01:00',
|
||||||
|
3599 => '0:59:59',
|
||||||
|
3600 => '1:00:00',
|
||||||
|
86399 => '23:59:59',
|
||||||
|
86400 => '24:00:00',
|
||||||
|
90000 => '25:00:00',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($cases as $seconds => $expected) {
|
||||||
|
$actual = $generator->getTimeFormat($seconds);
|
||||||
|
|
||||||
|
if ($actual !== $expected) {
|
||||||
|
fwrite(
|
||||||
|
STDERR,
|
||||||
|
"getTimeFormat($seconds): expected $expected, got $actual.\n"
|
||||||
|
);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fwrite(STDOUT, "getTimeFormat regression test passed.\n");
|
||||||
Reference in New Issue
Block a user