Skip to content

Commit

Permalink
fix: incorrect timezone correction for DST dates, fixes #604
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Apr 24, 2023
1 parent 21ee27b commit 33b3c01
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
Expand Down
4 changes: 2 additions & 2 deletions src/util/liquid-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions src/util/timezone-date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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$/)
Expand Down
23 changes: 8 additions & 15 deletions src/util/timezone-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}))$/

/**
Expand All @@ -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)
}
Expand Down Expand Up @@ -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!
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/issues.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 33b3c01

Please sign in to comment.