Skip to content

Commit a1251cd

Browse files
committed
fix: 修复时间戳解析丢失小数部分的问题
- 将 parseInt 改为 parseFloat,保留小数点后的精度 - 修复查询 1762596442.349 时返回 1762596442.339 记录的问题 - 确保带小数点的 Unix 时间戳(秒或毫秒)能正确解析和比较 - 提高时间戳比较的精度,避免因截断导致的查询错误
1 parent 58d6dc4 commit a1251cd

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

server/route/v2/change.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ changeRouter.get(
8484

8585
// 解析时间戳,支持多种格式
8686
function parseTimestamp(timestamp: string): Date {
87-
// 尝试解析为 Unix 时间戳(秒)
88-
const unixSeconds = parseInt(timestamp, 10);
89-
if (!isNaN(unixSeconds) && unixSeconds > 0) {
87+
// 尝试解析为 Unix 时间戳(支持带小数点的秒或毫秒)
88+
// 使用 parseFloat 保留小数部分,确保精度
89+
const unixTimestamp = parseFloat(timestamp);
90+
if (!isNaN(unixTimestamp) && unixTimestamp > 0) {
9091
// 判断是秒还是毫秒(大于 10^12 认为是毫秒)
91-
if (unixSeconds > 1000000000000) {
92-
return new Date(unixSeconds); // 毫秒
92+
if (unixTimestamp > 1000000000000) {
93+
return new Date(unixTimestamp); // 毫秒(支持小数点)
9394
} else {
94-
return new Date(unixSeconds * 1000); // 秒
95+
return new Date(unixTimestamp * 1000); // 秒(支持小数点,转换为毫秒)
9596
}
9697
}
9798

0 commit comments

Comments
 (0)