Skip to content

Commit

Permalink
feat: support timezone offset argument for date filter, #553
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Nov 27, 2022
1 parent cd918cc commit 7a71485
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/builtin/filters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/builtin/filters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit 7a71485

Please sign in to comment.