Skip to content

Instantly share code, notes, and snippets.

@5ouma
Last active July 12, 2025 04:31
Show Gist options
  • Save 5ouma/cfb87d425a19f13682f5ec10e03ad2d6 to your computer and use it in GitHub Desktop.
Save 5ouma/cfb87d425a19f13682f5ec10e03ad2d6 to your computer and use it in GitHub Desktop.
⏰ Wakeup Standard Time calculator
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);
});
@5ouma
Copy link
Author

5ouma commented Jul 12, 2025

deno serve -N https://gist.githubusercontent.com/5ouma/cfb87d425a19f13682f5ec10e03ad2d6/raw/wst.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment