Skip to content

Commit 7903400

Browse files
committed
Refactor relTimeToSeconds to separate function
No behavioral changes intended: the negative modifier bug is explicitly included in this refactoring.
1 parent f4cd8ab commit 7903400

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

webapp/src/Service/ExternalContestSourceService.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -690,41 +690,16 @@ protected function validateAndUpdateContest(Event $event, EventData $data): void
690690
->getRepository(Contest::class)
691691
->find($this->getSourceContestId());
692692

693-
// We need to convert the freeze to a value from the start instead of
694-
// the end so perform some regex magic.
695-
$duration = $data->duration;
696-
$freeze = $data->scoreboardFreezeDuration;
697-
$reltimeRegex = '/^(-)?(\d+):(\d{2}):(\d{2})(?:\.(\d{3}))?$/';
698-
preg_match($reltimeRegex, $duration, $durationData);
699-
700-
$durationNegative = ($durationData[1] === '-');
701-
$fullDuration = $durationNegative ? $duration : ('+' . $duration);
693+
// We need to convert the freeze to a value from the start instead of the end.
694+
$duration = $data->duration;
695+
$durationInSeconds = Utils::relTimeToSeconds($duration);
696+
$fullDuration = Utils::relTime($durationInSeconds, includePlus: true);
697+
$freeze = $data->scoreboardFreezeDuration;
702698

703699
if ($freeze !== null) {
704-
preg_match($reltimeRegex, $freeze, $freezeData);
705-
$freezeNegative = ($freezeData[1] === '-');
706-
$freezeHourModifier = $freezeNegative ? -1 : 1;
707-
$freezeInSeconds = $freezeHourModifier * (int)$freezeData[2] * 3600
708-
+ 60 * (int)$freezeData[3]
709-
+ (double)sprintf('%d.%03d', $freezeData[4], $freezeData[5] ?? 0);
710-
$durationHourModifier = $durationNegative ? -1 : 1;
711-
$durationInSeconds = $durationHourModifier * (int)$durationData[2] * 3600
712-
+ 60 * (int)$durationData[3]
713-
+ (double)sprintf('%d.%03d', $durationData[4], $durationData[5] ?? 0);
714-
$freezeStartSeconds = $durationInSeconds - $freezeInSeconds;
715-
$freezeHour = floor($freezeStartSeconds / 3600);
716-
$freezeMinutes = floor(($freezeStartSeconds % 3600) / 60);
717-
$freezeSeconds = floor(($freezeStartSeconds % 60) / 60);
718-
$freezeMilliseconds = $freezeStartSeconds - floor($freezeStartSeconds);
719-
720-
$fullFreeze = sprintf(
721-
'%s%d:%02d:%02d.%03d',
722-
$freezeHour < 0 ? '' : '+',
723-
$freezeHour,
724-
$freezeMinutes,
725-
$freezeSeconds,
726-
$freezeMilliseconds
727-
);
700+
$freezeInSeconds = Utils::relTimeToSeconds($freeze);
701+
$freezeStartSeconds = $durationInSeconds - $freezeInSeconds;
702+
$fullFreeze = Utils::relTime($freezeStartSeconds, includePlus: true);
728703
} else {
729704
$fullFreeze = null;
730705
}

webapp/src/Utils/Utils.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class Utils
167167

168168
final public const DAY_IN_SECONDS = 60*60*24;
169169

170+
final public const RELTIME_REGEX = '/^(-)?(\d+):(\d{2}):(\d{2})(?:\.(\d{3}))?$/';
171+
170172
/**
171173
* Returns the milliseconds part of a time stamp truncated at three digits.
172174
*/
@@ -191,12 +193,12 @@ public static function absTime(mixed $epoch, bool $floored = false): ?string
191193
}
192194

193195
/**
194-
* Prints a time diff as relative time as (-)?(h)*h:mm:ss(.uuu)?
195-
* (with millis if $floored is false).
196+
* Prints a time diff as relative time as ([-+])?(h)*h:mm:ss(.uuu)?
197+
* (with millis if $floored is false and with + sign only if $includePlus is true).
196198
*/
197-
public static function relTime(float $seconds, bool $floored = false): string
199+
public static function relTime(float $seconds, bool $floored = false, bool $includePlus = false): string
198200
{
199-
$sign = ($seconds < 0) ? '-' : '';
201+
$sign = ($seconds < 0) ? '-' : ($includePlus ? '+' : '');
200202
$seconds = abs($seconds);
201203
$hours = (int)($seconds / 3600);
202204
$minutes = (int)(($seconds - $hours*3600)/60);
@@ -206,6 +208,17 @@ public static function relTime(float $seconds, bool $floored = false): string
206208
. ($floored ? '' : $millis);
207209
}
208210

211+
public static function relTimeToSeconds(string $reltime): float
212+
{
213+
preg_match(self::RELTIME_REGEX, $reltime, $data);
214+
$negative = ($data[1] === '-');
215+
$modifier = $negative ? -1 : 1;
216+
$seconds = $modifier * (int)$data[2] * 3600
217+
+ (int)$data[3] * 60
218+
+ (float)sprintf('%d.%03d', $data[4], $data[5] ?? 0);
219+
return $seconds;
220+
}
221+
209222
/**
210223
* Parse a string as time and return as epoch in float format (with
211224
* optional fractional part). The original time string should be in one of

webapp/tests/Unit/Utils/UtilsTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public function testRelTime(): void
7272
self::assertEquals('1:18:31.000', Utils::relTime(4711));
7373
}
7474

75+
/**
76+
* Test that the relTime function returns the plus sign
77+
*/
78+
public function testRelTimeWithPlus(): void
79+
{
80+
self::assertEquals('+1:18:31.000', Utils::relTime(4711, includePlus: true));
81+
}
82+
7583
/**
7684
* Test that the relTime function returns the correct data when using a
7785
* time with millisecond precision
@@ -126,6 +134,30 @@ public function testNegativeRelTimeWithMillisFloored(): void
126134
self::assertEquals('-3:25:45', Utils::relTime(-12345.6789, true));
127135
}
128136

137+
/**
138+
* Test the relTimeToSeconds function with basic data.
139+
*/
140+
public function testRelTimeToSeconds(): void
141+
{
142+
self::assertEquals(1*3600 + 2*60 + 3, Utils::relTimeToSeconds('1:02:03'));
143+
}
144+
145+
/**
146+
* Test the relTimeToSeconds function with millisecond precision.
147+
*/
148+
public function testRelTimeToSecondsWithMillis(): void
149+
{
150+
self::assertEquals(10.123, Utils::relTimeToSeconds('0:00:10.123'));
151+
}
152+
153+
/**
154+
* Test the relTimeToSeconds function with negative reltime.
155+
*/
156+
public function testRelTimeToSecondsWithNegative(): void
157+
{
158+
self::assertEquals(-(1*3600 + 2*60 + 3), Utils::relTimeToSeconds('-1:02:03'));
159+
}
160+
129161
/**
130162
* Test that the toEpochFloat function works with a leap day
131163
*/

0 commit comments

Comments
 (0)