mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
fix: incorrect timezone correction for DST dates, fixes #604
This commit is contained in:
@@ -20,7 +20,9 @@ jobs:
|
|||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
npm run lint
|
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
|
- name: Coverage
|
||||||
uses: coverallsapp/[email protected]
|
uses: coverallsapp/[email protected]
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -14,6 +14,6 @@ export interface LiquidDate {
|
|||||||
getMonth(): number;
|
getMonth(): number;
|
||||||
getFullYear(): number;
|
getFullYear(): number;
|
||||||
getTimezoneOffset(): number;
|
getTimezoneOffset(): number;
|
||||||
toLocaleTimeString(locale?: string, init?: any): string;
|
toLocaleTimeString(): string;
|
||||||
toLocaleDateString(locale?: string, init?: any): string;
|
toLocaleDateString(): string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,12 @@ describe('TimezoneDate', () => {
|
|||||||
const date = new TimezoneDate('2021-12-07T00:00:00.001+08:00', -480)
|
const date = new TimezoneDate('2021-12-07T00:00:00.001+08:00', -480)
|
||||||
expect(date.getDay()).toBe(2)
|
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()', () => {
|
it('should support .toLocaleTimeString()', () => {
|
||||||
const date = new TimezoneDate('2021-10-06T00:00:00.001+00:00', -480)
|
const date = new TimezoneDate('2021-10-06T00:00:00.001+00:00', -480)
|
||||||
expect(date.toLocaleTimeString('en-US')).toMatch(/^8:00:00\sAM$/)
|
expect(date.toLocaleTimeString('en-US')).toMatch(/^8:00:00\sAM$/)
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { LiquidDate } from './liquid-date'
|
|||||||
|
|
||||||
// one minute in milliseconds
|
// one minute in milliseconds
|
||||||
const OneMinute = 60000
|
const OneMinute = 60000
|
||||||
const hostTimezoneOffset = new Date().getTimezoneOffset()
|
|
||||||
const ISO8601_TIMEZONE_PATTERN = /([zZ]|([+-])(\d{2}):(\d{2}))$/
|
const ISO8601_TIMEZONE_PATTERN = /([zZ]|([+-])(\d{2}):(\d{2}))$/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,7 +21,7 @@ export class TimezoneDate implements LiquidDate {
|
|||||||
: new Date(init)
|
: new Date(init)
|
||||||
this.timezoneOffset = timezoneOffset
|
this.timezoneOffset = timezoneOffset
|
||||||
|
|
||||||
const diff = (hostTimezoneOffset - this.timezoneOffset) * OneMinute
|
const diff = (this.date.getTimezoneOffset() - this.timezoneOffset) * OneMinute
|
||||||
const time = this.date.getTime() + diff
|
const time = this.date.getTime() + diff
|
||||||
this.displayDate = new Date(time)
|
this.displayDate = new Date(time)
|
||||||
}
|
}
|
||||||
@@ -56,22 +55,16 @@ export class TimezoneDate implements LiquidDate {
|
|||||||
return this.displayDate.getFullYear()
|
return this.displayDate.getFullYear()
|
||||||
}
|
}
|
||||||
toLocaleString (locale?: string, init?: any) {
|
toLocaleString (locale?: string, init?: any) {
|
||||||
if (locale === undefined) {
|
if (init?.timeZone) {
|
||||||
return this.displayDate.toLocaleString(locale)
|
return this.date.toLocaleString(locale, init)
|
||||||
}
|
}
|
||||||
return this.date.toLocaleString(locale, init)
|
return this.displayDate.toLocaleString(locale, init)
|
||||||
}
|
}
|
||||||
toLocaleTimeString (locale?: string, init?: any) {
|
toLocaleTimeString (locale?: string) {
|
||||||
if (locale === undefined) {
|
return this.displayDate.toLocaleTimeString(locale)
|
||||||
return this.displayDate.toLocaleTimeString(locale)
|
|
||||||
}
|
|
||||||
return this.date.toLocaleTimeString(locale, init)
|
|
||||||
}
|
}
|
||||||
toLocaleDateString (locale?: string, init?: any) {
|
toLocaleDateString (locale?: string) {
|
||||||
if (locale === undefined) {
|
return this.displayDate.toLocaleDateString(locale)
|
||||||
return this.displayDate.toLocaleDateString(locale)
|
|
||||||
}
|
|
||||||
return this.date.toLocaleDateString(locale, init)
|
|
||||||
}
|
}
|
||||||
getTimezoneOffset () {
|
getTimezoneOffset () {
|
||||||
return this.timezoneOffset!
|
return this.timezoneOffset!
|
||||||
|
|||||||
@@ -406,4 +406,24 @@ describe('Issues', function () {
|
|||||||
const html = await engine.parseAndRender(template)
|
const html = await engine.parseAndRender(template)
|
||||||
expect(html).toContain('true')
|
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