PITR.utils.ts 1.6 KB

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