fix(common): set correct timezone for ISO8601 dates in Safari (#21506)
Fixes #21491 PR Close #21506
This commit is contained in:
parent
bb62458566
commit
05208b8513
|
@ -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]);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue