mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-06 01:14:30 -06:00
Setup Timezone specification support
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
"@types/react-dom": "18.0.11",
|
"@types/react-dom": "18.0.11",
|
||||||
"autoprefixer": "^10.4.13",
|
"autoprefixer": "^10.4.13",
|
||||||
"date-fns": "^2.29.3",
|
"date-fns": "^2.29.3",
|
||||||
|
"date-fns-tz": "^2.0.0",
|
||||||
"discord.js": "^14.7.1",
|
"discord.js": "^14.7.1",
|
||||||
"ioredis": "^5.3.1",
|
"ioredis": "^5.3.1",
|
||||||
"life360-node-api": "^0.0.10",
|
"life360-node-api": "^0.0.10",
|
||||||
|
|||||||
4
src/env/schema.mjs
vendored
4
src/env/schema.mjs
vendored
@@ -1,6 +1,7 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
const TimezoneSchema = z.enum(Intl.supportedValuesOf("timeZone"));
|
||||||
/**
|
/**
|
||||||
* Specify your server-side environment variables schema here.
|
* Specify your server-side environment variables schema here.
|
||||||
* This way you can ensure the app isn't built with invalid env vars.
|
* 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),
|
EDGE_CACHE_TIME_SECONDS: z.coerce.number().int().nonnegative().default(60),
|
||||||
REDIS_URL: z.string().url(),
|
REDIS_URL: z.string().url(),
|
||||||
DISCORD_TOKEN: z.string(),
|
DISCORD_TOKEN: z.string(),
|
||||||
DISCORD_TARGET_USER_ID: z.string()
|
DISCORD_TARGET_USER_ID: z.string(),
|
||||||
|
TIMEZONE: TimezoneSchema
|
||||||
});
|
});
|
||||||
|
|||||||
3
src/env/server.mjs
vendored
3
src/env/server.mjs
vendored
@@ -19,7 +19,8 @@ const _serverEnv = serverSchema.safeParse({
|
|||||||
EDGE_CACHE_TIME_SECONDS: process.env.EDGE_CACHE_TIME_SECONDS,
|
EDGE_CACHE_TIME_SECONDS: process.env.EDGE_CACHE_TIME_SECONDS,
|
||||||
REDIS_URL: process.env.REDIS_URL,
|
REDIS_URL: process.env.REDIS_URL,
|
||||||
DISCORD_TOKEN: process.env.DISCORD_TOKEN,
|
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) {
|
if (_serverEnv.success === false) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { env } from "@/env/server.mjs";
|
|||||||
type ResponseData = {
|
type ResponseData = {
|
||||||
now: number;
|
now: number;
|
||||||
status: string;
|
status: string;
|
||||||
|
timezone: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
@@ -14,5 +15,6 @@ export default async function handler(
|
|||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
now: new Date().getTime(),
|
now: new Date().getTime(),
|
||||||
status: req.query.key == env.API_KEY ? "Authorized" : "Unauthorized",
|
status: req.query.key == env.API_KEY ? "Authorized" : "Unauthorized",
|
||||||
|
timezone: env.TIMEZONE
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/utils/timezone.ts
Normal file
13
src/utils/timezone.ts
Normal 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);
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES2016",
|
"target": "ES2016",
|
||||||
"lib": ["dom", "dom.iterable", "esnext"],
|
"lib": ["dom", "dom.iterable", "esnext", "es2021.intl"],
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
|||||||
@@ -357,6 +357,11 @@ csstype@^3.0.2:
|
|||||||
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.1.tgz#841b532c45c758ee546a11d5bd7b7b473c8c30b9"
|
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.1.tgz#841b532c45c758ee546a11d5bd7b7b473c8c30b9"
|
||||||
integrity sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==
|
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:
|
date-fns@^2.29.3:
|
||||||
version "2.29.3"
|
version "2.29.3"
|
||||||
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8"
|
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8"
|
||||||
|
|||||||
Reference in New Issue
Block a user