From 4b0478fe62ddcaab7d374b2754d43a0d1c538f85 Mon Sep 17 00:00:00 2001 From: Arno Kaimbacher Date: Mon, 28 Nov 2022 17:58:00 +0100 Subject: [PATCH] - parse any date automatically with timezone 'Europe/Vienna' --- src/config/db.config.ts | 2 +- src/controllers/oai.controller.ts | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/config/db.config.ts b/src/config/db.config.ts index 634f392..4a12cdc 100644 --- a/src/config/db.config.ts +++ b/src/config/db.config.ts @@ -40,7 +40,7 @@ const sequelizeConnection = new Sequelize(dbName, dbUser, dbPassword, { idle: 10000, }, logging: false, - timezone: "+02:00", //for writing to database + timezone: "Europe/Vienna", //for writing to database }); // sequelizeConnection.addModels([Dataset, Abstract]); // sequelizeConnection.addModels([DocumentXmlCache]); diff --git a/src/controllers/oai.controller.ts b/src/controllers/oai.controller.ts index 471c729..be395a2 100644 --- a/src/controllers/oai.controller.ts +++ b/src/controllers/oai.controller.ts @@ -17,6 +17,8 @@ import XmlModel from "../library/XmlModel"; import Configuration from "../library/oai/OaiConfiguration"; import ResumptionToken from "../library/oai/ResumptionToken"; import TokenWorker from "../library/oai/TokenWorker"; +import utc from "dayjs/plugin/utc"; +import timezone from "dayjs/plugin/timezone"; interface XslTParameter { [key: string]: any; @@ -53,6 +55,8 @@ export class OaiController { constructor() { this.proc = readFileSync(__dirname + "/datasetxml2oai.sef.json"); this.configuration = new Configuration(); + dayjs.extend(utc); + dayjs.extend(timezone); } @Post("") @@ -81,7 +85,7 @@ export class OaiController { let earliestDateFromDb; const firstPublishedDataset: Dataset | null = await Dataset.earliestPublicationDate(); firstPublishedDataset != null && - (earliestDateFromDb = dayjs(firstPublishedDataset.server_date_published).format("YYYY-MM-DDThh:mm:ss[Z]")); + (earliestDateFromDb = dayjs(firstPublishedDataset.server_date_published).format("YYYY-MM-DDTHH:mm:ss[Z]")); this.xsltParameter["earliestDatestamp"] = earliestDateFromDb; const oaiRequest: OaiParameter = request.query; @@ -157,7 +161,7 @@ export class OaiController { // Set response time const now: Dayjs = dayjs(); - this.xsltParameter["responseDate"] = now.format("YYYY-MM-DDThh:mm:ss[Z]"); + this.xsltParameter["responseDate"] = now.format("YYYY-MM-DDTHH:mm:ss[Z]"); this.xsltParameter["unixTimestamp"] = now.unix(); // set OAI base url @@ -446,15 +450,16 @@ export class OaiController { }; includeArray.push(icncludeFilter); } - } + } + // const timeZone = "Europe/Vienna"; // Canonical time zone name // &from=2020-09-03&until2020-09-03 // &from=2020-09-11&until=2021-05-11 if ("from" in oaiRequest && "until" in oaiRequest) { const from = oaiRequest["from"] as string; - let fromDate = dayjs(from); + let fromDate = dayjs.tz(from, "Europe/Vienna"); //.tz(timeZone); const until = oaiRequest["until"] as string; - let untilDate = dayjs(until); + let untilDate = dayjs.tz(until, "Europe/Vienna"); //.tz(timeZone); if (!fromDate.isValid() || !untilDate.isValid()) { throw new OaiModelException( StatusCodes.INTERNAL_SERVER_ERROR, @@ -473,18 +478,25 @@ export class OaiController { fromDate.hour() == 0 && (fromDate = fromDate.startOf("day")); untilDate.hour() == 0 && (untilDate = untilDate.endOf("day")); + // format("YYYY-MM-DDTHH:mm:ss[Z]")); + andArray.push({ server_date_published: { - // [Sequelize.Op.between]: [fromDate, untilDate] + // [Sequelize.Op.between]: [fromDate.format("YYYY-MM-DD HH:mm:ss"), untilDate.format("YYYY-MM-DD HH:mm:ss")] [Sequelize.Op.and]: { [Sequelize.Op.gte]: fromDate.format("YYYY-MM-DD HH:mm:ss"), [Sequelize.Op.lte]: untilDate.format("YYYY-MM-DD HH:mm:ss"), }, }, }); + // andArray.push({ + // server_date_published: { + // [Sequelize.Op.lte]: untilDate.format("YYYY-MM-DD HH:mm:ss"), + // }, + // }); } else if ("from" in oaiRequest && !("until" in oaiRequest)) { const from = oaiRequest["from"] as string; - let fromDate = dayjs(from); + let fromDate = dayjs.tz(from, "Europe/Vienna") if (!fromDate.isValid()) { throw new OaiModelException( StatusCodes.INTERNAL_SERVER_ERROR, @@ -511,7 +523,7 @@ export class OaiController { } } else if (!("from" in oaiRequest) && "until" in oaiRequest) { const until = oaiRequest["until"] as string; - let untilDate = dayjs(until); + let untilDate = dayjs.tz(until, "Europe/Vienna"); //.tz(timeZone); if (!untilDate.isValid()) { throw new OaiModelException( StatusCodes.INTERNAL_SERVER_ERROR,