feat: backend side of timezone convert, new route created for using luxon to convert timezones

This commit is contained in:
theoleuthardt 2025-02-20 22:03:51 +01:00
parent ab601b53fe
commit 2d53cb0017
4 changed files with 69 additions and 2 deletions

View file

@ -15,9 +15,11 @@
"fastify": "^5.2.1",
"fastify-cors": "^6.0.3",
"fastify-multipart": "^5.3.1",
"libreoffice-convert": "^1.6.0"
"libreoffice-convert": "^1.6.0",
"luxon": "^3.5.0"
},
"devDependencies": {
"@types/luxon": "^3.4.2",
"@types/node": "^22.13.4",
"prettier": "3.5.1",
"ts-node": "^10.9.2",
@ -263,6 +265,13 @@
"dev": true,
"license": "MIT"
},
"node_modules/@types/luxon": {
"version": "3.4.2",
"resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz",
"integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/node": {
"version": "22.13.4",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.4.tgz",
@ -682,6 +691,15 @@
"set-cookie-parser": "^2.6.0"
}
},
"node_modules/luxon": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/luxon/-/luxon-3.5.0.tgz",
"integrity": "sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ==",
"license": "MIT",
"engines": {
"node": ">=12"
}
},
"node_modules/make-error": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",

View file

@ -18,9 +18,11 @@
"fastify": "^5.2.1",
"fastify-cors": "^6.0.3",
"fastify-multipart": "^5.3.1",
"libreoffice-convert": "^1.6.0"
"libreoffice-convert": "^1.6.0",
"luxon": "^3.5.0"
},
"devDependencies": {
"@types/luxon": "^3.4.2",
"@types/node": "^22.13.4",
"prettier": "3.5.1",
"ts-node": "^10.9.2",

View file

@ -5,6 +5,7 @@ import { libreConvert } from "./src/routes/libreconvert.route";
import { colorConvert } from "./src/routes/colorconvert.route";
import { passwordGenerate } from "./src/routes/passwordgenerate.route";
import { regexTest } from "./src/routes/regextest.route";
import { tmzConvert } from "./src/routes/tmzconvert.route";
const app = Fastify({ logger: true });
@ -19,6 +20,7 @@ app.register(libreConvert);
app.register(colorConvert);
app.register(passwordGenerate);
app.register(regexTest);
app.register(tmzConvert);
const PORT = process.env.PORT || 4000;
app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => {

View 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" });
}
},
);
}