diff --git a/packages/string-templates/scripts/gen-collection-info.js b/packages/string-templates/scripts/gen-collection-info.js index 1977f95d22..fcd3cb4923 100644 --- a/packages/string-templates/scripts/gen-collection-info.js +++ b/packages/string-templates/scripts/gen-collection-info.js @@ -19,8 +19,9 @@ 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"], 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..d460708798 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 () => { @@ -162,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") }) @@ -172,6 +173,25 @@ 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.toUTCString(), + }) + 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.toUTCString() + }) + const timezone = dayjs.tz.guess() + const offset = new dayjs(date).tz(timezone).format("Z") + expect(output).toBe(offset) + }) }) describe("test the string helpers", () => {