Skip to content

Commit dbb343b

Browse files
Unify reltime regexes and logic
1 parent fae9071 commit dbb343b

File tree

2 files changed

+5
-15
lines changed

2 files changed

+5
-15
lines changed

webapp/src/Entity/Contest.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,20 +1049,8 @@ public function getAbsoluteTime(?string $time_string): float|int|string|null
10491049
{
10501050
if ($time_string === null) {
10511051
return null;
1052-
} elseif (preg_match('/^[+-][0-9]+:[0-9]{2}(:[0-9]{2}(\.[0-9]{0,6})?)?$/', $time_string)) {
1053-
$sign = ($time_string[0] == '-' ? -1 : +1);
1054-
$time_string[0] = 0;
1055-
$times = explode(':', $time_string, 3);
1056-
$hours = (int)$times[0];
1057-
$minutes = (int)$times[1];
1058-
if (count($times) == 2) {
1059-
$seconds = 0;
1060-
} else {
1061-
$seconds = (float)$times[2];
1062-
}
1063-
$seconds = $seconds + 60 * ($minutes + 60 * $hours);
1064-
$seconds *= $sign;
1065-
$absoluteTime = $this->starttime + $seconds;
1052+
} elseif (Utils::isRelTime($time_string)) {
1053+
$absoluteTime = $this->starttime + Utils::relTimeToSeconds($time_string);
10661054

10671055
// Take into account the removed intervals.
10681056
/** @var RemovedInterval[] $removedIntervals */

webapp/src/Utils/Utils.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ 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}))?)?$/';
170+
// Regex to parse relative times. Note that these are our own relative times, which allows
171+
// more than the CLICS spec does
172+
final public const RELTIME_REGEX = '/^([+-])?(\d+):(\d{2})(?::(\d{2})(?:\.(\d+))?)?$/';
171173

172174
/**
173175
* Returns the milliseconds part of a time stamp truncated at three digits.

0 commit comments

Comments
 (0)