Skip to content

Allow relative times in contest.yaml #3026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/jobs/fix_pipelinecomponents_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ set -eux

echo "Set plugin config for version detection"
phpcs --config-set installed_paths /app/vendor/phpcompatibility/php-compatibility

# Current released version does not know enums
echo "Upgrade the compatibility for PHP versions"
mydir=$(pwd)
sed -i 's/"phpcompatibility\/php-compatibility": "9.3.5"/"phpcompatibility\/php-compatibility": "dev-develop"/g' /app/composer.json
sed -i 's/"squizlabs\/php_codesniffer": "3.11.3"/"squizlabs\/php_codesniffer": "^3.13.0"/g' /app/composer.json
cd /app; composer update
cd $mydir
16 changes: 2 additions & 14 deletions webapp/src/Entity/Contest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,20 +1049,8 @@ public function getAbsoluteTime(?string $time_string): float|int|string|null
{
if ($time_string === null) {
return null;
} elseif (preg_match('/^[+-][0-9]+:[0-9]{2}(:[0-9]{2}(\.[0-9]{0,6})?)?$/', $time_string)) {
$sign = ($time_string[0] == '-' ? -1 : +1);
$time_string[0] = 0;
$times = explode(':', $time_string, 3);
$hours = (int)$times[0];
$minutes = (int)$times[1];
if (count($times) == 2) {
$seconds = 0;
} else {
$seconds = (float)$times[2];
}
$seconds = $seconds + 60 * ($minutes + 60 * $hours);
$seconds *= $sign;
$absoluteTime = $this->starttime + $seconds;
} elseif (Utils::isRelTime($time_string)) {
$absoluteTime = $this->starttime + Utils::relTimeToSeconds($time_string);

// Take into account the removed intervals.
/** @var RemovedInterval[] $removedIntervals */
Expand Down
1 change: 0 additions & 1 deletion webapp/src/Entity/SubmissionSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ enum SubmissionSource: string
case SHADOWING = 'shadowing';
case TEAM_PAGE = 'team page';
case UNKNOWN = 'unknown';

}
80 changes: 63 additions & 17 deletions webapp/src/Service/ImportExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use App\Utils\Scoreboard\Filter;
use App\Utils\Utils;
use Collator;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
Expand Down Expand Up @@ -83,10 +85,14 @@ public function getContestYamlData(Contest $contest, bool $includeProblems = tru
'formal_name' => $contest->getName(),
'name' => $contest->getShortname(),
'start_time' => Utils::absTime($contest->getStarttime(), true),
'end_time' => Utils::absTime($contest->getEndtime(), true),
'end_time' => Utils::isRelTime($contest->getEndtimeString())
? $contest->getEndtimeString()
: Utils::absTime($contest->getEndtime(), true),
'duration' => Utils::relTime($contest->getContestTime((float)$contest->getEndtime())),
'penalty_time' => $this->config->get('penalty_time'),
'activate_time' => Utils::absTime($contest->getActivatetime(), true),
'activate_time' => Utils::isRelTime($contest->getActivatetimeString())
? $contest->getActivatetimeString()
: Utils::absTime($contest->getActivatetime(), true),
];
if ($warnMsg = $contest->getWarningMessage()) {
$data['warning_message'] = $warnMsg;
Expand All @@ -100,21 +106,27 @@ public function getContestYamlData(Contest $contest, bool $includeProblems = tru
}

if ($contest->getFreezetime() !== null) {
$data['scoreboard_freeze_time'] = Utils::absTime($contest->getFreezetime(), true);
$data['scoreboard_freeze_time'] = Utils::isRelTime($contest->getFreezetimeString())
? $contest->getFreezetimeString()
: Utils::absTime($contest->getFreezetime(), true);
$data['scoreboard_freeze_duration'] = Utils::relTime(
$contest->getContestTime((float)$contest->getEndtime()) - $contest->getContestTime((float)$contest->getFreezetime()),
true,
);
if ($contest->getUnfreezetime() !== null) {
$data['scoreboard_thaw_time'] = Utils::absTime($contest->getUnfreezetime(), true);
$data['scoreboard_thaw_time'] = Utils::isRelTime($contest->getUnfreezetimeString())
? $contest->getUnfreezetimeString()
: Utils::absTime($contest->getUnfreezetime(), true);
}
}
if ($contest->getFinalizetime() !== null) {
$data['finalize_time'] = Utils::absTime($contest->getFinalizetime(), true);
}

if ($contest->getDeactivatetime() !== null) {
$data['deactivate_time'] = Utils::absTime($contest->getDeactivatetime(), true);
$data['deactivate_time'] = Utils::isRelTime($contest->getDeactivatetimeString())
? $contest->getDeactivatetimeString()
: Utils::absTime($contest->getDeactivatetime(), true);
}

if ($includeProblems) {
Expand Down Expand Up @@ -146,9 +158,15 @@ public function getContestYamlData(Contest $contest, bool $includeProblems = tru
*
* @param array<string> $fields
* @param array<string, string|DateTime|DateTimeImmutable> $data
* @return array{time: DateTimeImmutable|null, isRelative: bool|null, relativeTime: string|null}
*/
protected function convertImportedTime(array $fields, array $data, ?string &$errorMessage = null): ?DateTimeImmutable
{
protected function convertImportedTime(
array $fields,
array $data,
bool $allowRelative = true,
?DateTimeInterface $startTime = null,
?string &$errorMessage = null
): array {
$timeValue = null;
$usedField = null;
foreach ($fields as $field) {
Expand All @@ -160,22 +178,42 @@ protected function convertImportedTime(array $fields, array $data, ?string &$err
}
}

$isRelative = false;

if (is_string($timeValue)) {
$time = date_create_from_format(DateTime::ISO8601, $timeValue) ?:
// Make sure ISO 8601 but with the T replaced with a space also works.
date_create_from_format('Y-m-d H:i:sO', $timeValue);
if ($allowRelative && ($isRelative = Utils::isRelTime($timeValue)) && $startTime) {
$time = new DateTimeImmutable($startTime->format('Y-m-d H:i:s'), $startTime->getTimezone());
$seconds = Utils::relTimeToSeconds($timeValue);
if ($seconds < 0) {
$time = $time->sub(new DateInterval(sprintf('PT%sS', abs($seconds))));
} else {
$time = $time->add(new DateInterval(sprintf('PT%sS', $seconds)));
}
} else {
$time = date_create_from_format(DateTime::ISO8601, $timeValue) ?:
// Make sure ISO 8601 but with the T replaced with a space also works.
date_create_from_format('Y-m-d H:i:sO', $timeValue);
}
} else {
/** @var DateTime|DateTimeImmutable $time */
$time = $timeValue;
}
// If/When parsing fails we get a false instead of a null
if ($time === false) {
$errorMessage = 'Can not parse '.$usedField;
return null;
return [
'time' => null,
'isRelative' => null,
'relativeTime' => null,
];
} elseif ($time) {
$time = $time->setTimezone(new DateTimeZone(date_default_timezone_get()));
}
return $time instanceof DateTime ? DateTimeImmutable::createFromMutable($time) : $time;
return [
'time' => $time instanceof DateTime ? DateTimeImmutable::createFromMutable($time) : $time,
'isRelative' => $isRelative,
'relativeTime' => $isRelative ? $timeValue : null,
];
}

public function importContestData(mixed $data, ?string &$errorMessage = null, ?string &$cid = null): bool
Expand Down Expand Up @@ -215,13 +253,17 @@ public function importContestData(mixed $data, ?string &$errorMessage = null, ?s

$invalid_regex = str_replace(['/^[', '+$/'], ['/[^', '/'], DOMJudgeService::EXTERNAL_IDENTIFIER_REGEX);

$startTime = $this->convertImportedTime($startTimeFields, $data, $errorMessage);
['time' => $startTime] = $this->convertImportedTime($startTimeFields, $data, false, errorMessage: $errorMessage);
if ($errorMessage) {
return false;
}

// Activate time is special, it can return non empty message for parsing error or null if no field was provided
$activateTime = $this->convertImportedTime($activateTimeFields, $data, $errorMessage);
[
'time' => $activateTime,
'isRelative' => $activateTimeIsRelative,
'relativeTime' => $activateRelativeTime,
] = $this->convertImportedTime($activateTimeFields, $data, startTime: $startTime, errorMessage: $errorMessage);
if ($errorMessage) {
return false;
} elseif (!$activateTime) {
Expand All @@ -231,7 +273,11 @@ public function importContestData(mixed $data, ?string &$errorMessage = null, ?s
}
}

$deactivateTime = $this->convertImportedTime($deactivateTimeFields, $data, $errorMessage);
[
'time' => $deactivateTime,
'isRelative' => $deactivateTimeIsRelative,
'relativeTime' => $deactivateRelativeTime,
] = $this->convertImportedTime($deactivateTimeFields, $data, startTime: $startTime, errorMessage: $errorMessage);
if ($errorMessage) {
return false;
}
Expand All @@ -247,11 +293,11 @@ public function importContestData(mixed $data, ?string &$errorMessage = null, ?s
->setExternalid($contest->getShortname())
->setWarningMessage($data['warning_message'] ?? $data['warning-message'] ?? null)
->setStarttimeString(date_format($startTime, 'Y-m-d H:i:s e'))
->setActivatetimeString(date_format($activateTime, 'Y-m-d H:i:s e'))
->setActivatetimeString($activateTimeIsRelative ? $activateRelativeTime : date_format($activateTime, 'Y-m-d H:i:s e'))
->setEndtimeString(sprintf('+%s', $data['duration']))
->setPublic($data['public'] ?? true);
if ($deactivateTime) {
$contest->setDeactivatetimeString(date_format($deactivateTime, 'Y-m-d H:i:s e'));
$contest->setDeactivatetimeString($deactivateTimeIsRelative ? $deactivateRelativeTime : date_format($deactivateTime, 'Y-m-d H:i:s e'));
}

// Get all visible categories. For now, we assume these are the ones getting awards
Expand Down
11 changes: 9 additions & 2 deletions webapp/src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ class Utils

final public const DAY_IN_SECONDS = 60*60*24;

final public const RELTIME_REGEX = '/^(-)?(\d+):(\d{2}):(\d{2})(?:\.(\d{3}))?$/';
// Regex to parse relative times. Note that these are our own relative times, which allows
// more than the CLICS spec does
final public const RELTIME_REGEX = '/^([+-])?(\d+):(\d{2})(?::(\d{2})(?:\.(\d+))?)?$/';

/**
* Returns the milliseconds part of a time stamp truncated at three digits.
Expand All @@ -192,6 +194,11 @@ public static function absTime(mixed $epoch, bool $floored = false): ?string
. date("P", (int) $epoch);
}

public static function isRelTime(string $time): bool
{
return preg_match(self::RELTIME_REGEX, $time) === 1;
}

/**
* Prints a time diff as relative time as ([-+])?(h)*h:mm:ss(.uuu)?
* (with millis if $floored is false and with + sign only if $includePlus is true).
Expand All @@ -216,7 +223,7 @@ public static function relTimeToSeconds(string $reltime): float
$seconds = $modifier * (
(int)$data[2] * 3600
+ (int)$data[3] * 60
+ (float)sprintf('%d.%03d', $data[4], $data[5] ?? 0));
+ (float)sprintf('%d.%03d', $data[4] ?? 0, $data[5] ?? 0));
return $seconds;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public function testAddYaml(): void
name: practice
activate_time: '2021-03-27T09:00:00+00:00'
start_time: '2021-03-27T09:00:00+00:00'
end_time: '2021-03-27T11:00:00+00:00'
end_time: '+2:00:00'
duration: 2:00:00.000
penalty_time: 20
medals:
gold: 4
silver: 4
bronze: 4
scoreboard_freeze_time: '2021-03-27T10:30:00+00:00'
scoreboard_freeze_time: '+01:30:00'
scoreboard_freeze_duration: 0:30:00
EOF;

Expand Down
32 changes: 10 additions & 22 deletions webapp/tests/Unit/Controller/Jury/ImportExportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public function provideContestYamlContents(): Generator
formal_name: 'Demo contest'
name: demo
start_time: '{$year}-01-01T08:00:00+00:00'
end_time: '{$year}-01-01T13:00:00+00:00'
end_time: '+5:00:00'
duration: '5:00:00.000'
penalty_time: 20
activate_time: '{$pastYear}-01-01T08:00:00+00:00'
medals:
gold: 4
silver: 4
bronze: 4
scoreboard_freeze_time: '{$year}-01-01T12:00:00+00:00'
scoreboard_freeze_time: '+04:00:00'
scoreboard_freeze_duration: '1:00:00'
problems:
-
Expand Down Expand Up @@ -218,25 +218,13 @@ public function testResultsTsvExport(

public function provideResultsTsvExport(): Generator
{
yield [0, true, true, 'results 1
exteam Honorable 0 0 0
'];
yield [0, true, false, 'results 1
exteam Honorable 0 0 0
'];
yield [0, false, true, 'results 1
exteam Honorable 0 0 0
'];
yield [0, false, true, 'results 1
exteam Honorable 0 0 0
'];
yield [1, true, true, 'results 1
'];
yield [1, true, false, 'results 1
'];
yield [1, false, true, 'results 1
'];
yield [1, false, true, 'results 1
'];
yield [0, true, true, "results 1\nexteam Honorable 0 0 0 \n"];
yield [0, true, false, "results 1\nexteam Honorable 0 0 0 \n"];
yield [0, false, true, "results 1\nexteam Honorable 0 0 0 \n"];
yield [0, false, true, "results 1\nexteam Honorable 0 0 0 \n"];
yield [1, true, true, "results 1\n"];
yield [1, true, false, "results 1\n"];
yield [1, false, true, "results 1\n"];
yield [1, false, true, "results 1\n"];
}
}
Loading
Loading