Deduplicate timezone date helper

This commit is contained in:
Janpeter Visser 2026-04-19 10:37:58 +02:00
parent 1a64590545
commit 307686de68
3 changed files with 21 additions and 40 deletions

View file

@ -1,5 +1,6 @@
import { getAuthenticatedUser } from "@/lib/auth/session";
import { deriveBudgetSnapshot } from "@/lib/check-in/budget";
import { getLocalDateForTimezone } from "@/lib/dates";
import { ensureProfileBundleForCurrentUser } from "@/lib/profile/service";
import { createClient } from "@/lib/supabase/server";
import type {
@ -53,26 +54,6 @@ function mapMorningCheckInRow(row: MorningCheckInRow): MorningCheckInRecord {
};
}
function getLocalDateForTimezone(timezone: string, date = new Date()) {
const formatter = new Intl.DateTimeFormat("en-CA", {
timeZone: timezone,
year: "numeric",
month: "2-digit",
day: "2-digit",
});
const parts = formatter.formatToParts(date);
const year = parts.find((part) => part.type === "year")?.value;
const month = parts.find((part) => part.type === "month")?.value;
const day = parts.find((part) => part.type === "day")?.value;
if (!year || !month || !day) {
throw new Error("Lokale datum voor timezone kon niet worden bepaald.");
}
return `${year}-${month}-${day}`;
}
async function readMorningCheckInByDate(
supabase: SupabaseServerClient,
userId: string,

19
lib/dates.ts Normal file
View file

@ -0,0 +1,19 @@
export function getLocalDateForTimezone(timezone: string, date = new Date()) {
const formatter = new Intl.DateTimeFormat("en-CA", {
timeZone: timezone,
year: "numeric",
month: "2-digit",
day: "2-digit",
});
const parts = formatter.formatToParts(date);
const year = parts.find((part) => part.type === "year")?.value;
const month = parts.find((part) => part.type === "month")?.value;
const day = parts.find((part) => part.type === "day")?.value;
if (!year || !month || !day) {
throw new Error("Lokale datum voor timezone kon niet worden bepaald.");
}
return `${year}-${month}-${day}`;
}

View file

@ -1,4 +1,5 @@
import { getAuthenticatedUser } from "@/lib/auth/session";
import { getLocalDateForTimezone } from "@/lib/dates";
import { calculateDayOverviewSnapshot } from "@/lib/planning/day-overview";
import type {
ActivityCategory,
@ -104,26 +105,6 @@ function mapActivityRow(row: ActivityRow): ActivityRecord {
};
}
function getLocalDateForTimezone(timezone: string, date = new Date()) {
const formatter = new Intl.DateTimeFormat("en-CA", {
timeZone: timezone,
year: "numeric",
month: "2-digit",
day: "2-digit",
});
const parts = formatter.formatToParts(date);
const year = parts.find((part) => part.type === "year")?.value;
const month = parts.find((part) => part.type === "month")?.value;
const day = parts.find((part) => part.type === "day")?.value;
if (!year || !month || !day) {
throw new Error("Lokale plandatum voor timezone kon niet worden bepaald.");
}
return `${year}-${month}-${day}`;
}
function assertIsoDate(value: string) {
if (!ISO_DATE_PATTERN.test(value)) {
throw new Error("Ongeldige plandatum. Gebruik het formaat YYYY-MM-DD.");