Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/filters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function date_to_xmlschema (this: FilterImpl, v: string | Date) {
}

export function date_to_rfc822 (this: FilterImpl, v: string | Date) {
return date.call(this, v, '%a, %d %b %Y %H:%M:%S %z')
const date = parseDate(v, this.context.opts, undefined, 'en-US')
if (!date) return v
return strftime(date, '%a, %d %b %Y %H:%M:%S %z', this.context.memoryLimit)
}

export function date_to_string (this: FilterImpl, v: string | Date, type?: string, style?: string) {
Expand All @@ -42,10 +44,9 @@ function stringify_date (this: FilterImpl, v: string | Date, month_type: string,
return strftime(date, `%d ${month_type} %Y`, ml)
}

function parseDate (v: string | Date, opts: NormalizedFullOptions, timezoneOffset?: number | string): LiquidDate | undefined {
function parseDate (v: string | Date, opts: NormalizedFullOptions, timezoneOffset?: number | string, locale = opts.locale): LiquidDate | undefined {
let date: LiquidDate | undefined
const defaultTimezoneOffset = timezoneOffset ?? opts.timezoneOffset
const locale = opts.locale
v = toValue(v)
if (isNil(v)) {
return undefined
Expand Down
20 changes: 20 additions & 0 deletions test/integration/filters/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,26 @@ describe('filters/date_to_rfc822', function () {
const output = liquid.parseAndRenderSync('{{ "2008-11-07T13:07:54-08:00" | date_to_rfc822 }}')
expect(output).toEqual('Fri, 07 Nov 2008 13:07:54 -0800')
})
it('should use English names regardless of locale', function () {
const liquid = new Liquid({ locale: 'zh-CN', preserveTimezones: true })
const output = liquid.parseAndRenderSync('{{ "2008-11-07T13:07:54-08:00" | date_to_rfc822 }}')
expect(output).toEqual('Fri, 07 Nov 2008 13:07:54 -0800')
})
it('should keep the offset of the input with preserveTimezones', function () {
const liquid = new Liquid({ locale: 'zh-CN', preserveTimezones: true, timezoneOffset: 0 })
const output = liquid.parseAndRenderSync('{{ "2008-11-07T23:30:00-08:00" | date_to_rfc822 }}')
expect(output).toEqual('Fri, 07 Nov 2008 23:30:00 -0800')
})
it('should convert to timezoneOffset without preserveTimezones', function () {
const liquid = new Liquid({ locale: 'zh-CN', timezoneOffset: 0 })
const output = liquid.parseAndRenderSync('{{ "2008-11-07T23:30:00-08:00" | date_to_rfc822 }}')
expect(output).toEqual('Sat, 08 Nov 2008 07:30:00 +0000')
})
it('should use timezoneOffset for a timestamp with preserveTimezones', function () {
const liquid = new Liquid({ locale: 'zh-CN', preserveTimezones: true, timezoneOffset: 480 })
const output = liquid.parseAndRenderSync('{{ 1226129400 | date_to_rfc822 }}')
expect(output).toEqual('Fri, 07 Nov 2008 23:30:00 -0800')
})
})
describe('filters/date_to_string', function () {
const liquid = new Liquid({ preserveTimezones: true })
Expand Down
Loading