From 3e08d31d144e5d48e224bfd9142271c3e6c0cc41 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Tue, 9 Feb 2021 14:16:19 +0100 Subject: [PATCH] Removed moment for internal implementation --- package-lock.json | 5 ----- package.json | 1 - spec/index.ts | 20 ++++++++------------ src/utils.ts | 35 ++++++++++++++++++++++++++++++----- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7ad48ed..b2d90c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -179,11 +179,6 @@ "brace-expansion": "^1.1.7" } }, - "moment": { - "version": "2.29.1", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", - "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" - }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/package.json b/package.json index 96026e0..14529d6 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "dependencies": { "debug": "^4.3.1", "joi": "^17.3.0", - "moment": "^2.29.1", "node-forge": "^0.10.0", "tslib": "^2.1.0", "yazl": "^2.5.1" diff --git a/spec/index.ts b/spec/index.ts index 3c952b7..6744fa5 100644 --- a/spec/index.ts +++ b/spec/index.ts @@ -76,12 +76,11 @@ describe("Passkit-generator", function () { expect(pass.props["expirationDate"]).toBe(undefined); }); - it("Will set expiration with a Date as argument", () => { - pass.expiration(new Date(2020, 5, 1, 0, 0, 0)); - // this is made to avoid problems with winter and summer time: - // we focus only on the date and time for the tests. - let noTimeZoneDateTime = pass.props["expirationDate"].split("+")[0]; - expect(noTimeZoneDateTime).toBe("2020-06-01T00:00:00"); + it("expects a Date object as the only argument", () => { + pass.expiration(new Date(2020, 6, 1, 0, 0, 0, 0)); + // Month starts from 0 in Date Object when used this way, therefore + // we expect one month more + expect(pass.props["expirationDate"]).toBe("2020-07-01T00:00:00Z"); }); it("An invalid date, will not apply changes", () => { @@ -96,12 +95,9 @@ describe("Passkit-generator", function () { }); describe("relevantDate()", () => { - it("A date object will apply changes", () => { + it("expects a Date object as the only argument", () => { pass.relevantDate(new Date("10-04-2021")); - // this is made to avoid problems with winter and summer time: - // we focus only on the date and time for the tests. - let noTimeZoneDateTime = pass.props["relevantDate"].split("+")[0]; - expect(noTimeZoneDateTime).toBe("2021-10-04T00:00:00"); + expect(pass.props["relevantDate"]).toBe("2021-10-04T00:00:00Z"); }); }); @@ -264,8 +260,8 @@ describe("Passkit-generator", function () { }); it("Will ignore non-Barcodes schema compliant objects", () => { - // @ts-expect-error pass.barcodes( + // @ts-expect-error 5, 10, 15, diff --git a/src/utils.ts b/src/utils.ts index 19912e9..7e4a77d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,3 @@ -import moment from "moment"; import { EOL } from "os"; import { PartitionedBundle, BundleUnit } from "./schema"; import { sep } from "path"; @@ -41,13 +40,39 @@ export function dateToW3CString(date: Date) { return ""; } - const parsedDate = moment(date).format(); - - if (parsedDate === "Invalid date") { + // if it is NaN, it is "Invalid Date" + if (isNaN(Number(date))) { return undefined; } - return parsedDate; + const paddedMonth = padMeTwo(date.getMonth() + 1); + const paddedDay = padMeTwo(date.getDate()); + const paddedHour = padMeTwo(date.getHours()); + const paddedMinutes = padMeTwo(date.getMinutes()); + const paddedSeconds = padMeTwo(date.getSeconds()); + + /** + * Date.prototype.getTimezoneOffset returns the timezone UTC offset in + * minutes of the local machine. + * + * That value should then be used to calculate the effective timezone as + * string, but still that would be related to the machine and not to the + * specified date. + * + * For this reason we are completing date with "Z" TimeZoneDesignator (TZD) + * to say it to use local timezone. + * + * In the future we might think to integrate another parameter to represent + * a custom timezone. + * + * @see https://www.w3.org/TR/NOTE-datetime + */ + + return `${date.getFullYear()}-${paddedMonth}-${paddedDay}T${paddedHour}:${paddedMinutes}:${paddedSeconds}Z`; +} + +function padMeTwo(original: string | number) { + return String(original).padStart(2, "0"); } /**