fix: throws when using preserveTimezones on Node.js, fixes #431

This commit is contained in:
Harttle
2021-12-07 22:43:47 +08:00
committed by Harttle
parent 80c2280ee6
commit e2ef236f68
6 changed files with 117 additions and 51 deletions
+1
View File
@@ -50,6 +50,7 @@
<td align="center"><a href="https://github.com/bglw"><img src="https://avatars.githubusercontent.com/u/40188355?v=4?s=100" width="100px;" alt=""/></a></td> <td align="center"><a href="https://github.com/bglw"><img src="https://avatars.githubusercontent.com/u/40188355?v=4?s=100" width="100px;" alt=""/></a></td>
<td align="center"><a href="https://about.me/jasonkurian"><img src="https://avatars.githubusercontent.com/u/2642545?v=4?s=100" width="100px;" alt=""/></a></td> <td align="center"><a href="https://about.me/jasonkurian"><img src="https://avatars.githubusercontent.com/u/2642545?v=4?s=100" width="100px;" alt=""/></a></td>
<td align="center"><a href="https://github.com/dphm"><img src="https://avatars.githubusercontent.com/u/1707217?v=4?s=100" width="100px;" alt=""/></a></td> <td align="center"><a href="https://github.com/dphm"><img src="https://avatars.githubusercontent.com/u/1707217?v=4?s=100" width="100px;" alt=""/></a></td>
<td align="center"><a href="https://www.aleksandrhovhannisyan.com/"><img src="https://avatars.githubusercontent.com/u/19352442?v=4?s=100" width="100px;" alt=""/></a></td>
</tr> </tr>
</table> </table>
+3 -3
View File
@@ -1,11 +1,11 @@
import strftime from '../../util/strftime' import strftime, { LiquidDate } from '../../util/strftime'
import { isString, isNumber } from '../../util/underscore' import { isString, isNumber } from '../../util/underscore'
import { FilterImpl } from '../../template/filter/filter-impl' import { FilterImpl } from '../../template/filter/filter-impl'
import { TimezoneDate } from '../../util/timezone-date' import { TimezoneDate } from '../../util/timezone-date'
export function date (this: FilterImpl, v: string | Date, arg: string) { export function date (this: FilterImpl, v: string | Date, arg: string) {
const opts = this.context.opts const opts = this.context.opts
let date: Date let date: LiquidDate
if (v === 'now' || v === 'today') { if (v === 'now' || v === 'today') {
date = new Date() date = new Date()
} else if (isNumber(v)) { } else if (isNumber(v)) {
@@ -29,5 +29,5 @@ export function date (this: FilterImpl, v: string | Date, arg: string) {
} }
function isValidDate (date: any): date is Date { function isValidDate (date: any): date is Date {
return date instanceof Date && !isNaN(date.getTime()) return (date instanceof Date || date instanceof TimezoneDate) && !isNaN(date.getTime())
} }
+54 -39
View File
@@ -1,5 +1,20 @@
import { changeCase, padStart, padEnd } from './underscore' import { changeCase, padStart, padEnd } from './underscore'
export interface LiquidDate {
getTime(): number;
getMilliseconds(): number;
getSeconds(): number;
getMinutes(): number;
getHours(): number;
getDay(): number;
getDate(): number;
getMonth(): number;
getFullYear(): number;
getTimezoneOffset(): number;
toLocaleTimeString(): string;
toLocaleDateString(): string;
}
const rFormat = /%([-_0^#:]+)?(\d+)?([EO])?(.)/ const rFormat = /%([-_0^#:]+)?(\d+)?([EO])?(.)/
const monthNames = [ const monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
@@ -27,18 +42,18 @@ function abbr (str: string) {
} }
// prototype extensions // prototype extensions
function daysInMonth (d: Date) { function daysInMonth (d: LiquidDate) {
const feb = isLeapYear(d) ? 29 : 28 const feb = isLeapYear(d) ? 29 : 28
return [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] return [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
} }
function getDayOfYear (d: Date) { function getDayOfYear (d: LiquidDate) {
let num = 0 let num = 0
for (let i = 0; i < d.getMonth(); ++i) { for (let i = 0; i < d.getMonth(); ++i) {
num += daysInMonth(d)[i] num += daysInMonth(d)[i]
} }
return num + d.getDate() return num + d.getDate()
} }
function getWeekOfYear (d: Date, startDay: number) { function getWeekOfYear (d: LiquidDate, startDay: number) {
// Skip to startDay of this week // Skip to startDay of this week
const now = getDayOfYear(d) + (startDay - d.getDay()) const now = getDayOfYear(d) + (startDay - d.getDay())
// Find the first startDay of the year // Find the first startDay of the year
@@ -46,16 +61,16 @@ function getWeekOfYear (d: Date, startDay: number) {
const then = (7 - jan1.getDay() + startDay) const then = (7 - jan1.getDay() + startDay)
return String(Math.floor((now - then) / 7) + 1) return String(Math.floor((now - then) / 7) + 1)
} }
function isLeapYear (d: Date) { function isLeapYear (d: LiquidDate) {
const year = d.getFullYear() const year = d.getFullYear()
return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year))) return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)))
} }
function getSuffix (d: Date) { function getSuffix (d: LiquidDate) {
const str = d.getDate().toString() const str = d.getDate().toString()
const index = parseInt(str.slice(-1)) const index = parseInt(str.slice(-1))
return suffixes[index] || suffixes['default'] return suffixes[index] || suffixes['default']
} }
function century (d: Date) { function century (d: LiquidDate) {
return parseInt(d.getFullYear().toString().substring(0, 2), 10) return parseInt(d.getFullYear().toString().substring(0, 2), 10)
} }
@@ -90,41 +105,41 @@ const padChars = {
P: ' ' P: ' '
} }
const formatCodes = { const formatCodes = {
a: (d: Date) => dayNamesShort[d.getDay()], a: (d: LiquidDate) => dayNamesShort[d.getDay()],
A: (d: Date) => dayNames[d.getDay()], A: (d: LiquidDate) => dayNames[d.getDay()],
b: (d: Date) => monthNamesShort[d.getMonth()], b: (d: LiquidDate) => monthNamesShort[d.getMonth()],
B: (d: Date) => monthNames[d.getMonth()], B: (d: LiquidDate) => monthNames[d.getMonth()],
c: (d: Date) => d.toLocaleString(), c: (d: LiquidDate) => d.toLocaleString(),
C: (d: Date) => century(d), C: (d: LiquidDate) => century(d),
d: (d: Date) => d.getDate(), d: (d: LiquidDate) => d.getDate(),
e: (d: Date) => d.getDate(), e: (d: LiquidDate) => d.getDate(),
H: (d: Date) => d.getHours(), H: (d: LiquidDate) => d.getHours(),
I: (d: Date) => String(d.getHours() % 12 || 12), I: (d: LiquidDate) => String(d.getHours() % 12 || 12),
j: (d: Date) => getDayOfYear(d), j: (d: LiquidDate) => getDayOfYear(d),
k: (d: Date) => d.getHours(), k: (d: LiquidDate) => d.getHours(),
l: (d: Date) => String(d.getHours() % 12 || 12), l: (d: LiquidDate) => String(d.getHours() % 12 || 12),
L: (d: Date) => d.getMilliseconds(), L: (d: LiquidDate) => d.getMilliseconds(),
m: (d: Date) => d.getMonth() + 1, m: (d: LiquidDate) => d.getMonth() + 1,
M: (d: Date) => d.getMinutes(), M: (d: LiquidDate) => d.getMinutes(),
N: (d: Date, opts: FormatOptions) => { N: (d: LiquidDate, opts: FormatOptions) => {
const width = Number(opts.width) || 9 const width = Number(opts.width) || 9
const str = String(d.getMilliseconds()).substr(0, width) const str = String(d.getMilliseconds()).substr(0, width)
return padEnd(str, width, '0') return padEnd(str, width, '0')
}, },
p: (d: Date) => (d.getHours() < 12 ? 'AM' : 'PM'), p: (d: LiquidDate) => (d.getHours() < 12 ? 'AM' : 'PM'),
P: (d: Date) => (d.getHours() < 12 ? 'am' : 'pm'), P: (d: LiquidDate) => (d.getHours() < 12 ? 'am' : 'pm'),
q: (d: Date) => getSuffix(d), q: (d: LiquidDate) => getSuffix(d),
s: (d: Date) => Math.round(d.valueOf() / 1000), s: (d: LiquidDate) => Math.round(d.getTime() / 1000),
S: (d: Date) => d.getSeconds(), S: (d: LiquidDate) => d.getSeconds(),
u: (d: Date) => d.getDay() || 7, u: (d: LiquidDate) => d.getDay() || 7,
U: (d: Date) => getWeekOfYear(d, 0), U: (d: LiquidDate) => getWeekOfYear(d, 0),
w: (d: Date) => d.getDay(), w: (d: LiquidDate) => d.getDay(),
W: (d: Date) => getWeekOfYear(d, 1), W: (d: LiquidDate) => getWeekOfYear(d, 1),
x: (d: Date) => d.toLocaleDateString(), x: (d: LiquidDate) => d.toLocaleDateString(),
X: (d: Date) => d.toLocaleTimeString(), X: (d: LiquidDate) => d.toLocaleTimeString(),
y: (d: Date) => d.getFullYear().toString().substring(2, 4), y: (d: LiquidDate) => d.getFullYear().toString().substring(2, 4),
Y: (d: Date) => d.getFullYear(), Y: (d: LiquidDate) => d.getFullYear(),
z: (d: Date, opts: FormatOptions) => { z: (d: LiquidDate, opts: FormatOptions) => {
const nOffset = Math.abs(d.getTimezoneOffset()) const nOffset = Math.abs(d.getTimezoneOffset())
const h = Math.floor(nOffset / 60) const h = Math.floor(nOffset / 60)
const m = nOffset % 60 const m = nOffset % 60
@@ -139,7 +154,7 @@ const formatCodes = {
}; };
(formatCodes as any).h = formatCodes.b (formatCodes as any).h = formatCodes.b
export default function (d: Date, formatStr: string) { export default function (d: LiquidDate, formatStr: string) {
let output = '' let output = ''
let remaining = formatStr let remaining = formatStr
let match let match
@@ -151,7 +166,7 @@ export default function (d: Date, formatStr: string) {
return output + remaining return output + remaining
} }
function format (d: Date, match: RegExpExecArray) { function format (d: LiquidDate, match: RegExpExecArray) {
const [input, flagStr = '', width, modifier, conversion] = match const [input, flagStr = '', width, modifier, conversion] = match
const convert = formatCodes[conversion] const convert = formatCodes[conversion]
if (!convert) return input if (!convert) return input
+49 -7
View File
@@ -1,3 +1,5 @@
import { LiquidDate } from './strftime'
// one minute in milliseconds // one minute in milliseconds
const OneMinute = 60000 const OneMinute = 60000
const hostTimezoneOffset = new Date().getTimezoneOffset() const hostTimezoneOffset = new Date().getTimezoneOffset()
@@ -10,14 +12,54 @@ const ISO8601_TIMEZONE_PATTERN = /([zZ]|([+-])(\d{2}):(\d{2}))$/
* - create a Date offset by it's timezone difference, avoiding overriding a bunch of methods * - create a Date offset by it's timezone difference, avoiding overriding a bunch of methods
* - rewrite getTimezoneOffset() to trick strftime * - rewrite getTimezoneOffset() to trick strftime
*/ */
export class TimezoneDate extends Date { export class TimezoneDate implements LiquidDate {
private timezoneOffset?: number private timezoneOffset?: number
constructor (init: string | number | Date, timezoneOffset: number) { private date: Date
if (init instanceof TimezoneDate) return init constructor (init: string | number | Date | TimezoneDate, timezoneOffset: number) {
const diff = (hostTimezoneOffset - timezoneOffset) * OneMinute const diff = (hostTimezoneOffset - timezoneOffset) * OneMinute
const time = new Date(init).getTime() + diff if (init instanceof TimezoneDate) {
super(time) this.date = init.date
this.timezoneOffset = timezoneOffset this.timezoneOffset = init.timezoneOffset
} else {
const time = new Date(init).getTime() + diff
this.date = new Date(time)
this.timezoneOffset = timezoneOffset
}
}
getTime () {
return this.date.getTime()
}
getMilliseconds () {
return this.date.getMilliseconds()
}
getSeconds () {
return this.date.getSeconds()
}
getMinutes () {
return this.date.getMinutes()
}
getHours () {
return this.date.getHours()
}
getDay () {
return this.date.getDay()
}
getDate () {
return this.date.getDate()
}
getMonth () {
return this.date.getMonth()
}
getFullYear () {
return this.date.getFullYear()
}
toLocaleTimeString () {
return this.date.toLocaleTimeString()
}
toLocaleDateString () {
return this.date.toLocaleDateString()
} }
getTimezoneOffset () { getTimezoneOffset () {
return this.timezoneOffset! return this.timezoneOffset!
@@ -36,7 +78,7 @@ export class TimezoneDate extends Date {
* we create a different Date to trick strftime, it's both simpler and more performant. * we create a different Date to trick strftime, it's both simpler and more performant.
* Given that a template is expected to be parsed fewer times than rendered. * Given that a template is expected to be parsed fewer times than rendered.
*/ */
static createDateFixedToTimezone (dateString: string) { static createDateFixedToTimezone (dateString: string): LiquidDate {
const m = dateString.match(ISO8601_TIMEZONE_PATTERN) const m = dateString.match(ISO8601_TIMEZONE_PATTERN)
// representing a UTC timestamp // representing a UTC timestamp
if (m && m[1] === 'Z') { if (m && m[1] === 'Z') {
+9 -1
View File
@@ -1,4 +1,4 @@
import { Liquid } from '../../src/liquid' import { Liquid } from '../..'
import { expect, use } from 'chai' import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised' import * as chaiAsPromised from 'chai-as-promised'
import * as sinon from 'sinon' import * as sinon from 'sinon'
@@ -153,4 +153,12 @@ describe('Issues', function () {
expect(exists).to.be.calledOnce expect(exists).to.be.calledOnce
expect(readFile).to.be.calledOnce expect(readFile).to.be.calledOnce
}) })
it('#431 Error when using Date timezoneOffset in 9.28.5', async () => {
const engine = new Liquid({
timezoneOffset: 0,
preserveTimezones: true
})
const tpl = engine.parse('Welcome to {{ now | date: "%Y-%m-%d" }}!')
expect(engine.render(tpl, { now: new Date('2019/02/01') })).to.eventually.equal('Welcome to 2019-02-01')
})
}) })
+1 -1
View File
@@ -75,7 +75,7 @@ describe('filters/date', function () {
const scope = { date: new Date('1990-12-31T23:00:00Z') } const scope = { date: new Date('1990-12-31T23:00:00Z') }
return test('{{ date | date: "%z"}}', scope, '-0600', opts) return test('{{ date | date: "%z"}}', scope, '-0600', opts)
}) })
it('should ignore this setting when `preserveTimezones` also specified', function () { it('should work with `preserveTimezones`', function () {
const opts: LiquidOptions = { timezoneOffset: 600, preserveTimezones: true } 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) return test('{{ "1990-12-31T23:00:00+02:30" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
}) })