Skip to content

Commit 3dce735

Browse files
committed
feat: implement getTimestamp utility function and corresponding tests
Adds a new utility function `getTimestamp` that returns the current timestamp in ISO string format. Includes unit tests to verify the function's output, ensuring it returns a valid ISO string and is close to the current time.
1 parent 6786ac7 commit 3dce735

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/lib/utils/date.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { getTimestamp } from './date'
3+
4+
// Helper to check if a string is a valid ISO date
5+
function isValidISODate(str: string): boolean {
6+
const d = new Date(str)
7+
return !isNaN(d.getTime()) && str === d.toISOString()
8+
}
9+
10+
describe('getTimestamp', () => {
11+
it('returns a valid ISO string', () => {
12+
const ts = getTimestamp()
13+
expect(isValidISODate(ts)).toBe(true)
14+
})
15+
16+
it('returns a timestamp close to now', () => {
17+
const now = new Date()
18+
const ts = new Date(getTimestamp())
19+
// Should be within 1 second of now
20+
expect(Math.abs(ts.getTime() - now.getTime())).toBeLessThan(1000)
21+
})
22+
})

src/lib/utils/date.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Get the current timestamp in ISO string format
3+
* @returns The current timestamp in ISO string format
4+
*/
5+
export const getTimestamp = () => {
6+
return new Date().toISOString()
7+
}

0 commit comments

Comments
 (0)