Skip to content

Commit c0509d5

Browse files
committed
fix(timestamp): invalid date cannot auto run
1 parent d7459c9 commit c0509d5

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Panel/Timestamp.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PanelType } from "../shared";
22
import { ToolPanel } from "../common/ToolPanel";
33
import * as vscode from "vscode";
44
import i18n from "../i18n";
5+
import { isValidDate } from "../utils";
56

67
export class Timestamp extends ToolPanel<Timestamp> {
78
constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) {
@@ -19,8 +20,13 @@ export class Timestamp extends ToolPanel<Timestamp> {
1920

2021
public static canBeTreatedByTool(data: string): boolean | PanelType {
2122
try {
22-
new Date(data);
23-
return PanelType.timestamp;
23+
const d = new Date(data);
24+
const e = new Date(Number(data));
25+
if (isValidDate(d) || isValidDate(e)) {
26+
return PanelType.timestamp;
27+
} else {
28+
return false;
29+
}
2430
} catch (error) {
2531
return false;
2632
}

src/utils/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ export async function asyncFilter<T>(
1919
)
2020
).filter((i) => i !== fail);
2121
}
22+
23+
export function isValidDate(d: Date): boolean {
24+
return d instanceof Date && !isNaN(d.getTime());
25+
}

0 commit comments

Comments
 (0)