From 05208b85133cd9e18b04b4f81e833c0ea5b03ba9 Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Fri, 12 Jan 2018 16:05:41 +0100 Subject: [PATCH] fix(common): set correct timezone for ISO8601 dates in Safari (#21506) Fixes #21491 PR Close #21506 --- packages/common/src/pipes/date_pipe.ts | 13 +++++++------ packages/common/test/pipes/date_pipe_spec.ts | 10 ++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/common/src/pipes/date_pipe.ts b/packages/common/src/pipes/date_pipe.ts index 1b9a532a72..2e2c1ededc 100644 --- a/packages/common/src/pipes/date_pipe.ts +++ b/packages/common/src/pipes/date_pipe.ts @@ -142,6 +142,7 @@ export class DatePipe implements PipeTransform { } let date: Date; + let match: RegExpMatchArray|null; if (isDate(value)) { date = value; } else if (!isNaN(value - parseFloat(value))) { @@ -158,17 +159,14 @@ export class DatePipe implements PipeTransform { */ const [y, m, d] = value.split('-').map((val: string) => +val); date = new Date(y, m - 1, d); + } else if ((typeof value === 'string') && (match = value.match(ISO8601_DATE_REGEX))) { + date = isoStringToDate(match); } else { date = new Date(value); } if (!isDate(date)) { - let match: RegExpMatchArray|null; - if ((typeof value === 'string') && (match = value.match(ISO8601_DATE_REGEX))) { - date = isoStringToDate(match); - } else { - throw invalidPipeArgumentError(DatePipe, value); - } + throw invalidPipeArgumentError(DatePipe, value); } return formatDate(date, format, locale || this.locale, timezone); @@ -180,9 +178,12 @@ export function isoStringToDate(match: RegExpMatchArray): Date { const date = new Date(0); let tzHour = 0; let tzMin = 0; + + // match[8] means that the string contains "Z" (UTC) or a timezone like "+01:00" or "+0100" const dateSetter = match[8] ? date.setUTCFullYear : date.setFullYear; const timeSetter = match[8] ? date.setUTCHours : date.setHours; + // if there is a timezone defined like "+01:00" or "+0100" if (match[9]) { tzHour = +(match[9] + match[10]); tzMin = +(match[9] + match[11]); diff --git a/packages/common/test/pipes/date_pipe_spec.ts b/packages/common/test/pipes/date_pipe_spec.ts index 93f549dcaf..82e5df9154 100644 --- a/packages/common/test/pipes/date_pipe_spec.ts +++ b/packages/common/test/pipes/date_pipe_spec.ts @@ -275,6 +275,16 @@ import localeTh from '@angular/common/locales/th'; () => expect(pipe.transform('2017-05-07T22:14:39', 'dd-MM-yyyy HH:mm')) .toMatch(/07-05-2017 \d{2}:\d{2}/)); + // test for issue https://github.com/angular/angular/issues/21491 + it('should not assume UTC for iso strings in Safari if the timezone is not defined', () => { + // this test only works if the timezone is not in UTC + // which is the case for BrowserStack when we test Safari + if (new Date().getTimezoneOffset() !== 0) { + expect(pipe.transform('2018-01-11T13:00:00', 'HH')) + .not.toEqual(pipe.transform('2018-01-11T13:00:00Z', 'HH')); + } + }); + // test for the following bugs: // https://github.com/angular/angular/issues/16624 // https://github.com/angular/angular/issues/17478