Skip to content

Commit

Permalink
feat: stick date modifiers to UTC TZ
Browse files Browse the repository at this point in the history
  • Loading branch information
vvagaytsev committed May 15, 2024
1 parent 986d9b3 commit 890adc0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
27 changes: 15 additions & 12 deletions core/src/template-string/date-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ const validModifyDateTimeUnits = ["years", "months", "days", "hours", "minutes",
type ModifyDateTimeUnit = (typeof validModifyDateTimeUnits)[number]
// This is still type-safe because every entry of ModifyDateTimeUnit must be declared in the index below.
const modifyDateFunctions: { [k in ModifyDateTimeUnit]: (date: Date, timeUnits: number) => void } = {
years: (date, timeUnits) => date.setFullYear(timeUnits),
months: (date, timeUnits) => date.setMonth(timeUnits),
days: (date, timeUnits) => date.setDate(timeUnits),
hours: (date, timeUnits) => date.setHours(timeUnits),
minutes: (date, timeUnits) => date.setMinutes(timeUnits),
seconds: (date, timeUnits) => date.setSeconds(timeUnits),
milliseconds: (date, timeUnits) => date.setMilliseconds(timeUnits),
years: (date, timeUnits) => date.setUTCFullYear(timeUnits),
months: (date, timeUnits) => date.setUTCMonth(timeUnits),
days: (date, timeUnits) => date.setUTCDate(timeUnits),
hours: (date, timeUnits) => date.setUTCHours(timeUnits),
minutes: (date, timeUnits) => date.setUTCMinutes(timeUnits),
seconds: (date, timeUnits) => date.setUTCSeconds(timeUnits),
milliseconds: (date, timeUnits) => date.setUTCMilliseconds(timeUnits),
} as const

const timeZoneComment =
"The input date is always converted to the UTC time zone before the modification. If no explicit timezone is specified on the input date, then the system default one will be used. The output date is always returned in the UTC time zone too."

export const dateHelperFunctionSpecs: TemplateHelperFunction[] = [
{
name: "formatDate",
Expand All @@ -55,8 +58,8 @@ export const dateHelperFunctionSpecs: TemplateHelperFunction[] = [
},
},
{
name: "shiftDate",
description: "Shifts the date by the specified amount of time units.",
name: "shiftDateUtc",
description: `Shifts the date by the specified amount of time units. ${timeZoneComment}`,
arguments: {
date: joi.string().required().description("The date to shift."),
amount: joi.number().required().description("The amount of time units to shift the date by."),
Expand Down Expand Up @@ -87,8 +90,8 @@ export const dateHelperFunctionSpecs: TemplateHelperFunction[] = [
},
},
{
name: "modifyDate",
description: "Modifies the date by setting the specified amount of time units.",
name: "modifyDateUtc",
description: `Modifies the date by setting the specified amount of time units. ${timeZoneComment}`,
arguments: {
date: joi.string().required().description("The date to modify."),
amount: joi.number().required().description("The amount of time units to set."),
Expand All @@ -103,7 +106,7 @@ export const dateHelperFunctionSpecs: TemplateHelperFunction[] = [
{ input: ["2021-01-01T00:00:00.234Z", 345, "milliseconds"], output: "2021-01-01T00:00:00.345Z" },
{ input: ["2021-01-01T00:00:05Z", 30, "seconds"], output: "2021-01-01T00:00:30.000Z" },
{ input: ["2021-01-01T00:01:00Z", 15, "minutes"], output: "2021-01-01T00:15:00.000Z" },
{ input: ["2021-01-01T12:00:00Z", 11, "hours"], output: "2021-01-01T11:00:00.000Z", skipTest: true },
{ input: ["2021-01-01T12:00:00Z", 11, "hours"], output: "2021-01-01T11:00:00.000Z" },
{ input: ["2021-01-31T00:00:00Z", 1, "days"], output: "2021-01-01T00:00:00.000Z" },
{ input: ["2021-03-01T00:00:00Z", 0, "months"], output: "2021-01-01T00:00:00.000Z" }, // 0 (Jan) - 11 (Dec)
{ input: ["2021-01-01T00:00:00Z", 2024, "years"], output: "2024-01-01T00:00:00.000Z" },
Expand Down
50 changes: 25 additions & 25 deletions docs/reference/template-strings/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,21 @@ Examples:

* `${lower("Some String")}` -> `"some string"`

## modifyDate
## modifyDateUtc

Modifies the date by setting the specified amount of time units.
Modifies the date by setting the specified amount of time units. The input date is always converted to the UTC time zone before the modification. If no explicit timezone is specified on the input date, then the system default one will be used. The output date is always returned in the UTC time zone too.

Usage: `modifyDate(date, amount, unit)`
Usage: `modifyDateUtc(date, amount, unit)`

Examples:

* `${modifyDate("2021-01-01T00:00:00.234Z", 345, "milliseconds")}` -> `"2021-01-01T00:00:00.345Z"`
* `${modifyDate("2021-01-01T00:00:05Z", 30, "seconds")}` -> `"2021-01-01T00:00:30.000Z"`
* `${modifyDate("2021-01-01T00:01:00Z", 15, "minutes")}` -> `"2021-01-01T00:15:00.000Z"`
* `${modifyDate("2021-01-01T12:00:00Z", 11, "hours")}` -> `"2021-01-01T11:00:00.000Z"`
* `${modifyDate("2021-01-31T00:00:00Z", 1, "days")}` -> `"2021-01-01T00:00:00.000Z"`
* `${modifyDate("2021-03-01T00:00:00Z", 0, "months")}` -> `"2021-01-01T00:00:00.000Z"`
* `${modifyDate("2021-01-01T00:00:00Z", 2024, "years")}` -> `"2024-01-01T00:00:00.000Z"`
* `${modifyDateUtc("2021-01-01T00:00:00.234Z", 345, "milliseconds")}` -> `"2021-01-01T00:00:00.345Z"`
* `${modifyDateUtc("2021-01-01T00:00:05Z", 30, "seconds")}` -> `"2021-01-01T00:00:30.000Z"`
* `${modifyDateUtc("2021-01-01T00:01:00Z", 15, "minutes")}` -> `"2021-01-01T00:15:00.000Z"`
* `${modifyDateUtc("2021-01-01T12:00:00Z", 11, "hours")}` -> `"2021-01-01T11:00:00.000Z"`
* `${modifyDateUtc("2021-01-31T00:00:00Z", 1, "days")}` -> `"2021-01-01T00:00:00.000Z"`
* `${modifyDateUtc("2021-03-01T00:00:00Z", 0, "months")}` -> `"2021-01-01T00:00:00.000Z"`
* `${modifyDateUtc("2021-01-01T00:00:00Z", 2024, "years")}` -> `"2024-01-01T00:00:00.000Z"`

## replace

Expand All @@ -182,26 +182,26 @@ Examples:

* `${sha256("Some String")}` -> `"7f0fd64653ba0bb1a579ced2b6bf375e916cc60662109ee0c0b24f0a750c3a6c"`

## shiftDate
## shiftDateUtc

Shifts the date by the specified amount of time units.
Shifts the date by the specified amount of time units. The input date is always converted to the UTC time zone before the modification. If no explicit timezone is specified on the input date, then the system default one will be used. The output date is always returned in the UTC time zone too.

Usage: `shiftDate(date, amount, unit)`
Usage: `shiftDateUtc(date, amount, unit)`

Examples:

* `${shiftDate("2021-01-01T00:00:00Z", 1, "seconds")}` -> `"2021-01-01T00:00:01.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", -1, "seconds")}` -> `"2020-12-31T23:59:59.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", 1, "minutes")}` -> `"2021-01-01T00:01:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", -1, "minutes")}` -> `"2020-12-31T23:59:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", 1, "hours")}` -> `"2021-01-01T01:00:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", -1, "hours")}` -> `"2020-12-31T23:00:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", 1, "days")}` -> `"2021-01-02T00:00:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", -1, "days")}` -> `"2020-12-31T00:00:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", 1, "months")}` -> `"2021-02-01T00:00:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", -1, "months")}` -> `"2020-12-01T00:00:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", 1, "years")}` -> `"2022-01-01T00:00:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", -1, "years")}` -> `"2020-01-01T00:00:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", 1, "seconds")}` -> `"2021-01-01T00:00:01.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", -1, "seconds")}` -> `"2020-12-31T23:59:59.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", 1, "minutes")}` -> `"2021-01-01T00:01:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", -1, "minutes")}` -> `"2020-12-31T23:59:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", 1, "hours")}` -> `"2021-01-01T01:00:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", -1, "hours")}` -> `"2020-12-31T23:00:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", 1, "days")}` -> `"2021-01-02T00:00:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", -1, "days")}` -> `"2020-12-31T00:00:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", 1, "months")}` -> `"2021-02-01T00:00:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", -1, "months")}` -> `"2020-12-01T00:00:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", 1, "years")}` -> `"2022-01-01T00:00:00.000Z"`
* `${shiftDateUtc("2021-01-01T00:00:00Z", -1, "years")}` -> `"2020-01-01T00:00:00.000Z"`

## slice

Expand Down

0 comments on commit 890adc0

Please sign in to comment.