Show Beijing time across admin UI and ntfy payloads.

Format ISO/unix stamps with Asia/Shanghai so join leads and alerts match China local clock.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Eric 2026-11-05 19:35:30 +08:00
parent 344886172d
commit d830097b00
12 changed files with 97 additions and 22 deletions

View File

@ -126,9 +126,24 @@ export function fmtYuan(fen: number) {
return `¥${(Number(fen || 0) / 100).toFixed(2)}`
}
/** 管理后台统一北京时间(Asia/Shanghai)。 */
export function fmtTime(sec?: number | null) {
if (!sec) return '—'
return new Date(sec * 1000).toLocaleString()
const ms = sec < 1e12 ? sec * 1000 : sec
try {
return new Intl.DateTimeFormat('zh-CN', {
timeZone: 'Asia/Shanghai',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
}).format(new Date(ms))
} catch {
return new Date(ms).toISOString().slice(0, 19).replace('T', ' ')
}
}
export function shortId(id?: string | null, keep = 8) {

View File

@ -2,7 +2,9 @@
from __future__ import annotations
import logging
from datetime import datetime
from typing import Any
from zoneinfo import ZoneInfo
import httpx
@ -11,6 +13,19 @@ from ebook_svc.config import settings
logger = logging.getLogger(__name__)
DEFAULT_ICON = "https://nomadro.com/icon-192.png"
_BEIJING = ZoneInfo("Asia/Shanghai")
def format_beijing_time(ts: float | int | None = None) -> str:
"""Admin / ntfy 统一北京时间。"""
try:
if ts is None:
d = datetime.now(_BEIJING)
else:
d = datetime.fromtimestamp(float(ts), tz=_BEIJING)
return d.strftime("%Y-%m-%d %H:%M:%S")
except Exception:
return datetime.now(_BEIJING).strftime("%Y-%m-%d %H:%M:%S")
async def notify(

View File

@ -242,6 +242,7 @@ def format_event_message(ev: dict[str, Any], *, openid: str | None, client: str
bullets: list[tuple[str, str]] = [
("用户", _short_openid(openid)),
("渠道", _channel_label(client)),
("时间", f"{ntfy_svc.format_beijing_time(ev.get('ts'))}(北京时间)"),
]
if ev.get("page"):
bullets.append(("页面", str(ev["page"])))

View File

@ -0,0 +1,38 @@
/** Admin UI clock: always Asia/Shanghai (北京时间). */
const BEIJING: Intl.DateTimeFormatOptions = {
timeZone: 'Asia/Shanghai',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
}
function toDate(v: unknown): Date | null {
if (v == null || v === '') return null
if (typeof v === 'number' && Number.isFinite(v)) {
return new Date(v < 1e12 ? v * 1000 : v)
}
const s = String(v).trim()
if (!s) return null
if (/^\d+$/.test(s)) {
const n = Number(s)
return new Date(n < 1e12 ? n * 1000 : n)
}
const d = new Date(s)
return Number.isNaN(d.getTime()) ? null : d
}
/** Format any ISO / unix / Date-like value as 北京时间. */
export function formatBeijingTime(v: unknown): string {
const d = toDate(v)
if (!d) return '—'
try {
return new Intl.DateTimeFormat('zh-CN', BEIJING).format(d)
} catch {
return d.toISOString().slice(0, 19).replace('T', ' ')
}
}

View File

@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { api } from '../api/client'
import { formatBeijingTime } from '../lib/beijingTime'
type TrackEvent = {
id: number
@ -54,9 +55,7 @@ function clientLabel(c: string) {
}
function fmtTime(ts: number) {
if (!ts) return '—'
const d = new Date(ts < 1e12 ? ts * 1000 : ts)
return d.toLocaleString('zh-CN', { hour12: false })
return formatBeijingTime(ts)
}
function shortId(id: string, n = 8) {

View File

@ -1,5 +1,6 @@
import { useCallback, useEffect, useState } from 'react'
import { api } from '../api/client'
import { formatBeijingTime } from '../lib/beijingTime'
type Usage = { totalBytes: number; usedBytes: number; usedPercent: number }
type Health = {
@ -35,7 +36,7 @@ export default function Operations() {
</header>
{error && <div className="com-toast com-toast-err" role="alert">{error} {data ? '下方保留上次检查结果。' : ''}</div>}
{data ? <>
<section className={`com-health-banner ${data.ok && !error ? 'is-healthy' : 'is-degraded'}`} role="status"><span className="com-status-dot" /><div><strong>{error ? '状态更新失败' : data.ok ? '核心服务运行正常' : '部分服务需要检查'}</strong><p>上次检查:{new Date(data.checkedAt).toLocaleString('zh-CN', { hour12: false })}</p></div></section>
<section className={`com-health-banner ${data.ok && !error ? 'is-healthy' : 'is-degraded'}`} role="status"><span className="com-status-dot" /><div><strong>{error ? '状态更新失败' : data.ok ? '核心服务运行正常' : '部分服务需要检查'}</strong><p>上次检查:{formatBeijingTime(data.checkedAt)}(北京时间)</p></div></section>
<div className="com-health-grid">{data.checks.map(check => <article className="com-panel" key={check.name}><span className={`com-badge tone-${check.ok ? 'ok' : 'bad'}`}>{check.ok ? '可用' : '异常'}</span><h2>{check.name}</h2><strong className="com-health-value">{check.latencyMs}<small> ms</small></strong><p className="com-muted">{check.status ? `HTTP ${check.status}` : '连接失败或检查超时'}</p></article>)}</div>
<div className="com-health-grid">{(['memory', 'disk'] as const).map(key => {
const usage = data.resources[key]

View File

@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { api } from '../api/client'
import { formatBeijingTime } from '../lib/beijingTime'
import { orderState } from '../lib/orders'
import { downloadCsv } from '../lib/csv'
@ -17,9 +18,7 @@ type Order = {
type StatusTab = 'all' | 'paid' | 'pending' | 'other'
function fmtTime(ts: number) {
if (!ts) return '—'
const d = new Date(ts < 1e12 ? ts * 1000 : ts)
return d.toLocaleString('zh-CN', { hour12: false })
return formatBeijingTime(ts)
}
function fmtYuan(fen: number) {

View File

@ -5,6 +5,7 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { Link, useParams } from 'react-router-dom'
import { api } from '../api/client'
import { formatBeijingTime } from '../lib/beijingTime'
import { useConfigLive } from '../lib/useConfigLive'
import { useDraft } from '../lib/useDraft'
import UnsavedChanges from '../components/UnsavedChanges'
@ -205,7 +206,11 @@ export default function Pricing() {
{cfg.flags?.payEnabled !== false ? '支付开' : '支付关'}
</span>
<span className="com-chip">v{cfg.version ?? '—'}</span>
{cfg.updatedAt ? <span className="com-chip">{cfg.updatedAt}</span> : null}
{cfg.updatedAt ? (
<span className="com-chip" title="北京时间">
{formatBeijingTime(cfg.updatedAt)}
</span>
) : null}
</div>
</header>

View File

@ -5,6 +5,7 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { Link, useParams } from 'react-router-dom'
import { api } from '../api/client'
import { formatBeijingTime } from '../lib/beijingTime'
import { useConfigLive } from '../lib/useConfigLive'
import { useDraft } from '../lib/useDraft'
import UnsavedChanges from '../components/UnsavedChanges'
@ -260,7 +261,11 @@ export default function Switches() {
? '当前免费'
: `现价 ¥${((Number(cfg.copy?.vipPriceFen) || 0) / 100).toFixed((Number(cfg.copy?.vipPriceFen) || 0) % 100 === 0 ? 0 : 2)}`}
</span>
{cfg.updatedAt ? <span className="com-chip">{cfg.updatedAt}</span> : null}
{cfg.updatedAt ? (
<span className="com-chip" title="北京时间">
{formatBeijingTime(cfg.updatedAt)}
</span>
) : null}
</div>
</header>

View File

@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { api } from '../api/client'
import { formatBeijingTime } from '../lib/beijingTime'
type UnlockRow = { book_id: string; unlocked_at: number; source: string }
@ -17,9 +18,7 @@ type UserRow = {
type Tab = 'all' | 'unlocked' | 'vip' | 'locked' | 'blocked'
function fmtTime(ts: number) {
if (!ts) return '—'
const d = new Date(ts < 1e12 ? ts * 1000 : ts)
return d.toLocaleString('zh-CN', { hour12: false })
return formatBeijingTime(ts)
}
function shortId(id: string, n = 10) {

View File

@ -1,5 +1,7 @@
/** 游民社区后台 · 分区与字段元数据 */
import { formatBeijingTime } from '../lib/beijingTime'
export type SectionKey =
| 'overview'
| 'messages'
@ -318,14 +320,9 @@ export function labelOf(key: string): string {
return FIELD_LABEL[key] || key
}
/** 管理后台统一北京时间(Asia/Shanghai)。 */
export function fmtTime(v: unknown) {
if (v == null || v === '') return '—'
const s = String(v)
if (/^\d+$/.test(s)) {
const n = Number(s)
return new Date(n < 1e12 ? n * 1000 : n).toLocaleString('zh-CN', { hour12: false })
}
return s.slice(0, 19).replace('T', ' ')
return formatBeijingTime(v)
}
export function short(v: unknown, n = 48) {

View File

@ -61,7 +61,7 @@ function shortUid(uid: string | undefined): string {
function fmtTime(ts?: number): string {
const d = ts && Number.isFinite(ts) ? new Date(ts) : new Date();
try {
return new Intl.DateTimeFormat("zh-CN", {
const stamp = new Intl.DateTimeFormat("zh-CN", {
timeZone: "Asia/Shanghai",
year: "numeric",
month: "2-digit",
@ -71,8 +71,9 @@ function fmtTime(ts?: number): string {
second: "2-digit",
hour12: false,
}).format(d);
return `${stamp}(北京时间)`;
} catch {
return d.toISOString();
return `${d.toISOString()}(北京时间)`;
}
}