diff --git a/docs/themes/navy/layout/partial/all-contributors.swig b/docs/themes/navy/layout/partial/all-contributors.swig index d73b9aacf3..2b7af66f7f 100644 --- a/docs/themes/navy/layout/partial/all-contributors.swig +++ b/docs/themes/navy/layout/partial/all-contributors.swig @@ -50,6 +50,7 @@ + diff --git a/src/builtin/filters/date.ts b/src/builtin/filters/date.ts index c4272ff66e..f46b2e0b49 100644 --- a/src/builtin/filters/date.ts +++ b/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)) { @@ -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()) } diff --git a/src/util/strftime.ts b/src/util/strftime.ts index ddf88b2faa..f3d87ce977 100644 --- a/src/util/strftime.ts +++ b/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', @@ -27,18 +42,18 @@ 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 @@ -46,16 +61,16 @@ function getWeekOfYear (d: Date, startDay: number) { 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) } @@ -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 @@ -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 @@ -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 diff --git a/src/util/timezone-date.ts b/src/util/timezone-date.ts index 68c9fb495c..ea1452cdf5 100644 --- a/src/util/timezone-date.ts +++ b/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() @@ -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! @@ -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') { diff --git a/test/e2e/issues.ts b/test/e2e/issues.ts index 199c120de3..f4bd002a47 100644 --- a/test/e2e/issues.ts +++ b/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' @@ -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') + }) }) diff --git a/test/integration/builtin/filters/date.ts b/test/integration/builtin/filters/date.ts index e80ace1ca1..425c7fe88e 100644 --- a/test/integration/builtin/filters/date.ts +++ b/test/integration/builtin/filters/date.ts @@ -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) })