fix: support timezoneOffset for date from scope, #401

This commit is contained in:
Harttle
2021-10-06 15:45:41 +08:00
parent 4f6b88c154
commit fd5ef474c3
11 changed files with 132 additions and 53 deletions
+6
View File
@@ -108,4 +108,10 @@ describe('Issues', function () {
const html = await engine.parseAndRender(tpl)
expect(html).to.equal('\r\ntrue\r\n')
})
it('#401 Timezone Offset Issue', async () => {
const engine = new Liquid({ timezoneOffset: -600 })
const tpl = engine.parse('{{ date | date: "%Y-%m-%d %H:%M %p %z" }}')
const html = await engine.render(tpl, { date: '2021-10-06T15:31:00+08:00' })
expect(html).to.equal('2021-10-06 17:31 PM +1000')
})
})
+26
View File
@@ -54,4 +54,30 @@ describe('filters/date', function () {
'2017-02-28T12:00:00'
)
})
describe('timezoneOffset', function () {
// -06:00
const opts: LiquidOptions = { timezoneOffset: 360 }
it('should offset UTC date literal', function () {
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T17:00:00', undefined, opts)
})
it('should offset date literal with timezone 00:00 specified', function () {
return test('{{ "1990-12-31T23:00:00+00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T17:00:00', undefined, opts)
})
it('should offset date literal with timezone -01:00 specified', function () {
return test('{{ "1990-12-31T23:00:00-01:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T18:00:00', undefined, opts)
})
it('should offset date from scope', function () {
const scope = { date: new Date('1990-12-31T23:00:00Z') }
return test('{{ date | date: "%Y-%m-%dT%H:%M:%S"}}', scope, '1990-12-31T17:00:00', opts)
})
it('should reflect timezoneOffset', function () {
const scope = { date: new Date('1990-12-31T23:00:00Z') }
return test('{{ date | date: "%z"}}', scope, '-0600', opts)
})
it('should ignore this setting when `preserveTimezones` also specified', function () {
const opts: LiquidOptions = { timezoneOffset: 600, preserveTimezones: true }
return test('{{ "1990-12-31T23:00:00+02:30" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
})
})
})
+6
View File
@@ -0,0 +1,6 @@
export class DateWithTimezone extends Date {
constructor (init: string, timezone: number) {
super(init)
this.getTimezoneOffset = () => timezone
}
}
+4 -4
View File
@@ -8,11 +8,11 @@ export function render (src: string, ctx?: object) {
return liquid.parseAndRender(src, ctx)
}
export async function test (src: string, ctx: object | string, dst?: string, opts?: LiquidOptions) {
if (dst === undefined) {
dst = ctx as string
export async function test (src: string, ctx: object | string, expected?: string, opts?: LiquidOptions) {
if (expected === undefined) {
expected = ctx as string
ctx = {}
}
const engine = opts ? new Liquid(opts) : liquid
return expect(await engine.parseAndRender(src, ctx as object)).to.equal(dst)
return expect(await engine.parseAndRender(src, ctx as object)).to.equal(expected)
}
+8 -11
View File
@@ -1,5 +1,6 @@
import * as chai from 'chai'
import t, { timezoneOffset } from '../../../src/util/strftime'
import t from '../../../src/util/strftime'
import { DateWithTimezone } from '../../stub/date-with-timezone'
const expect = chai.expect
describe('util/strftime', function () {
@@ -118,18 +119,14 @@ describe('util/strftime', function () {
})
describe('Time zone', () => {
afterEach(() => {
(timezoneOffset as any) = (new Date()).getTimezoneOffset()
})
it('should format %z as time zone', function () {
const now = new Date('2016-01-04 13:15:23');
(timezoneOffset as any) = -480 // suppose we're in +8:00
// suppose we're in +8:00
const now = new DateWithTimezone('2016-01-04 13:15:23', -480)
expect(t(now, '%z')).to.equal('+0800')
})
it('should format %z as negative time zone', function () {
const date = new Date('2016-01-04T13:15:23.000Z');
(timezoneOffset as any) = 480 // suppose we're in -8:00
// suppose we're in -8:00
const date = new DateWithTimezone('2016-01-04T13:15:23.000Z', 480)
expect(t(date, '%z')).to.equal('-0800')
})
})
@@ -210,8 +207,8 @@ describe('util/strftime', function () {
expect(t(now, '%#P')).to.equal('PM')
})
it('should support : flag', () => {
const date = new Date('2016-01-04T13:15:23.000Z');
(timezoneOffset as any) = -480 // suppose we're in +8:00
// suppose we're in +8:00
const date = new DateWithTimezone('2016-01-04T13:15:23.000Z', -480)
expect(t(date, '%:z')).to.equal('+08:00')
expect(t(date, '%z')).to.equal('+0800')
})
+16
View File
@@ -0,0 +1,16 @@
import { TimezoneDate } from '../../../src/util/timezone-date'
import { expect } from 'chai'
describe('TimezoneDate', () => {
it('should respect timezone set to 00:00', () => {
const date = new TimezoneDate('2021-10-06T14:26:00.000+08:00', 0)
expect(date.getTimezoneOffset()).to.equal(0)
expect(date.getHours()).to.equal(6)
expect(date.getMinutes()).to.equal(26)
})
it('should respect timezone set to -06:00', () => {
const date = new TimezoneDate('2021-10-06T14:26:00.000+08:00', -360)
expect(date.getTimezoneOffset()).to.equal(-360)
expect(date.getMinutes()).to.equal(26)
})
})