Skip to content
This repository was archived by the owner on Feb 15, 2025. It is now read-only.

Commit 1a263a3

Browse files
authored
feat: formats time field values from epoch to ISO (#32)
1 parent f5ce321 commit 1a263a3

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/lib.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ export function parseGitBlamePorcelain(blame: string): {
1515
return fields;
1616
}
1717

18+
export function formatTimeValue(value: string) {
19+
const date = new Date(parseInt(value) * 1000);
20+
21+
return {
22+
dateString: date.toISOString(),
23+
timeAgo: relativeTimePassed(Date.now(), date.getTime()),
24+
};
25+
}
26+
1827
export function relativeTimePassed(now: number, past: number): string {
1928
const msMinutes = 60 * 1000;
2029
const msHours = msMinutes * 60;
@@ -50,13 +59,10 @@ export function relativeTimePassed(now: number, past: number): string {
5059
}
5160

5261
export function formatMessage(fields: Record<string, string>): string {
53-
const elapsed = relativeTimePassed(
54-
Date.now(),
55-
parseInt(fields["author-time"]) * 1000
56-
);
62+
const { timeAgo } = formatTimeValue(fields["author-time"]);
5763
const isUncommitted = fields["author"] === "Not Committed Yet";
5864
const isUnsaved = fields["author"] === "External file (--contents)";
59-
const defaultMessage = `${fields.author}, ${elapsed}${fields.summary}`;
65+
const defaultMessage = `${fields.author}, ${timeAgo}${fields.summary}`;
6066

6167
if (isUncommitted) {
6268
return "Not committed yet";
@@ -70,7 +76,15 @@ export function formatMessage(fields: Record<string, string>): string {
7076
export function formatHoverMessage(fields: Record<string, string>): string {
7177
const header = "| Key | Value |\n| :-- | :-- |\n";
7278
const message = Object.entries(fields)
73-
.map(([k, v]) => `| ${k} | \`${v}\` |`)
79+
.map(([k, v]) => {
80+
if (k === "author-time" || k === "committer-time") {
81+
const { dateString, timeAgo } = formatTimeValue(v);
82+
83+
v = `${dateString} (${timeAgo})`;
84+
}
85+
86+
return `| ${k} | \`${v}\` |`;}
87+
)
7488
.join("\n");
7589
return header + message;
7690
}

0 commit comments

Comments
 (0)