mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem Changing the restore date resets the entered time, and the repeated hour at the end of daylight saving time can resolve to the wrong occurrence. Both can change the submitted recovery timestamp. ## Fix Store the recovery point as a Unix timestamp and preserve its occurrence when editing an ambiguous local time. Calendar edits carry the entered hours, minutes, and seconds onto the selected day, applying that day's timezone offset and retaining the existing backup-range clamping. This focuses on date edits and repeated-hour handling from #48903. It changes only the PITR form, its utilities, and regression tests. Related to [FE-4129](https://linear.app/supabase/issue/FE-4129/r2np-datetime-widget). ## How to test - In PITR or restore-to-new-project, enter a time and select another available calendar day. Expected result: the entered hours, minutes, and seconds remain selected, and the submitted timestamp matches the new date. - Run the PITRForm and PITR.utils Vitest suites. Their fixtures cover both occurrences of the repeated Madrid/New York hour, edits across DST, spring-forward normalization, fractional offsets, and backup boundaries. - Twelve focused regression cases passed under a Madrid browser timezone. Those cases plus the existing selector component test passed under Hong Kong, thirteen tests total. All CI checks passed, including Studio tests, all four Next.js/TanStack E2E shards, typecheck, lint ratchet, formatting, spelling, unused-code checks, and Docker build.
81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
import dayjs from 'dayjs'
|
|
|
|
import type { Time } from './PITR.types'
|
|
import type { ProjectSelectedAddon } from '@/data/subscriptions/types'
|
|
import { findTimezoneByIana, TIMEZONES_BY_IANA } from '@/lib/constants/timezones'
|
|
import { guessLocalTimezone } from '@/lib/dayjs'
|
|
|
|
export const getPITRRetentionDuration = (addons: ProjectSelectedAddon[]) => {
|
|
const pitrAddon = addons.find((addon) => addon.type === 'pitr')
|
|
if (!pitrAddon) return 0
|
|
|
|
return (pitrAddon.variant.meta as any)?.backup_duration_days ?? 0
|
|
}
|
|
|
|
export const getDatesBetweenRange = (startDate: dayjs.Dayjs, endDate: dayjs.Dayjs) => {
|
|
const diff = endDate.diff(startDate, 'day')
|
|
|
|
return Array.from({ length: diff }, (_, index) => startDate.add(index, 'day'))
|
|
}
|
|
|
|
export const getClientTimezone = () => {
|
|
const utcTimezone = TIMEZONES_BY_IANA.find((option) => option.value === 'UTC')
|
|
return findTimezoneByIana(guessLocalTimezone()) ?? utcTimezone ?? TIMEZONES_BY_IANA[0]
|
|
}
|
|
|
|
export const formatNumberToTwoDigits = (number: Number) => {
|
|
return number.toLocaleString('en-US', {
|
|
minimumIntegerDigits: 2,
|
|
useGrouping: false,
|
|
})
|
|
}
|
|
|
|
// Formats Time object to hh:mm:ss
|
|
export const formatTimeToTimeString = (time: Time) => {
|
|
return `${formatNumberToTwoDigits(time.h)}:${formatNumberToTwoDigits(
|
|
time.m
|
|
)}:${formatNumberToTwoDigits(time.s)}`
|
|
}
|
|
|
|
export function constrainDateToRange(current: dayjs.Dayjs, lower: dayjs.Dayjs, upper: dayjs.Dayjs) {
|
|
if (current.isBefore(lower)) {
|
|
return lower
|
|
}
|
|
if (current.isAfter(upper)) {
|
|
return upper
|
|
}
|
|
return current
|
|
}
|
|
|
|
export function getRestoreDate(unix: number, timezone: string) {
|
|
const instant = dayjs.unix(unix).utc()
|
|
const offset = new Intl.DateTimeFormat('en-US', {
|
|
timeZone: timezone,
|
|
timeZoneName: 'longOffset',
|
|
})
|
|
.formatToParts(instant.toDate())
|
|
.find((part) => part.type === 'timeZoneName')?.value
|
|
const match = offset?.match(/^GMT([+-])(\d{2}):(\d{2})$/)
|
|
if (!match) return instant
|
|
|
|
const minutes = Number(match[2]) * 60 + Number(match[3])
|
|
return instant.utcOffset(match[1] === '-' ? -minutes : minutes)
|
|
}
|
|
|
|
export function updateRestoreDate(
|
|
current: dayjs.Dayjs,
|
|
date: string,
|
|
time: Time,
|
|
timezone: string
|
|
) {
|
|
const wallTime = dayjs.utc(date).hour(time.h).minute(time.m).second(time.s)
|
|
const candidate = getRestoreDate(
|
|
wallTime.subtract(current.utcOffset(), 'minute').unix(),
|
|
timezone
|
|
)
|
|
const format = 'YYYY-MM-DD HH:mm:ss'
|
|
if (candidate.format(format) === wallTime.format(format)) return candidate
|
|
|
|
return getRestoreDate(dayjs.tz(wallTime.format(format), timezone).unix(), timezone)
|
|
}
|