PITR.utils.ts53 lines · main
| 1 | import dayjs from 'dayjs' |
| 2 | |
| 3 | import { ALL_TIMEZONES } from './PITR.constants' |
| 4 | import type { Time } from './PITR.types' |
| 5 | import type { ProjectSelectedAddon } from '@/data/subscriptions/types' |
| 6 | import { guessLocalTimezone } from '@/lib/dayjs' |
| 7 | |
| 8 | export 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 | |
| 15 | export 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 | |
| 21 | export 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 | |
| 31 | export 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 |
| 39 | export const formatTimeToTimeString = (time: Time) => { |
| 40 | return `${formatNumberToTwoDigits(time.h)}:${formatNumberToTwoDigits( |
| 41 | time.m |
| 42 | )}:${formatNumberToTwoDigits(time.s)}` |
| 43 | } |
| 44 | |
| 45 | export 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 | } |