mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
fix: incorrect timezone correction for DST dates, fixes #604
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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$/)
|
||||
|
||||
@@ -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!
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user