From 3ffd340450e70475b166c7a273b4ed0d1b759333 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 9 Jun 2021 11:44:24 +0100 Subject: [PATCH 1/4] Updating date helper to allow timezone capabilites. --- packages/string-templates/src/helpers/date.js | 14 ++++++++++++-- .../string-templates/test/helpers.spec.js | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/string-templates/src/helpers/date.js b/packages/string-templates/src/helpers/date.js index ce845eee3f..440657b733 100644 --- a/packages/string-templates/src/helpers/date.js +++ b/packages/string-templates/src/helpers/date.js @@ -3,6 +3,7 @@ dayjs.extend(require("dayjs/plugin/duration")) dayjs.extend(require("dayjs/plugin/advancedFormat")) dayjs.extend(require("dayjs/plugin/relativeTime")) dayjs.extend(require("dayjs/plugin/utc")) +dayjs.extend(require("dayjs/plugin/timezone")) /** * This file was largely taken from the helper-date package - we did this for two reasons: @@ -72,7 +73,8 @@ function setLocale(str, pattern, options) { // if options is null then it'll get updated here const config = initialConfig(str, pattern, options) const defaults = { lang: "en", date: new Date(config.str) } - const opts = getContext(this, defaults, config.options) + // for now don't allow this to be configurable, don't pass in options + const opts = getContext(this, defaults, {}) // set the language to use dayjs.locale(opts.lang || opts.language) @@ -89,7 +91,15 @@ module.exports.date = (str, pattern, options) => { setLocale(config.str, config.pattern, config.options) - const date = dayjs(new Date(config.str)).utc() + let date = dayjs(new Date(config.str)) + if (typeof config.options === "string") { + date = + config.options.toLowerCase() === "utc" + ? date.utc() + : date.tz(config.options) + } else { + date = date.tz(dayjs.tz.guess()) + } if (config.pattern === "") { return date.toISOString() } diff --git a/packages/string-templates/test/helpers.spec.js b/packages/string-templates/test/helpers.spec.js index 6be8e2ed0e..f0a71b9ba4 100644 --- a/packages/string-templates/test/helpers.spec.js +++ b/packages/string-templates/test/helpers.spec.js @@ -1,5 +1,6 @@ const { processString, processObject, isValid } = require("../src/index.cjs") const tableJson = require("./examples/table.json") +const dayjs = require("dayjs") describe("test the custom helpers we have applied", () => { it("should be able to use the object helper", async () => { @@ -172,6 +173,24 @@ describe("test the date helpers", () => { const output = await processString("{{ date now 'DD' }}", {}) expect(parseInt(output)).toBe(date.getDate()) }) + + it("should test the timezone capabilities", async () => { + const date = new Date(1611577535000) + const output = await processString("{{ date time 'HH-mm-ss Z' 'America/New_York' }}", { + time: date.toISOString(), + }) + expect(output).toBe("07-25-35 -04:00") + }) + + it("should guess the users timezone when not specified", async () => { + const date = new Date() + const output = await processString("{{ date time 'Z' }}", { + time: date.toISOString() + }) + const timezone = dayjs.tz.guess() + const offset = new dayjs(date).tz(timezone).format("Z") + expect(output).toBe(offset) + }) }) describe("test the string helpers", () => { From a98cff0446005220d85cd0fc05780c5a9dd50115 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 9 Jun 2021 11:50:00 +0100 Subject: [PATCH 2/4] Adding info about timezone to date helper info. --- packages/string-templates/scripts/gen-collection-info.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/string-templates/scripts/gen-collection-info.js b/packages/string-templates/scripts/gen-collection-info.js index 1977f95d22..7baee1e797 100644 --- a/packages/string-templates/scripts/gen-collection-info.js +++ b/packages/string-templates/scripts/gen-collection-info.js @@ -19,8 +19,8 @@ const ADDED_HELPERS = { date: { args: ["datetime", "format"], numArgs: 2, - example: '{{date now "DD-MM-YYYY"}} -> 21-01-2021', - description: "Format a date using moment.js date formatting.", + example: '{{date now "DD-MM-YYYY" "America/New_York" }} -> 21-01-2021', + description: "Format a date using moment.js date formatting - the timezone is optional and uses the tz database.", }, duration: { args: ["time", "durationType"], From 310ed3aefc0fe956e9ba84c2bef087e04e054fe9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 9 Jun 2021 11:52:26 +0100 Subject: [PATCH 3/4] Formatting./ --- packages/string-templates/scripts/gen-collection-info.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/string-templates/scripts/gen-collection-info.js b/packages/string-templates/scripts/gen-collection-info.js index 7baee1e797..fcd3cb4923 100644 --- a/packages/string-templates/scripts/gen-collection-info.js +++ b/packages/string-templates/scripts/gen-collection-info.js @@ -20,7 +20,8 @@ const ADDED_HELPERS = { args: ["datetime", "format"], numArgs: 2, example: '{{date now "DD-MM-YYYY" "America/New_York" }} -> 21-01-2021', - description: "Format a date using moment.js date formatting - the timezone is optional and uses the tz database.", + description: + "Format a date using moment.js date formatting - the timezone is optional and uses the tz database.", }, duration: { args: ["time", "durationType"], From f9ba157f97141379315c53a62371363272abcf6a Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 9 Jun 2021 12:11:40 +0100 Subject: [PATCH 4/4] Updating test. --- packages/string-templates/test/helpers.spec.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/string-templates/test/helpers.spec.js b/packages/string-templates/test/helpers.spec.js index f0a71b9ba4..d460708798 100644 --- a/packages/string-templates/test/helpers.spec.js +++ b/packages/string-templates/test/helpers.spec.js @@ -163,7 +163,7 @@ describe("test the date helpers", () => { it("should allow use of the date helper", async () => { const date = new Date(1611577535000) const output = await processString("{{ date time 'YYYY-MM-DD' }}", { - time: date.toISOString(), + time: date.toUTCString(), }) expect(output).toBe("2021-01-25") }) @@ -177,15 +177,16 @@ describe("test the date helpers", () => { it("should test the timezone capabilities", async () => { const date = new Date(1611577535000) const output = await processString("{{ date time 'HH-mm-ss Z' 'America/New_York' }}", { - time: date.toISOString(), + time: date.toUTCString(), }) - expect(output).toBe("07-25-35 -04:00") + const formatted = new dayjs(date).tz("America/New_York").format("HH-mm-ss Z") + expect(output).toBe(formatted) }) it("should guess the users timezone when not specified", async () => { const date = new Date() const output = await processString("{{ date time 'Z' }}", { - time: date.toISOString() + time: date.toUTCString() }) const timezone = dayjs.tz.guess() const offset = new dayjs(date).tz(timezone).format("Z")