Setup Timezone specification support

This commit is contained in:
2023-03-09 18:03:07 -06:00
parent d8a9270ce6
commit 17785c89b9
7 changed files with 27 additions and 3 deletions
+3 -1
View File
@@ -1,6 +1,7 @@
// @ts-check
import { z } from 'zod';
const TimezoneSchema = z.enum(Intl.supportedValuesOf("timeZone"));
/**
* Specify your server-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
@@ -18,5 +19,6 @@ export const serverSchema = z.object({
EDGE_CACHE_TIME_SECONDS: z.coerce.number().int().nonnegative().default(60),
REDIS_URL: z.string().url(),
DISCORD_TOKEN: z.string(),
DISCORD_TARGET_USER_ID: z.string()
DISCORD_TARGET_USER_ID: z.string(),
TIMEZONE: TimezoneSchema
});
+2 -1
View File
@@ -19,7 +19,8 @@ const _serverEnv = serverSchema.safeParse({
EDGE_CACHE_TIME_SECONDS: process.env.EDGE_CACHE_TIME_SECONDS,
REDIS_URL: process.env.REDIS_URL,
DISCORD_TOKEN: process.env.DISCORD_TOKEN,
DISCORD_TARGET_USER_ID: process.env.DISCORD_TARGET_USER_ID
DISCORD_TARGET_USER_ID: process.env.DISCORD_TARGET_USER_ID,
TIMEZONE: process.env.TIMEZONE
});
if (_serverEnv.success === false) {
+2
View File
@@ -5,6 +5,7 @@ import { env } from "@/env/server.mjs";
type ResponseData = {
now: number;
status: string;
timezone: string;
};
export default async function handler(
@@ -14,5 +15,6 @@ export default async function handler(
res.status(200).json({
now: new Date().getTime(),
status: req.query.key == env.API_KEY ? "Authorized" : "Unauthorized",
timezone: env.TIMEZONE
});
}
+13
View File
@@ -0,0 +1,13 @@
import { utcToZonedTime } from 'date-fns-tz';
import { z } from 'zod';
import { env } from '@/env/server.mjs';
// @ts-ignore TS2339 -- TODO: Figure out why Intl.supportedValuesOf isn't seen by Typescript
export const TimezoneSchema = z.enum(Intl.supportedValuesOf('timeZone'));
export type Timezone = z.infer<typeof TimezoneSchema>;
export function localNow(now?: Date, zone?: Timezone): Date {
zone = zone ?? env.TIMEZONE;
now = now ?? new Date();
return utcToZonedTime(now, zone);
}