Last active
July 12, 2025 04:31
-
-
Save 5ouma/cfb87d425a19f13682f5ec10e03ad2d6 to your computer and use it in GitHub Desktop.
⏰ Wakeup Standard Time calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Hono } from "jsr:@hono/hono"; | |
import { logger } from "jsr:@hono/hono/logger"; | |
import { vValidator } from "jsr:@hono/valibot-validator"; | |
import { STATUS_CODE } from "jsr:@std/http/status"; | |
import { | |
object, | |
pipe, | |
safeParse, | |
string, | |
transform, | |
} from "jsr:@valibot/valibot"; | |
type Time = { hour: number; minute: number; second: number }; | |
export const dateSchema = pipe(string(), transform((input) => new Date(input))); | |
export const dateObjectSchema = object({ date: dateSchema }); | |
const calcWST = (wakeUpAt: Date): Time => { | |
const diff = new Date().getTime() - wakeUpAt.getTime(); | |
const total = Math.floor(diff / 1000); | |
const hour = Math.floor(total / 3600); | |
const minute = Math.floor((total % 3600) / 60); | |
const second = total % 60; | |
return { hour, minute, second }; | |
}; | |
export default new Hono() | |
.use(logger()) | |
.get("/", (ctx) => { | |
const { date } = ctx.req.query(); | |
const { output, success } = safeParse(dateSchema, date); | |
if (!success) { | |
return ctx.json({ error: "Invalid date format" }, STATUS_CODE.BadRequest); | |
} | |
const time = calcWST(output); | |
return ctx.json(time, STATUS_CODE.OK); | |
}) | |
.post("/", vValidator("json", dateObjectSchema), (ctx) => { | |
const { date } = ctx.req.valid("json"); | |
const time = calcWST(date); | |
return ctx.json(time, STATUS_CODE.OK); | |
}); |
Author
5ouma
commented
Jul 12, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment