Skip to content

Commit

Permalink
Add the date util
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhit14 committed Nov 12, 2024
1 parent e4ef171 commit e1aeac1
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/api/src/utils/dateUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const maxDuration = 30 * 24 * 60 * 60 * 1000 // 30 days
const defaultDuration = 7 * 24 * 60 * 60 * 1000 // 7 days

/**
* Validates that the given time range doesn't exceed the max allowed duration.
*
* @param startAt
* @param endAt
* @returns
*/
export const validateDateRange = (startAt: string, endAt: string) => {
const start = new Date(startAt)
const end = new Date(endAt || new Date())
const durationMs = end.getTime() - start.getTime()
return durationMs <= maxDuration
}

/**
* Utility to get default dates if not provided.
* Default to last 7 days
*
* @param startAt
* @param endAt
* @returns
*/
export const getValidatedDates = (startAt?: string, endAt?: string) => {
const now = new Date()

if (!startAt && !endAt) {
return {
startAt: new Date(now.getTime() - defaultDuration).toISOString(),
endAt: null
}
}

if (startAt && !endAt) {
const start = new Date(startAt)
return {
startAt,
endAt: new Date(Math.min(start.getTime() + defaultDuration, now.getTime())).toISOString()
}
}

if (!startAt && endAt) {
const end = new Date(endAt)
return {
startAt: new Date(end.getTime() - defaultDuration).toISOString(),
endAt
}
}

return { startAt, endAt }
}

0 comments on commit e1aeac1

Please sign in to comment.