Browse Source

Updating date helper to use UTC.

pull/1574/head
mike12345567 5 years ago
parent
commit
1470e601aa
  1. 7
      packages/string-templates/src/helpers/date.js
  2. 9
      packages/string-templates/test/helpers.spec.js

7
packages/string-templates/src/helpers/date.js

@ -2,6 +2,7 @@ const dayjs = require("dayjs")
dayjs.extend(require("dayjs/plugin/duration"))
dayjs.extend(require("dayjs/plugin/advancedFormat"))
dayjs.extend(require("dayjs/plugin/relativeTime"))
dayjs.extend(require("dayjs/plugin/utc"))
/**
* This file was largely taken from the helper-date package - we did this for two reasons:
@ -88,7 +89,11 @@ module.exports.date = (str, pattern, options) => {
setLocale(config.str, config.pattern, config.options)
return dayjs(new Date(config.str)).format(config.pattern)
const date = dayjs(new Date(config.str)).utc()
if (config.pattern === "") {
return date.toISOString()
}
return date.format(config.pattern)
}
module.exports.duration = (str, pattern, format) => {

9
packages/string-templates/test/helpers.spec.js

@ -356,6 +356,15 @@ describe("Cover a few complex use cases", () => {
expect(validity).toBe(true)
})
it("should test a case of attempting to get a UTC ISO back out after processing", async () => {
const date = new Date()
const input = `{{ date (subtract (date dateThing "x") 300000) "" }}`
const output = await processString(input, {
dateThing: date.toISOString(),
})
expect(output).toEqual(new Date(date.getTime() - 300000).toISOString())
})
it("test a very complex duration output", async () => {
const currentTime = new Date(1612432082000).toISOString(),
eventTime = new Date(1612432071000).toISOString()

Loading…
Cancel
Save