统一页面时间为东八区并调整活动条数

This commit is contained in:
SepComet 2026-05-07 09:36:01 +08:00
parent d46f69c9a6
commit f0397730e6
4 changed files with 43 additions and 14 deletions

View File

@ -76,7 +76,7 @@ function summarizeActivity(item: GiteaActivityData['recent'][number]) {
} }
} }
const recentActivities = activity.recent.slice(0, 3).map(summarizeActivity); const recentActivities = activity.recent.slice(0, 4).map(summarizeActivity);
const isPlaceholder = activity.source === 'placeholder'; const isPlaceholder = activity.source === 'placeholder';
--- ---

View File

@ -1,4 +1,6 @@
--- ---
import { formatDate } from '../utils/datetime';
interface Props { interface Props {
slug: string; slug: string;
title: string; title: string;
@ -9,11 +11,7 @@ interface Props {
} }
const { slug, title, date, repo, summary, tags } = Astro.props; const { slug, title, date, repo, summary, tags } = Astro.props;
const formattedDate = date.toLocaleDateString('zh-CN', { const formattedDate = formatDate(date);
year: 'numeric',
month: '2-digit',
day: '2-digit',
});
--- ---
<article class="card"> <article class="card">

View File

@ -2,6 +2,7 @@
import { getCollection, render } from 'astro:content'; import { getCollection, render } from 'astro:content';
import Layout from '../../layouts/Layout.astro'; import Layout from '../../layouts/Layout.astro';
import { site } from '../../config'; import { site } from '../../config';
import { formatDate } from '../../utils/datetime';
export async function getStaticPaths() { export async function getStaticPaths() {
const logs = await getCollection('logs'); const logs = await getCollection('logs');
@ -14,11 +15,7 @@ export async function getStaticPaths() {
const { log } = Astro.props; const { log } = Astro.props;
const { Content } = await render(log); const { Content } = await render(log);
const formattedDate = log.data.date.toLocaleDateString('zh-CN', { const formattedDate = formatDate(log.data.date);
year: 'numeric',
month: '2-digit',
day: '2-digit',
});
--- ---
<Layout <Layout

View File

@ -1,14 +1,34 @@
export function formatDateTime(value?: string) { const DISPLAY_TIME_ZONE = 'Asia/Shanghai';
type DateInput = string | number | Date | undefined;
function normalizeTimestamp(value?: DateInput) {
if (!value) { if (!value) {
return ''; return '';
} }
const time = Date.parse(value); const time =
value instanceof Date
? value.getTime()
: typeof value === 'number'
? value
: Date.parse(value);
if (Number.isNaN(time)) { if (Number.isNaN(time)) {
return value; return typeof value === 'string' ? value : '';
}
return time;
}
export function formatDateTime(value?: DateInput) {
const time = normalizeTimestamp(value);
if (time === '') {
return '';
} }
return new Intl.DateTimeFormat('zh-CN', { return new Intl.DateTimeFormat('zh-CN', {
timeZone: DISPLAY_TIME_ZONE,
year: 'numeric', year: 'numeric',
month: '2-digit', month: '2-digit',
day: '2-digit', day: '2-digit',
@ -18,6 +38,20 @@ export function formatDateTime(value?: string) {
}).format(new Date(time)); }).format(new Date(time));
} }
export function formatDate(value?: DateInput) {
const time = normalizeTimestamp(value);
if (time === '') {
return '';
}
return new Intl.DateTimeFormat('zh-CN', {
timeZone: DISPLAY_TIME_ZONE,
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).format(new Date(time));
}
export function getLatestDate(values: string[]) { export function getLatestDate(values: string[]) {
const timestamps = values.map((value) => Date.parse(value)).filter((value) => !Number.isNaN(value)); const timestamps = values.map((value) => Date.parse(value)).filter((value) => !Number.isNaN(value));
if (timestamps.length === 0) { if (timestamps.length === 0) {