Compare commits

...

2 Commits

Author SHA1 Message Date
dc3c40463d
feat(submissions): user-friendly time format
All checks were successful
continuous-integration/drone/push Build is passing
2023-02-17 19:01:22 +08:00
d91f72491f
fix(blogs): list order 2023-02-17 18:45:48 +08:00
6 changed files with 66 additions and 46 deletions

View File

@ -3,7 +3,7 @@ requirePHPLib('form');
Auth::check() || redirectToLogin();
function echoBlogItem($info) {
function echoBlog($info) {
$blog = new UOJBlog($info);
$poster = UOJUser::query($blog->info['poster']);
@ -60,7 +60,7 @@ $pag = new Paginator([
'table_name' => 'blogs',
'col_names' => ['id', 'poster', 'title', 'post_time', 'active_time', 'zan', 'is_hidden'],
'cond' => '1',
'tail' => 'order by post_time desc',
'tail' => 'order by post_time desc, id desc',
'post_filter' => function ($info) {
return (new UOJBlog($info))->userCanView(Auth::user());
},
@ -104,7 +104,7 @@ $pag = new Paginator([
<?php endif ?>
<?php foreach ($pag->get() as $idx => $row) : ?>
<?php echoBlogItem($row) ?>
<?php echoBlog($row) ?>
<?php endforeach ?>
</div>
</div>

View File

@ -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'],

View File

@ -983,24 +983,6 @@ function echoHacksList($cond, $tail, $config, $user) {
);
}
function echoBlog($blog, $config = array()) {
global $REQUIRE_LIB;
$default_config = array(
'blog' => $blog,
'show_title_only' => false,
'is_preview' => false
);
foreach ($default_config as $key => $val) {
if (!isset($config[$key])) {
$config[$key] = $val;
}
}
$config['REQUIRE_LIB'] = $REQUIRE_LIB;
uojIncludeView('blog-preview', $config);
}
function echoBlogTag($tag) {
echo ' <a class="uoj-blog-tag">', '<span class="badge bg-secondary">', HTML::escape($tag), '</span></a> ';
}

View File

@ -627,7 +627,7 @@ class UOJSubmission {
default:
echo '<div class="d-flex justify-content-between align-items-center">';
echo '<span class="flex-shrink-0 me-2">', $name, '</span>';
echo '<span class="text-end text-truncate d-inline-block">', $this->echoStatusBarTD($id, $cfg), '</span>';
echo '<span class="text-end text-truncate d-inline-block">', $this->echoStatusBarTD($id, $cfg + ['time_font_size' => 'normal']), '</span>';
echo '</div>';
break;

View File

@ -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 '<small>', $this->info[$name], '</small>';
if ($cfg['time_font_size'] == 'small') {
echo '<small>';
}
if ($cfg['time_format'] == 'friendly') {
echo UOJTime::userFriendlyFormat($this->info[$name]);
} else {
echo $this->info[$name];
}
if ($cfg['time_font_size'] == 'small') {
echo '</small>';
}
break;
default:
echo '?';

View File

@ -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 '';
}
}
}