Skip to content

Commit

Permalink
feat: timezone name for opts.timezoneOffset and date argument, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Nov 29, 2022
1 parent b7ab357 commit 89c6c76
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/filters/date.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime } from '../util'
import { FilterImpl } from '../template'

export function date (this: FilterImpl, v: string | Date, format: string, timeZoneOffset?: number) {
export function date (this: FilterImpl, v: string | Date, format: string, timezoneOffset?: number | string) {
const opts = this.context.opts
let date: LiquidDate
v = toValue(v)
Expand All @@ -22,14 +22,24 @@ export function date (this: FilterImpl, v: string | Date, format: string, timeZo
date = v
}
if (!isValidDate(date)) return v
if (timeZoneOffset !== undefined) {
date = new TimezoneDate(date, timeZoneOffset)
if (timezoneOffset !== undefined) {
date = new TimezoneDate(date, parseTimezoneOffset(date, timezoneOffset))
} else if (opts.timezoneOffset !== undefined) {
date = new TimezoneDate(date, opts.timezoneOffset!)
date = new TimezoneDate(date, parseTimezoneOffset(date, opts.timezoneOffset))
}
return strftime(date, format)
}

function isValidDate (date: any): date is Date {
return (date instanceof Date || date instanceof TimezoneDate) && !isNaN(date.getTime())
}

/**
* need pass in a `date` because offset is dependent on whether DST is active
*/
function parseTimezoneOffset (date: Date, timeZone: string | number) {
if (isNumber(timeZone)) return timeZone
const utcDate = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' }))
const tzDate = new Date(date.toLocaleString('en-US', { timeZone }))
return (utcDate.getTime() - tzDate.getTime()) / 6e4
}
4 changes: 2 additions & 2 deletions src/liquid-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export interface LiquidOptions {
ownPropertyOnly?: boolean;
/** Modifies the behavior of `strictVariables`. If set, a single undefined variable will *not* cause an exception in the context of the `if`/`elsif`/`unless` tag and the `default` filter. Instead, it will evaluate to `false` and `null`, respectively. Irrelevant if `strictVariables` is not set. Defaults to `false`. **/
lenientIf?: boolean;
/** JavaScript timezoneOffset for `date` filter, default to local time. That means if you're in Australia (UTC+10), it'll default to -600 */
timezoneOffset?: number;
/** JavaScript timezone name or timezoneOffset for `date` filter, default to local time. That means if you're in Australia (UTC+10), it'll default to `-600` or `Australia/Lindeman` */
timezoneOffset?: number | string;
/** Strip blank characters (including ` `, `\t`, and `\r`) from the right of tags (`{% %}`) until `\n` (inclusive). Defaults to `false`. */
trimTagRight?: boolean;
/** Similar to `trimTagRight`, whereas the `\n` is exclusive. Defaults to `false`. See Whitespace Control for details. */
Expand Down
15 changes: 14 additions & 1 deletion test/integration/filters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,29 @@ describe('filters/date', function () {
it('should support timezone offset argument', function () {
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", 360}}', '1990-12-31T17:00:00')
})
it('should support timezone name argument', function () {
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "Asia/Colombo" }}', '1991-01-01T04:30:00')
})
it('should support timezone name argument when DST is not active', function () {
return test('{{ "2021-01-01T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "America/New_York" }}', '2021-01-01T18:00:00')
})
it('should support timezone name argument when DST is active', function () {
return test('{{ "2021-06-01T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "America/New_York" }}', '2021-06-01T19:00:00')
})
it('should offset date literal with timezone 00:00 specified', function () {
return test('{{ "1990-12-31T23:00:00+00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T17:00:00', undefined, opts)
})
it('should offset date literal with timezone -01:00 specified', function () {
return test('{{ "1990-12-31T23:00:00-01:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T18:00:00', undefined, opts)
})
it('should offset date from scope', function () {
it('should offset date from scope (timezone offset)', function () {
const scope = { date: new Date('1990-12-31T23:00:00Z') }
return test('{{ date | date: "%Y-%m-%dT%H:%M:%S"}}', scope, '1990-12-31T17:00:00', opts)
})
it('should offset date from scope (timezone name)', function () {
const scope = { date: new Date('1990-12-31T23:00:00Z') }
return test('{{ date | date: "%Y-%m-%dT%H:%M:%S"}}', scope, '1990-12-31T17:00:00', { timezoneOffset: 'America/Merida' })
})
it('should reflect timezoneOffset', function () {
const scope = { date: new Date('1990-12-31T23:00:00Z') }
return test('{{ date | date: "%z"}}', scope, '-0600', opts)
Expand Down

0 comments on commit 89c6c76

Please sign in to comment.