From 88fc6f67f11e6f10f2448a76a234eb061c492651 Mon Sep 17 00:00:00 2001 From: Anders Gustafsson <34234789+anders-photowall@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:08:52 +0200 Subject: [PATCH] fix timezone for publish date mutation (#149) --- Dockerfile | 2 +- Dockerfile-dev | 2 +- src/datasources/product-api.ts | 21 ++++++++++++--------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index ed23ba9..aed1b0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,4 +12,4 @@ ENV NEW_RELIC_NO_CONFIG_FILE=true # Installs npm dependencies on container RUN npm ci # Command container will actually run when called -CMD ["npx", "nodemon", "-w", "src", "--ext", "ts", "--exec", "ts-node", "src/index.ts"] +CMD ["npx", "nodemon", "-w", "src", "--ext", "ts", "--exec", "TZ=Europe/Paris", "ts-node", "src/index.ts"] diff --git a/Dockerfile-dev b/Dockerfile-dev index 6d33454..82f8bc8 100644 --- a/Dockerfile-dev +++ b/Dockerfile-dev @@ -9,4 +9,4 @@ WORKDIR /app ENV NODE_ENV=development ENV NEW_RELIC_NO_CONFIG_FILE=true # Command container will actually run when called -CMD ["npx", "nodemon", "-w", "src", "--ext", "ts", "--exec", "ts-node", "src/index.ts"] +CMD ["npx", "nodemon", "-w", "src", "--ext", "ts", "--exec", "TZ=Europe/Paris", "ts-node", "src/index.ts"] diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index 2f86775..e6179e1 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -537,15 +537,18 @@ export class ProductAPI extends BaseSQLDataSource { promises.push(promise); } - const pd = info.publishingDate; - const publishingDate = new Date( - pd.getFullYear(), - pd.getMonth(), - pd.getDate(), - 0, - 0, - 0, - ); + const publishingDate = info.publishingDate; + // Some notes about timezone here: + // If client sets YYYY-MM-10T00:00:00.000+02 (which admin does) the timestamp + // here will be YYYY-MM-09T22:00:00.000Z since JS internal Dates are always in UTC and will be + // sent like the string above from client to graphql server. + // Some functions on the Date object however is local timezone dependent. setHours is one of them. + // So as long as we correctly set timezone for node to be Europe/Paris to match + // database server the publishDate above (in UTC) can be YYYY-MM-09T22:00:00.000Z and setHours will not (incorrectly) + // set it to YYYY-MM-09T00:00:00.000Z but but instead keep it as YYYY-MM-09T22:00:00.000Z since that is hour, minute + // second 0 of YYYY-MM-10 with the local timezone. + publishingDate.setHours(0, 0, 0); + promises.push( this.knex .table('product-products')