Merge pull request #1675 from Budibase/fix/timezones

Allowing use of timezones with date helper
This commit is contained in:
Michael Drury 2021-06-09 12:29:00 +01:00 committed by GitHub
commit cabea04bbc
3 changed files with 36 additions and 5 deletions

View File

@ -19,8 +19,9 @@ const ADDED_HELPERS = {
date: { date: {
args: ["datetime", "format"], args: ["datetime", "format"],
numArgs: 2, numArgs: 2,
example: '{{date now "DD-MM-YYYY"}} -> 21-01-2021', example: '{{date now "DD-MM-YYYY" "America/New_York" }} -> 21-01-2021',
description: "Format a date using moment.js date formatting.", description:
"Format a date using moment.js date formatting - the timezone is optional and uses the tz database.",
}, },
duration: { duration: {
args: ["time", "durationType"], args: ["time", "durationType"],

View File

@ -3,6 +3,7 @@ dayjs.extend(require("dayjs/plugin/duration"))
dayjs.extend(require("dayjs/plugin/advancedFormat")) dayjs.extend(require("dayjs/plugin/advancedFormat"))
dayjs.extend(require("dayjs/plugin/relativeTime")) dayjs.extend(require("dayjs/plugin/relativeTime"))
dayjs.extend(require("dayjs/plugin/utc")) 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: * 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 // if options is null then it'll get updated here
const config = initialConfig(str, pattern, options) const config = initialConfig(str, pattern, options)
const defaults = { lang: "en", date: new Date(config.str) } 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 // set the language to use
dayjs.locale(opts.lang || opts.language) dayjs.locale(opts.lang || opts.language)
@ -89,7 +91,15 @@ module.exports.date = (str, pattern, options) => {
setLocale(config.str, config.pattern, config.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 === "") { if (config.pattern === "") {
return date.toISOString() return date.toISOString()
} }

View File

@ -1,5 +1,6 @@
const { processString, processObject, isValid } = require("../src/index.cjs") const { processString, processObject, isValid } = require("../src/index.cjs")
const tableJson = require("./examples/table.json") const tableJson = require("./examples/table.json")
const dayjs = require("dayjs")
describe("test the custom helpers we have applied", () => { describe("test the custom helpers we have applied", () => {
it("should be able to use the object helper", async () => { 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 () => { it("should allow use of the date helper", async () => {
const date = new Date(1611577535000) const date = new Date(1611577535000)
const output = await processString("{{ date time 'YYYY-MM-DD' }}", { const output = await processString("{{ date time 'YYYY-MM-DD' }}", {
time: date.toISOString(), time: date.toUTCString(),
}) })
expect(output).toBe("2021-01-25") expect(output).toBe("2021-01-25")
}) })
@ -172,6 +173,25 @@ describe("test the date helpers", () => {
const output = await processString("{{ date now 'DD' }}", {}) const output = await processString("{{ date now 'DD' }}", {})
expect(parseInt(output)).toBe(date.getDate()) 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", () => { describe("test the string helpers", () => {