Skip to content

Commit 59cf3c0

Browse files
committed
fix: preserveTimezones support for RFC2822 date, #784
1 parent 5f1a4cf commit 59cf3c0

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/util/liquid-date.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { isString } from './underscore'
33

44
// one minute in milliseconds
55
const OneMinute = 60000
6-
const ISO8601_TIMEZONE_PATTERN = /([zZ]|([+-])(\d{2}):(\d{2}))$/
6+
/**
7+
* Need support both ISO8601 and RFC2822 as in major browsers & NodeJS
8+
* RFC2822: https://datatracker.ietf.org/doc/html/rfc2822#section-3.3
9+
*/
10+
const TIMEZONE_PATTERN = /([zZ]|([+-])(\d{2}):?(\d{2}))$/
711
const monthNames = [
812
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
913
'September', 'October', 'November', 'December'
@@ -126,7 +130,7 @@ export class LiquidDate {
126130
* Given that a template is expected to be parsed fewer times than rendered.
127131
*/
128132
static createDateFixedToTimezone (dateString: string, locale: string): LiquidDate {
129-
const m = dateString.match(ISO8601_TIMEZONE_PATTERN)
133+
const m = dateString.match(TIMEZONE_PATTERN)
130134
// representing a UTC timestamp
131135
if (m && m[1] === 'Z') {
132136
return new LiquidDate(+new Date(dateString), locale, 0)

test/integration/filters/date.spec.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,23 @@ describe('filters/date', function () {
8080
return test('{{ "1991-02-22T00:00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1991-02-22T00:00:00')
8181
})
8282
describe('when preserveTimezones is enabled', function () {
83-
const opts: LiquidOptions = { preserveTimezones: true }
83+
const opts: LiquidOptions = { preserveTimezones: true, locale: 'en-US' }
8484

8585
it('should not change the timezone between input and output', function () {
8686
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
8787
})
8888
it('should apply numeric timezone offset (0)', function () {
89-
return test('{{ "1990-12-31T23:00:00+00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
89+
return test('{{ "1990-12-31T23:00:00+00:00" | date: "%Y-%m-%dT%H:%M:%S %z"}}', '1990-12-31T23:00:00 +0000', undefined, opts)
9090
})
9191
it('should apply numeric timezone offset (-1)', function () {
92-
return test('{{ "1990-12-31T23:00:00-01:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
92+
return test('{{ "1990-12-31T23:00:00-01:00" | date: "%Y-%m-%dT%H:%M:%S %z"}}', '1990-12-31T23:00:00 -0100', undefined, opts)
9393
})
9494
it('should apply numeric timezone offset (+2.30)', function () {
95-
return test('{{ "1990-12-31T23:00:00+02:30" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
95+
return test('{{ "1990-12-31T23:00:00+02:30" | date: "%Y-%m-%dT%H:%M:%S %z"}}', '1990-12-31T23:00:00 +0230', undefined, opts)
96+
})
97+
it('should support timezone in more casual JavaScript Date', async () => {
98+
await test('{{ "2025-01-02 03:04:05 -0100" | date: "%Y-%m-%dT%H:%M:%S %z" }}', '2025-01-02T03:04:05 -0100', undefined, opts)
99+
await test('{{ "2025-01-02 03:04:05 -0100" | date }}', 'Thursday, January 2, 2025 at 3:04 am -0100', undefined, opts)
96100
})
97101
it('should automatically work when timezone not specified', function () {
98102
return test('{{ "1990-12-31T23:00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)

0 commit comments

Comments
 (0)