From dc3c40463d6e98bc5dcf311fed70e0b722f4db49 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 17 Feb 2023 19:01:22 +0800 Subject: [PATCH] feat(submissions): user-friendly time format --- web/app/controllers/submissions_list.php | 2 + web/app/models/UOJSubmission.php | 2 +- web/app/models/UOJSubmissionLikeTrait.php | 23 +++++++-- web/app/models/UOJTime.php | 61 +++++++++++++++-------- 4 files changed, 63 insertions(+), 25 deletions(-) diff --git a/web/app/controllers/submissions_list.php b/web/app/controllers/submissions_list.php index 0423eaa..620cd5f 100644 --- a/web/app/controllers/submissions_list.php +++ b/web/app/controllers/submissions_list.php @@ -5,6 +5,8 @@ Auth::check() || redirectToLogin(); $conds = []; $config = [ + 'time_format' => 'friendly', + 'time_font_size' => 'normal', 'judge_time_hidden' => true, 'table_config' => [ 'div_classes' => ['card', 'mb-3', 'table-responsive'], diff --git a/web/app/models/UOJSubmission.php b/web/app/models/UOJSubmission.php index 2013f61..429fa1c 100644 --- a/web/app/models/UOJSubmission.php +++ b/web/app/models/UOJSubmission.php @@ -627,7 +627,7 @@ class UOJSubmission { default: echo '
'; echo '', $name, ''; - echo '', $this->echoStatusBarTD($id, $cfg), ''; + echo '', $this->echoStatusBarTD($id, $cfg + ['time_font_size' => 'normal']), ''; echo '
'; break; diff --git a/web/app/models/UOJSubmissionLikeTrait.php b/web/app/models/UOJSubmissionLikeTrait.php index 0a998a0..e238dc1 100644 --- a/web/app/models/UOJSubmissionLikeTrait.php +++ b/web/app/models/UOJSubmissionLikeTrait.php @@ -252,6 +252,11 @@ trait UOJSubmissionLikeTrait { protected function echoStatusBarTDBase($name, array $cfg) { + $cfg += [ + 'time_format' => 'normal', + 'time_font_size' => 'small', + ]; + switch ($name) { case 'id': echo $this->getLink(); @@ -277,21 +282,31 @@ trait UOJSubmissionLikeTrait { break; case 'used_memory': if ($cfg['show_actual_score']) { - echo $this->info['used_memory'] . 'kb'; + echo $this->info['used_memory'] . 'KB'; } else { echo '/'; } break; case 'tot_size': if ($this->info['tot_size'] < 1024) { - echo $this->info['tot_size'] . 'b'; + echo $this->info['tot_size'] . 'B'; } else { - echo sprintf("%.1f", $this->info['tot_size'] / 1024) . 'kb'; + echo sprintf("%.1f", $this->info['tot_size'] / 1024) . 'KB'; } break; case 'submit_time': case 'judge_time': - echo '', $this->info[$name], ''; + if ($cfg['time_font_size'] == 'small') { + echo ''; + } + if ($cfg['time_format'] == 'friendly') { + echo UOJTime::userFriendlyFormat($this->info[$name]); + } else { + echo $this->info[$name]; + } + if ($cfg['time_font_size'] == 'small') { + echo ''; + } break; default: echo '?'; diff --git a/web/app/models/UOJTime.php b/web/app/models/UOJTime.php index da98d75..dfe71b5 100644 --- a/web/app/models/UOJTime.php +++ b/web/app/models/UOJTime.php @@ -3,31 +3,52 @@ class UOJTime { public static ?DateTime $time_now = null; public static string $time_now_str = ''; - + const FORMAT = 'Y-m-d H:i:s'; + const FORMAT_CURRENT_YEAR = 'm-d H:i:s'; + const FORMAT_NOT_CURRENT_YEAR = 'Y-m-d H:i'; const MAX_TIME = '9999-12-31 23:59:59'; - + public static function init() { self::$time_now = new DateTime(); self::$time_now_str = self::$time_now->format(static::FORMAT); } - - public static function str2time($input) { - if (!is_string($input)) { - return null; - } - try { - $time = new DateTime($input); - if ($time->format(static::FORMAT) !== $input) { - return null; - } - return $time; - } catch (Exception $e) { - return null; - } - } - public static function time2str(DateTime $time) { - return $time->format(static::FORMAT); - } + public static function str2time($input) { + if (!is_string($input)) { + return null; + } + try { + $time = new DateTime($input); + if ($time->format(static::FORMAT) !== $input) { + return null; + } + return $time; + } catch (Exception $e) { + return null; + } + } + + public static function time2str(DateTime $time) { + return $time->format(static::FORMAT); + } + + /** + * @var DateTime|string $time + */ + public static function userFriendlyFormat($time) { + try { + if (!($time instanceof DateTime)) { + $time = new DateTime($time); + } + + if ($time->format('Y') === self::$time_now->format('Y')) { + return $time->format(static::FORMAT_CURRENT_YEAR); + } else { + return $time->format(static::FORMAT_NOT_CURRENT_YEAR); + } + } catch (Exception $e) { + return ''; + } + } }