diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index bb5491188..a4e79c3b3 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -20,7 +20,9 @@ jobs: - name: Test run: | npm run lint - npm run test + TZ=Etc/GMT npm run test + TZ=Asia/Shanghai npm run test + TZ=America/New_York npm run test - name: Coverage uses: coverallsapp/github-action@v1.1.2 with: diff --git a/src/util/liquid-date.ts b/src/util/liquid-date.ts index 2891648b6..fb2c348f6 100644 --- a/src/util/liquid-date.ts +++ b/src/util/liquid-date.ts @@ -14,6 +14,6 @@ export interface LiquidDate { getMonth(): number; getFullYear(): number; getTimezoneOffset(): number; - toLocaleTimeString(locale?: string, init?: any): string; - toLocaleDateString(locale?: string, init?: any): string; + toLocaleTimeString(): string; + toLocaleDateString(): string; } diff --git a/src/util/timezone-date.spec.ts b/src/util/timezone-date.spec.ts index c1aa76c66..5f4c3a62f 100644 --- a/src/util/timezone-date.spec.ts +++ b/src/util/timezone-date.spec.ts @@ -24,6 +24,12 @@ describe('TimezoneDate', () => { const date = new TimezoneDate('2021-12-07T00:00:00.001+08:00', -480) expect(date.getDay()).toBe(2) }) + it('should support .toLocaleString()', () => { + const date = new TimezoneDate('2021-10-06T00:00:00.001+00:00', -480) + expect(date.toLocaleString('en-US')).toMatch(/8:00:00\sAM$/) + expect(date.toLocaleString('en-US', { timeZone: 'America/New_York' })).toMatch(/8:00:00\sPM$/) + expect(() => date.toLocaleString()).not.toThrow() + }) it('should support .toLocaleTimeString()', () => { const date = new TimezoneDate('2021-10-06T00:00:00.001+00:00', -480) expect(date.toLocaleTimeString('en-US')).toMatch(/^8:00:00\sAM$/) diff --git a/src/util/timezone-date.ts b/src/util/timezone-date.ts index 49427e371..6e65c80f3 100644 --- a/src/util/timezone-date.ts +++ b/src/util/timezone-date.ts @@ -2,7 +2,6 @@ import { LiquidDate } from './liquid-date' // one minute in milliseconds const OneMinute = 60000 -const hostTimezoneOffset = new Date().getTimezoneOffset() const ISO8601_TIMEZONE_PATTERN = /([zZ]|([+-])(\d{2}):(\d{2}))$/ /** @@ -22,7 +21,7 @@ export class TimezoneDate implements LiquidDate { : new Date(init) this.timezoneOffset = timezoneOffset - const diff = (hostTimezoneOffset - this.timezoneOffset) * OneMinute + const diff = (this.date.getTimezoneOffset() - this.timezoneOffset) * OneMinute const time = this.date.getTime() + diff this.displayDate = new Date(time) } @@ -56,22 +55,16 @@ export class TimezoneDate implements LiquidDate { return this.displayDate.getFullYear() } toLocaleString (locale?: string, init?: any) { - if (locale === undefined) { - return this.displayDate.toLocaleString(locale) + if (init?.timeZone) { + return this.date.toLocaleString(locale, init) } - return this.date.toLocaleString(locale, init) + return this.displayDate.toLocaleString(locale, init) } - toLocaleTimeString (locale?: string, init?: any) { - if (locale === undefined) { - return this.displayDate.toLocaleTimeString(locale) - } - return this.date.toLocaleTimeString(locale, init) + toLocaleTimeString (locale?: string) { + return this.displayDate.toLocaleTimeString(locale) } - toLocaleDateString (locale?: string, init?: any) { - if (locale === undefined) { - return this.displayDate.toLocaleDateString(locale) - } - return this.date.toLocaleDateString(locale, init) + toLocaleDateString (locale?: string) { + return this.displayDate.toLocaleDateString(locale) } getTimezoneOffset () { return this.timezoneOffset! diff --git a/test/e2e/issues.spec.ts b/test/e2e/issues.spec.ts index f26bdae1a..09c026482 100644 --- a/test/e2e/issues.spec.ts +++ b/test/e2e/issues.spec.ts @@ -406,4 +406,24 @@ describe('Issues', function () { const html = await engine.parseAndRender(template) expect(html).toContain('true') }) + it('#604 date filter appears to add DST correction to UTC dates', () => { + const engine = new Liquid({ + timezoneOffset: 'Etc/GMT' + }) + + const html = engine.parseAndRenderSync( + '{{ "2023-04-05T12:00:00Z" | date: "%Y-%m-%dT%H:%M:%S%z", "Etc/GMT" }}' + + '{{ "2023-01-05T12:00:00Z" | date: "%Y-%m-%dT%H:%M:%S%z", 0 }}' + + '{{ "2023-01-05T12:00:00Z" | date: "%Y-%m-%dT%H:%M:%S%z", "Etc/GMT" }}' + + '{{ "2023-01-05T12:00:00Z" | date: "%Y-%m-%dT%H:%M:%S%z" }}' + + '{{ "2023-01-05T12:00:00+0000" | date: "%Y-%m-%dT%H:%M:%S%z", 0 }}' + ) + const expected = + '2023-04-05T12:00:00+0000' + + '2023-01-05T12:00:00+0000' + + '2023-01-05T12:00:00+0000' + + '2023-01-05T12:00:00+0000' + + '2023-01-05T12:00:00+0000' + expect(html).toEqual(expected) + }) })