-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create time format helper utility and tests
- Loading branch information
Showing
3 changed files
with
77 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { formatLastUsage } from './time'; | ||
import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest'; | ||
|
||
describe('formatLastUsage', () => { | ||
const NOW = new Date(); | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
vi.setSystemTime(NOW); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
test('formats time within the last hour', () => { | ||
const timestamp = new Date(NOW.getTime() - 30 * 60 * 1000); // 30 minutes ago | ||
expect(formatLastUsage(timestamp)).toBe(`today at ${timestamp.toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric' })}`); | ||
}); | ||
|
||
test('formats time from earlier today', () => { | ||
const timestamp = new Date(NOW.getTime() - 7 * 60 * 60 * 1000); // 7 hours ago | ||
expect(formatLastUsage(timestamp)).toBe(`today at ${timestamp.toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric' })}`); | ||
}); | ||
|
||
test('formats time from yesterday', () => { | ||
const timestamp = new Date(NOW.getTime() - 24 * 60 * 60 * 1000); // 24 hours ago | ||
expect(formatLastUsage(timestamp)).toBe('yesterday'); | ||
}); | ||
|
||
test('formats time from several days ago', () => { | ||
const timestamp = new Date(NOW.getTime() - 5 * 24 * 60 * 60 * 1000); // 5 days ago | ||
expect(formatLastUsage(timestamp)).toBe('5 days ago'); | ||
}); | ||
|
||
test('formats time from last month', () => { | ||
const timestamp = new Date(NOW.getTime() - 30 * 24 * 60 * 60 * 1000); // ~1 month ago | ||
expect(formatLastUsage(timestamp)).toBe('last month'); | ||
}); | ||
|
||
test('formats time from 5 months ago', () => { | ||
const timestamp = new Date(NOW.getTime() - 5 * 30 * 24 * 60 * 60 * 1000); // ~1 month ago | ||
expect(formatLastUsage(timestamp)).toBe('5 months ago'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export const formatLastUsage = (timestamp: Date): string => { | ||
const now = new Date(); | ||
const diffInMillis = timestamp.getTime() - now.getTime(); | ||
const diffInDays = Math.round(diffInMillis / (1000 * 60 * 60 * 24)); | ||
|
||
// If more than 25 days, use months | ||
if (Math.abs(diffInDays) >= 25) { | ||
const diffInMonths = Math.round(diffInDays / 30); | ||
return new Intl.RelativeTimeFormat("en", { | ||
numeric: "auto", | ||
style: "long", | ||
}).format(diffInMonths, "month"); | ||
} | ||
|
||
const relativeTime = new Intl.RelativeTimeFormat("en", { | ||
numeric: "auto", | ||
style: "long", | ||
}).format(diffInDays, "day"); | ||
|
||
// If within last 24 hours, append the time | ||
if (Math.abs(diffInDays) < 1) { | ||
const timeString = new Intl.DateTimeFormat("en", { | ||
hour: "numeric", | ||
minute: "numeric", | ||
}).format(timestamp); | ||
return `${relativeTime} at ${timeString}`; | ||
} | ||
|
||
return relativeTime; | ||
}; |