mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: support timezone offset argument for date filter, #553
This commit is contained in:
@@ -4,11 +4,11 @@ import { toValue, stringify, isString, isNumber } from '../../util/underscore'
|
||||
import { FilterImpl } from '../../template/filter/filter-impl'
|
||||
import { TimezoneDate } from '../../util/timezone-date'
|
||||
|
||||
export function date (this: FilterImpl, v: string | Date, arg: string) {
|
||||
export function date (this: FilterImpl, v: string | Date, format: string, timeZoneOffset?: number) {
|
||||
const opts = this.context.opts
|
||||
let date: LiquidDate
|
||||
v = toValue(v)
|
||||
arg = stringify(arg)
|
||||
format = stringify(format)
|
||||
if (v === 'now' || v === 'today') {
|
||||
date = new Date()
|
||||
} else if (isNumber(v)) {
|
||||
@@ -25,10 +25,12 @@ export function date (this: FilterImpl, v: string | Date, arg: string) {
|
||||
date = v
|
||||
}
|
||||
if (!isValidDate(date)) return v
|
||||
if (opts.hasOwnProperty('timezoneOffset')) {
|
||||
if (timeZoneOffset !== undefined) {
|
||||
date = new TimezoneDate(date, timeZoneOffset)
|
||||
} else if (opts.timezoneOffset !== undefined) {
|
||||
date = new TimezoneDate(date, opts.timezoneOffset!)
|
||||
}
|
||||
return strftime(date, arg)
|
||||
return strftime(date, format)
|
||||
}
|
||||
|
||||
function isValidDate (date: any): date is Date {
|
||||
|
||||
@@ -61,6 +61,9 @@ describe('filters/date', function () {
|
||||
it('should offset UTC date literal', function () {
|
||||
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T17:00:00', undefined, opts)
|
||||
})
|
||||
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 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)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user