Skip to content

Commit

Permalink
fix: throws when using preserveTimezones on Node.js, fixes #431
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Dec 7, 2021
1 parent 80c2280 commit e2ef236
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 51 deletions.
1 change: 1 addition & 0 deletions docs/themes/navy/layout/partial/all-contributors.swig
Expand Up @@ -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://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://www.aleksandrhovhannisyan.com/"><img src="https://avatars.githubusercontent.com/u/19352442?v=4?s=100" width="100px;" alt=""/></a></td>
</tr>
</table>

Expand Down
6 changes: 3 additions & 3 deletions src/builtin/filters/date.ts
@@ -1,11 +1,11 @@
import strftime from '../../util/strftime'
import strftime, { LiquidDate } from '../../util/strftime'
import { isString, isNumber } from '../../util/underscore'
import { FilterImpl } from '../../template/filter/filter-impl'
import { TimezoneDate } from '../../util/timezone-date'

export function date (this: FilterImpl, v: string | Date, arg: string) {
const opts = this.context.opts
let date: Date
let date: LiquidDate
if (v === 'now' || v === 'today') {
date = new Date()
} else if (isNumber(v)) {
Expand All @@ -29,5 +29,5 @@ export function date (this: FilterImpl, v: string | Date, arg: string) {
}

function isValidDate (date: any): date is Date {
return date instanceof Date && !isNaN(date.getTime())
return (date instanceof Date || date instanceof TimezoneDate) && !isNaN(date.getTime())
}
93 changes: 54 additions & 39 deletions src/util/strftime.ts
@@ -1,5 +1,20 @@
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 monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
Expand Down Expand Up @@ -27,35 +42,35 @@ function abbr (str: string) {
}

// prototype extensions
function daysInMonth (d: Date) {
function daysInMonth (d: LiquidDate) {
const feb = isLeapYear(d) ? 29 : 28
return [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
}
function getDayOfYear (d: Date) {
function getDayOfYear (d: LiquidDate) {
let num = 0
for (let i = 0; i < d.getMonth(); ++i) {
num += daysInMonth(d)[i]
}
return num + d.getDate()
}
function getWeekOfYear (d: Date, startDay: number) {
function getWeekOfYear (d: LiquidDate, startDay: number) {
// Skip to startDay of this week
const now = getDayOfYear(d) + (startDay - d.getDay())
// Find the first startDay of the year
const jan1 = new Date(d.getFullYear(), 0, 1)
const then = (7 - jan1.getDay() + startDay)
return String(Math.floor((now - then) / 7) + 1)
}
function isLeapYear (d: Date) {
function isLeapYear (d: LiquidDate) {
const year = d.getFullYear()
return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)))
}
function getSuffix (d: Date) {
function getSuffix (d: LiquidDate) {
const str = d.getDate().toString()
const index = parseInt(str.slice(-1))
return suffixes[index] || suffixes['default']
}
function century (d: Date) {
function century (d: LiquidDate) {
return parseInt(d.getFullYear().toString().substring(0, 2), 10)
}

Expand Down Expand Up @@ -90,41 +105,41 @@ const padChars = {
P: ' '
}
const formatCodes = {
a: (d: Date) => dayNamesShort[d.getDay()],
A: (d: Date) => dayNames[d.getDay()],
b: (d: Date) => monthNamesShort[d.getMonth()],
B: (d: Date) => monthNames[d.getMonth()],
c: (d: Date) => d.toLocaleString(),
C: (d: Date) => century(d),
d: (d: Date) => d.getDate(),
e: (d: Date) => d.getDate(),
H: (d: Date) => d.getHours(),
I: (d: Date) => String(d.getHours() % 12 || 12),
j: (d: Date) => getDayOfYear(d),
k: (d: Date) => d.getHours(),
l: (d: Date) => String(d.getHours() % 12 || 12),
L: (d: Date) => d.getMilliseconds(),
m: (d: Date) => d.getMonth() + 1,
M: (d: Date) => d.getMinutes(),
N: (d: Date, opts: FormatOptions) => {
a: (d: LiquidDate) => dayNamesShort[d.getDay()],
A: (d: LiquidDate) => dayNames[d.getDay()],
b: (d: LiquidDate) => monthNamesShort[d.getMonth()],
B: (d: LiquidDate) => monthNames[d.getMonth()],
c: (d: LiquidDate) => d.toLocaleString(),
C: (d: LiquidDate) => century(d),
d: (d: LiquidDate) => d.getDate(),
e: (d: LiquidDate) => d.getDate(),
H: (d: LiquidDate) => d.getHours(),
I: (d: LiquidDate) => String(d.getHours() % 12 || 12),
j: (d: LiquidDate) => getDayOfYear(d),
k: (d: LiquidDate) => d.getHours(),
l: (d: LiquidDate) => String(d.getHours() % 12 || 12),
L: (d: LiquidDate) => d.getMilliseconds(),
m: (d: LiquidDate) => d.getMonth() + 1,
M: (d: LiquidDate) => d.getMinutes(),
N: (d: LiquidDate, opts: FormatOptions) => {
const width = Number(opts.width) || 9
const str = String(d.getMilliseconds()).substr(0, width)
return padEnd(str, width, '0')
},
p: (d: Date) => (d.getHours() < 12 ? 'AM' : 'PM'),
P: (d: Date) => (d.getHours() < 12 ? 'am' : 'pm'),
q: (d: Date) => getSuffix(d),
s: (d: Date) => Math.round(d.valueOf() / 1000),
S: (d: Date) => d.getSeconds(),
u: (d: Date) => d.getDay() || 7,
U: (d: Date) => getWeekOfYear(d, 0),
w: (d: Date) => d.getDay(),
W: (d: Date) => getWeekOfYear(d, 1),
x: (d: Date) => d.toLocaleDateString(),
X: (d: Date) => d.toLocaleTimeString(),
y: (d: Date) => d.getFullYear().toString().substring(2, 4),
Y: (d: Date) => d.getFullYear(),
z: (d: Date, opts: FormatOptions) => {
p: (d: LiquidDate) => (d.getHours() < 12 ? 'AM' : 'PM'),
P: (d: LiquidDate) => (d.getHours() < 12 ? 'am' : 'pm'),
q: (d: LiquidDate) => getSuffix(d),
s: (d: LiquidDate) => Math.round(d.getTime() / 1000),
S: (d: LiquidDate) => d.getSeconds(),
u: (d: LiquidDate) => d.getDay() || 7,
U: (d: LiquidDate) => getWeekOfYear(d, 0),
w: (d: LiquidDate) => d.getDay(),
W: (d: LiquidDate) => getWeekOfYear(d, 1),
x: (d: LiquidDate) => d.toLocaleDateString(),
X: (d: LiquidDate) => d.toLocaleTimeString(),
y: (d: LiquidDate) => d.getFullYear().toString().substring(2, 4),
Y: (d: LiquidDate) => d.getFullYear(),
z: (d: LiquidDate, opts: FormatOptions) => {
const nOffset = Math.abs(d.getTimezoneOffset())
const h = Math.floor(nOffset / 60)
const m = nOffset % 60
Expand All @@ -139,7 +154,7 @@ const formatCodes = {
};
(formatCodes as any).h = formatCodes.b

export default function (d: Date, formatStr: string) {
export default function (d: LiquidDate, formatStr: string) {
let output = ''
let remaining = formatStr
let match
Expand All @@ -151,7 +166,7 @@ export default function (d: Date, formatStr: string) {
return output + remaining
}

function format (d: Date, match: RegExpExecArray) {
function format (d: LiquidDate, match: RegExpExecArray) {
const [input, flagStr = '', width, modifier, conversion] = match
const convert = formatCodes[conversion]
if (!convert) return input
Expand Down
56 changes: 49 additions & 7 deletions src/util/timezone-date.ts
@@ -1,3 +1,5 @@
import { LiquidDate } from './strftime'

// one minute in milliseconds
const OneMinute = 60000
const hostTimezoneOffset = new Date().getTimezoneOffset()
Expand All @@ -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
* - rewrite getTimezoneOffset() to trick strftime
*/
export class TimezoneDate extends Date {
export class TimezoneDate implements LiquidDate {
private timezoneOffset?: number
constructor (init: string | number | Date, timezoneOffset: number) {
if (init instanceof TimezoneDate) return init
private date: Date
constructor (init: string | number | Date | TimezoneDate, timezoneOffset: number) {
const diff = (hostTimezoneOffset - timezoneOffset) * OneMinute
const time = new Date(init).getTime() + diff
super(time)
this.timezoneOffset = timezoneOffset
if (init instanceof TimezoneDate) {
this.date = init.date
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 () {
return this.timezoneOffset!
Expand All @@ -36,7 +78,7 @@ export class TimezoneDate extends Date {
* 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.
*/
static createDateFixedToTimezone (dateString: string) {
static createDateFixedToTimezone (dateString: string): LiquidDate {
const m = dateString.match(ISO8601_TIMEZONE_PATTERN)
// representing a UTC timestamp
if (m && m[1] === 'Z') {
Expand Down
10 changes: 9 additions & 1 deletion test/e2e/issues.ts
@@ -1,4 +1,4 @@
import { Liquid } from '../../src/liquid'
import { Liquid } from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as sinon from 'sinon'
Expand Down Expand Up @@ -153,4 +153,12 @@ describe('Issues', function () {
expect(exists).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')
})
})
2 changes: 1 addition & 1 deletion test/integration/builtin/filters/date.ts
Expand Up @@ -75,7 +75,7 @@ describe('filters/date', 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 () {
it('should work with `preserveTimezones`', 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)
})
Expand Down

0 comments on commit e2ef236

Please sign in to comment.