mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
feat: backend side of timezone convert, new route created for using luxon to convert timezones
This commit is contained in:
parent
ab601b53fe
commit
2d53cb0017
4 changed files with 69 additions and 2 deletions
45
backend/src/routes/tmzconvert.route.ts
Normal file
45
backend/src/routes/tmzconvert.route.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
import { DateTime } from "luxon";
|
||||
|
||||
interface RequestBody {
|
||||
time: string;
|
||||
fromZone: string;
|
||||
toZone: string;
|
||||
}
|
||||
|
||||
export async function tmzConvert(app: FastifyInstance) {
|
||||
app.post(
|
||||
"/api/tmz-convert",
|
||||
async (
|
||||
request: FastifyRequest<{ Body: RequestBody }>,
|
||||
reply: FastifyReply,
|
||||
) => {
|
||||
const data = request.body;
|
||||
const time = data.time;
|
||||
const fromZone = data.fromZone;
|
||||
const toZone = data.toZone;
|
||||
|
||||
try {
|
||||
if (!time || !fromZone || !toZone) {
|
||||
return reply.status(400).send({ error: "Missing parameters" });
|
||||
}
|
||||
|
||||
const dateTime = DateTime.fromISO(time, { zone: fromZone });
|
||||
|
||||
if (!dateTime.isValid) {
|
||||
return reply.status(400).send({ error: "Invalid date or time zone" });
|
||||
}
|
||||
|
||||
const convertedTime = dateTime.setZone(toZone).toISO();
|
||||
|
||||
return reply
|
||||
.header("Content-Type", "text/plain")
|
||||
.status(200)
|
||||
.send(convertedTime);
|
||||
} catch (error) {
|
||||
console.error("Conversion error:", error);
|
||||
reply.status(500).send({ error: "Error converting time" });
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue