Skip to content

Commit 49bc5b5

Browse files
committed
fix time format on macOS 12.7.6
1 parent e952fe3 commit 49bc5b5

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src-tauri/src/commands.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ func main() {
237237
/// 获取当前时间戳(本地时间格式)
238238
#[tauri::command]
239239
pub fn get_current_timestamp() -> String {
240-
chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
240+
// 使用 ISO 8601 格式,确保在所有系统上都能正确解析
241+
chrono::Local::now().format("%Y-%m-%dT%H:%M:%S").to_string()
241242
}
242243

243244
/// 获取支持的编程语言列表

src/services/tauri.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ export async function getCurrentTimestamp(): Promise<string> {
3434
return await invoke("get_current_timestamp");
3535
} catch (error) {
3636
console.error("Failed to get current timestamp:", error);
37-
// 回退到前端本地时间
37+
// 回退到前端本地时间,使用 ISO 8601 格式
3838
const now = new Date();
3939
const year = now.getFullYear();
4040
const month = String(now.getMonth() + 1).padStart(2, "0");
4141
const day = String(now.getDate()).padStart(2, "0");
4242
const hours = String(now.getHours()).padStart(2, "0");
4343
const minutes = String(now.getMinutes()).padStart(2, "0");
4444
const seconds = String(now.getSeconds()).padStart(2, "0");
45-
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
45+
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
4646
}
4747
}
4848

src/utils/time.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,34 @@ import { useI18n } from "vue-i18n";
22

33
export function formatTime(dateInput: Date | string): string {
44
const { t, locale } = useI18n();
5-
const date = typeof dateInput === "string" ? new Date(dateInput) : dateInput;
5+
6+
let date: Date;
7+
8+
if (typeof dateInput === "string") {
9+
// 尝试解析时间字符串
10+
let parsedDate: Date | null = null;
11+
12+
// 尝试直接解析
13+
try {
14+
parsedDate = new Date(dateInput);
15+
if (!isNaN(parsedDate.getTime())) {
16+
date = parsedDate;
17+
} else {
18+
// 尝试将空格替换为 T 来解析
19+
const isoStr = dateInput.replace(" ", "T");
20+
parsedDate = new Date(isoStr);
21+
if (!isNaN(parsedDate.getTime())) {
22+
date = parsedDate;
23+
} else {
24+
return t("time.invalid_date");
25+
}
26+
}
27+
} catch {
28+
return t("time.invalid_date");
29+
}
30+
} else {
31+
date = dateInput;
32+
}
633

734
// 检查日期是否有效
835
if (isNaN(date.getTime())) {

0 commit comments

Comments
 (0)