mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: timezone name for opts.timezoneOffset and date argument, fixes #553
This commit is contained in:
+14
-4
@@ -1,7 +1,7 @@
|
|||||||
import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime } from '../util'
|
import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime } from '../util'
|
||||||
import { FilterImpl } from '../template'
|
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
|
const opts = this.context.opts
|
||||||
let date: LiquidDate
|
let date: LiquidDate
|
||||||
v = toValue(v)
|
v = toValue(v)
|
||||||
@@ -22,10 +22,10 @@ export function date (this: FilterImpl, v: string | Date, format: string, timeZo
|
|||||||
date = v
|
date = v
|
||||||
}
|
}
|
||||||
if (!isValidDate(date)) return v
|
if (!isValidDate(date)) return v
|
||||||
if (timeZoneOffset !== undefined) {
|
if (timezoneOffset !== undefined) {
|
||||||
date = new TimezoneDate(date, timeZoneOffset)
|
date = new TimezoneDate(date, parseTimezoneOffset(date, timezoneOffset))
|
||||||
} else if (opts.timezoneOffset !== undefined) {
|
} else if (opts.timezoneOffset !== undefined) {
|
||||||
date = new TimezoneDate(date, opts.timezoneOffset!)
|
date = new TimezoneDate(date, parseTimezoneOffset(date, opts.timezoneOffset))
|
||||||
}
|
}
|
||||||
return strftime(date, format)
|
return strftime(date, format)
|
||||||
}
|
}
|
||||||
@@ -33,3 +33,13 @@ export function date (this: FilterImpl, v: string | Date, format: string, timeZo
|
|||||||
function isValidDate (date: any): date is Date {
|
function isValidDate (date: any): date is Date {
|
||||||
return (date instanceof Date || date instanceof TimezoneDate) && !isNaN(date.getTime())
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ export interface LiquidOptions {
|
|||||||
ownPropertyOnly?: boolean;
|
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`. **/
|
/** 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;
|
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 */
|
/** 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;
|
timezoneOffset?: number | string;
|
||||||
/** Strip blank characters (including ` `, `\t`, and `\r`) from the right of tags (`{% %}`) until `\n` (inclusive). Defaults to `false`. */
|
/** Strip blank characters (including ` `, `\t`, and `\r`) from the right of tags (`{% %}`) until `\n` (inclusive). Defaults to `false`. */
|
||||||
trimTagRight?: boolean;
|
trimTagRight?: boolean;
|
||||||
/** Similar to `trimTagRight`, whereas the `\n` is exclusive. Defaults to `false`. See Whitespace Control for details. */
|
/** Similar to `trimTagRight`, whereas the `\n` is exclusive. Defaults to `false`. See Whitespace Control for details. */
|
||||||
|
|||||||
@@ -64,16 +64,29 @@ describe('filters/date', function () {
|
|||||||
it('should support timezone offset argument', 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')
|
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 () {
|
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)
|
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 () {
|
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)
|
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') }
|
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)
|
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 () {
|
it('should reflect timezoneOffset', function () {
|
||||||
const scope = { date: new Date('1990-12-31T23:00:00Z') }
|
const scope = { date: new Date('1990-12-31T23:00:00Z') }
|
||||||
return test('{{ date | date: "%z"}}', scope, '-0600', opts)
|
return test('{{ date | date: "%z"}}', scope, '-0600', opts)
|
||||||
|
|||||||
Reference in New Issue
Block a user