Skip to content

Commit

Permalink
fix: date utils and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 committed Jun 25, 2024
1 parent 7366989 commit b5ca170
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
42 changes: 18 additions & 24 deletions src/lib/utils/__tests__/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,66 @@ import { getDateMinusMinutes, getDatePlusMinutes, getDatePlusSeconds } from '$li

describe('getDatePlusMinutes()', () => {
it('returns the current date and time plus the specified minutes', () => {
const now = new Date()
const minutesToAdd = 5
const result = getDatePlusMinutes(minutesToAdd)
const expected = new Date(now.getTime() + minutesToAdd * 60 * 1000)
expect(result.getTime()).toBeCloseTo(expected.getTime(), 2)
const expected = new Date(Date.now() + minutesToAdd * 60 * 1000)
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})

it('handles the edge case of zero minutes', () => {
const now = new Date()
const result = getDatePlusMinutes(0)
expect(result.getTime()).toBeCloseTo(now.getTime(), 2)
const expected = new Date()
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})

it('handles the edge case of negative minutes', () => {
const now = new Date()
const minutesToAdd = -5
const result = getDatePlusMinutes(minutesToAdd)
const expected = new Date(now.getTime() + minutesToAdd * 60 * 1000)
expect(result.getTime()).toBeCloseTo(expected.getTime(), 2)
const expected = new Date(Date.now() + minutesToAdd * 60 * 1000)
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})
})

describe('getDateMinusMinutes()', () => {
it('returns the current date and time minus the specified minutes', () => {
const now = new Date()
const minutesToSubtract = 5
const result = getDateMinusMinutes(minutesToSubtract)
const expected = new Date(now.getTime() - minutesToSubtract * 60 * 1000)
expect(result.getTime()).toBeCloseTo(expected.getTime(), 2)
const expected = new Date(Date.now() - minutesToSubtract * 60 * 1000)
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})

it('handles the edge case of zero minutes', () => {
const now = new Date()
const result = getDateMinusMinutes(0)
expect(result.getTime()).toBeCloseTo(now.getTime(), 2)
const expected = new Date()
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})

it('handles the edge case of negative minutes', () => {
const now = new Date()
const minutesToSubtract = -5
const result = getDateMinusMinutes(minutesToSubtract)
const expected = new Date(now.getTime() - minutesToSubtract * 60 * 1000)
expect(result.getTime()).toBeCloseTo(expected.getTime(), 2)
const expected = new Date(Date.now() - minutesToSubtract * 60 * 1000)
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})
})

describe('getDatePlusSeconds()', () => {
it('returns the current date and time plus the specified seconds', () => {
const now = new Date()
const secondsToAdd = 5
const result = getDatePlusSeconds(secondsToAdd)
const expected = new Date(now.getTime() + secondsToAdd * 1000)
expect(result.getTime()).toBeCloseTo(expected.getTime(), 2)
const expected = new Date(Date.now() + secondsToAdd * 1000)
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})

it('handles the edge case of zero seconds', () => {
const now = new Date()
const result = getDatePlusSeconds(0)
expect(result.getTime()).toBeCloseTo(now.getTime(), 2)
const expected = new Date()
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})

it('handles the edge case of negative seconds', () => {
const now = new Date()
const secondsToAdd = -5
const result = getDatePlusSeconds(secondsToAdd)
const expected = new Date(now.getTime() + secondsToAdd * 1000)
expect(result.getTime()).toBeCloseTo(expected.getTime(), 2)
const expected = new Date(Date.now() + secondsToAdd * 1000)
expect(expected.getTime() - result.getTime()).toBeLessThanOrEqual(1)
})
})
13 changes: 6 additions & 7 deletions src/lib/utils/date.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
export function getDatePlusMinutes(minutes: number): Date {
const milliseconds = minutes * 60 * 1000
return new Date(new Date().getTime() + milliseconds)
export function getDatePlusMinutes(minuteOffset: number): Date {
const millisecondOffset = minuteOffset * 60 * 1000
return new Date(Date.now() + millisecondOffset)
}

export function getDateMinusMinutes(minutes: number): Date {
const milliseconds = minutes * 60 * 1000
return new Date(new Date().getTime() - milliseconds)
const millisecondsOffset = minutes * 60 * 1000
return new Date(Date.now() - millisecondsOffset)
}

export function getDatePlusSeconds(seconds: number): Date {
const milliseconds = seconds * 1000
return new Date(new Date().getTime() + milliseconds)
return new Date(Date.now() + seconds * 1000)
}

0 comments on commit b5ca170

Please sign in to comment.