Skip to content

Commit e3ecfe3

Browse files
wyoziharttle
authored andcommitted
fix: simpler timezone regex and non-null offset
1 parent c16c787 commit e3ecfe3

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

src/util/strftime.ts

+11-16
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ const formatCodes = {
142142

143143
export default function (inputDate: Date, formatStr: string) {
144144
let d = inputDate
145-
if (d instanceof TimezoneDate && d.timezoneOffset !== null) {
146-
d = new Date((+d) + new Date().getTimezoneOffset() * 60 * 1000 + d.timezoneOffset * 60 * 1000)
145+
if (d instanceof TimezoneDate) {
146+
d = new Date((+d) + d.inputTimezoneOffset * 60 * 1000)
147147
}
148148

149149
let output = ''
@@ -175,24 +175,19 @@ function format (d: Date, match: RegExpExecArray) {
175175
}
176176

177177
export class TimezoneDate extends Date {
178-
ISO8601_PATTERN = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
179-
ISO8601_OFFSET_PATTERN = /^([+-])(\d{2}):(\d{2})$/;
178+
ISO8601_TIMEZONE_PATTERN = /([zZ]|([+-])(\d{2}):(\d{2}))$/;
180179

181-
timezoneOffset: number | null = null;
180+
inputTimezoneOffset: number = 0;
182181

183182
constructor (public dateString: string) {
184183
super(dateString)
185-
const m = dateString.match(this.ISO8601_PATTERN)
186-
const tzString = m && m[21]
187-
if (tzString) {
188-
if (tzString === 'Z') {
189-
this.timezoneOffset = 0
190-
} else {
191-
const m2 = tzString.match(this.ISO8601_OFFSET_PATTERN)
192-
if (m2) {
193-
this.timezoneOffset = (m2[1] === '+' ? 1 : -1) * (parseInt(m2[2], 10) * 60 + parseInt(m2[3], 10))
194-
}
195-
}
184+
const m = dateString.match(this.ISO8601_TIMEZONE_PATTERN)
185+
if (m && m[1] === 'Z') {
186+
this.inputTimezoneOffset = this.getTimezoneOffset()
187+
} else if (m && m[2] && m[3] && m[4]) {
188+
const [, , sign, hours, minutes] = m;
189+
const delta = (sign === '+' ? 1 : -1) * (parseInt(hours, 10) * 60 + parseInt(minutes, 10))
190+
this.inputTimezoneOffset = this.getTimezoneOffset() + delta
196191
}
197192
}
198193
}

test/integration/builtin/filters/date.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ describe('filters/date', function () {
1111
it('should create a new Date when given "today"', function () {
1212
return test('{{ "today" | date: "%Y"}}', (new Date()).getFullYear().toString())
1313
})
14-
it('should parse as Date when given UTC string', function () {
14+
it('should parse as Date when given a timezoneless string', function () {
1515
return test('{{ "1991-02-22T00:00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1991-02-22T00:00:00')
1616
})
1717
it('should not change the timezone between input and output', function () {
1818
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00')
1919
})
20-
it('should apply numeric offset', function () {
20+
it('should apply numeric timezone offset (0)', function () {
2121
return test('{{ "1990-12-31T23:00:00+00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00')
2222
})
23-
it('should apply numeric offset', function () {
23+
it('should apply numeric timezone offset (-1)', function () {
2424
return test('{{ "1990-12-31T23:00:00-01:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00')
2525
})
26-
it('should apply numeric offset', function () {
26+
it('should apply numeric timezone offset (+2.30)', function () {
2727
return test('{{ "1990-12-31T23:00:00+02:30" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00')
2828
})
2929
it('should render string as string if not valid', function () {

0 commit comments

Comments
 (0)