Vercel Edge Functions: Missing Automatic Time‑Zone Conversion Configuration
0 reputation · 26 Dec 2022, 06:54 UTC
0 reputation · 26 Dec 2022, 06:54 UTC
The goal is to let Vercel Edge Functions automatically convert UTC timestamps to a visitor’s local time zone using information present in the HTTP request, so developers do not need to write custom Intl conversion logic.
Constraints include the stateless nature of Edge Functions, which execute in a global CDN and default to UTC for Date.now(), the lack of a built‑in mapping from headers such as Accept‑Language to a time zone, and the need to persist any user‑selected zone in an external store like a cookie or database.
The unresolved decision is whether Vercel should provide a project‑level configuration flag, a runtime helper, or an API that performs this conversion automatically, and what privacy, security, or performance trade‑offs such a feature would introduce.
33275 reputation · 26 Dec 2022, 08:59 UTC
Vercel Edge Functions do not currently provide a project-level configuration flag or a built-in runtime helper to automatically convert UTC timestamps to a visitor's local time zone. The Edge runtime executes in a V8 isolate where the system time is locked to UTC; therefore, Date.now() and new Date() will always return UTC values regardless of the visitor's location.
Implementing automatic conversion at the platform level introduces several technical and privacy challenges:
Accept-Language header indicates locale preferences, not geographic time zones. A user with en-US may be physically located in Tokyo.x-vercel-ip-country header, mapping a country to a time zone is imprecise, as many countries span multiple IANA time zones.Date object to force a local time zone could cause unpredictable failures in third-party libraries that expect standard UTC behavior in serverless environments.Since automatic conversion is unavailable, the standard approach is to pass the time zone as a parameter from the client or infer it via a cookie. You can then use the Intl.DateTimeFormat API, which is supported in the Edge runtime.
export default async function handler(req) {
// Assume the time zone is passed via a cookie or custom header
const userTimeZone = req.cookies.get('timezone') || 'UTC';
const date = new Date();
const formatter = new Intl.DateTimeFormat('en-US', {
timeZone: userTimeZone,
dateStyle: 'medium',
timeStyle: 'long',
});
return new Response(`Local time: ${formatter.format(date)}`);
}
new Date().toString() to confirm the default UTC output.x-vercel-ip-country is present for geographic approximations.Intl.DateTimeFormat with a known IANA zone (e.g., America/New_York) to ensure the runtime processes the conversion correctly.Diagnostic Detail Needed: Are you relying on IP-based geolocation for these conversions, or do you have access to a user-defined time zone preference stored in a database or cookie?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.