PITR.utils.ts53 lines · main
1import dayjs from 'dayjs'
2
3import { ALL_TIMEZONES } from './PITR.constants'
4import type { Time } from './PITR.types'
5import type { ProjectSelectedAddon } from '@/data/subscriptions/types'
6import { guessLocalTimezone } from '@/lib/dayjs'
7
8export const getPITRRetentionDuration = (addons: ProjectSelectedAddon[]) => {
9 const pitrAddon = addons.find((addon) => addon.type === 'pitr')
10 if (!pitrAddon) return 0
11
12 return (pitrAddon.variant.meta as any)?.backup_duration_days ?? 0
13}
14
15export const getDatesBetweenRange = (startDate: dayjs.Dayjs, endDate: dayjs.Dayjs) => {
16 const diff = endDate.diff(startDate, 'day')
17
18 return Array.from({ length: diff }, (_, index) => startDate.add(index, 'day'))
19}
20
21export const getClientTimezone = () => {
22 const defaultTz = guessLocalTimezone()
23 const utcTz = ALL_TIMEZONES.find((option) => option.value === 'UTC')
24 const timezone = ALL_TIMEZONES.find((option) => {
25 if (option.utc.includes(defaultTz)) return option
26 else return undefined
27 })
28 return timezone ?? (utcTz || ALL_TIMEZONES[0])
29}
30
31export const formatNumberToTwoDigits = (number: Number) => {
32 return number.toLocaleString('en-US', {
33 minimumIntegerDigits: 2,
34 useGrouping: false,
35 })
36}
37
38// Formats Time object to hh:mm:ss
39export const formatTimeToTimeString = (time: Time) => {
40 return `${formatNumberToTwoDigits(time.h)}:${formatNumberToTwoDigits(
41 time.m
42 )}:${formatNumberToTwoDigits(time.s)}`
43}
44
45export function constrainDateToRange(current: dayjs.Dayjs, lower: dayjs.Dayjs, upper: dayjs.Dayjs) {
46 if (current.isBefore(lower)) {
47 return lower
48 }
49 if (current.isAfter(upper)) {
50 return upper
51 }
52 return current
53}