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

View File

@@ -17,6 +17,7 @@
"@types/react-dom": "18.0.11",
"autoprefixer": "^10.4.13",
"date-fns": "^2.29.3",
"date-fns-tz": "^2.0.0",
"discord.js": "^14.7.1",
"ioredis": "^5.3.1",
"life360-node-api": "^0.0.10",

4
src/env/schema.mjs vendored
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
});

3
src/env/server.mjs vendored
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) {

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
src/utils/timezone.ts Normal file
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);
}

View File

@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES2016",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": ["dom", "dom.iterable", "esnext", "es2021.intl"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,

View File

@@ -357,6 +357,11 @@ csstype@^3.0.2:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.1.tgz#841b532c45c758ee546a11d5bd7b7b473c8c30b9"
integrity sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==
date-fns-tz@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/date-fns-tz/-/date-fns-tz-2.0.0.tgz#1b14c386cb8bc16fc56fe333d4fc34ae1d1099d5"
integrity sha512-OAtcLdB9vxSXTWHdT8b398ARImVwQMyjfYGkKD2zaGpHseG2UPHbHjXELReErZFxWdSLph3c2zOaaTyHfOhERQ==
date-fns@^2.29.3:
version "2.29.3"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8"