This commit is contained in:
2026-06-25 18:24:29 +08:00
commit a1190d7d9a
90 changed files with 5459 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
// utils/format.js — 时长 / 距离 / 日期格式化
/** 秒 → "MM:SS" 或 "HH:MM:SS" */
function formatDuration(sec) {
sec = Math.max(0, Math.floor(sec || 0));
const h = Math.floor(sec / 3600);
const m = Math.floor((sec % 3600) / 60);
const s = sec % 60;
const pad = (n) => String(n).padStart(2, '0');
return h > 0 ? `${pad(h)}:${pad(m)}:${pad(s)}` : `${pad(m)}:${pad(s)}`;
}
/** 秒 → "12分34秒"(历史列表用) */
function formatDurationCN(sec) {
sec = Math.max(0, Math.floor(sec || 0));
const m = Math.floor(sec / 60);
const s = sec % 60;
return `${m}${String(s).padStart(2, '0')}`;
}
/** 公里数 → "1.2km"null → "距离未记录" */
function formatDistance(km) {
if (km == null) return '距离未记录';
return `${Number(km).toFixed(1)}km`;
}
/** 时间戳 → 本地 "yyyy-mm-dd" */
function toDateStr(ts, tzOffsetHours = 8) {
const d = new Date(ts + tzOffsetHours * 3600 * 1000);
const y = d.getUTCFullYear();
const m = String(d.getUTCMonth() + 1).padStart(2, '0');
const day = String(d.getUTCDate()).padStart(2, '0');
return `${y}-${m}-${day}`;
}
/** 时间戳 → "今天 早上 / 昨天 傍晚" 之类的友好时段(简版占位) */
function friendlyTime(ts) {
// TODO: 阶段 2 完整实现(今天/昨天/前天 + 时段)
return new Date(ts).toLocaleString();
}
module.exports = {
formatDuration,
formatDurationCN,
formatDistance,
toDateStr,
friendlyTime,
};