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 date: Date;
|
||||||
|
let match: RegExpMatchArray|null;
|
||||||
if (isDate(value)) {
|
if (isDate(value)) {
|
||||||
date = value;
|
date = value;
|
||||||
} else if (!isNaN(value - parseFloat(value))) {
|
} else if (!isNaN(value - parseFloat(value))) {
|
||||||
@ -158,18 +159,15 @@ export class DatePipe implements PipeTransform {
|
|||||||
*/
|
*/
|
||||||
const [y, m, d] = value.split('-').map((val: string) => +val);
|
const [y, m, d] = value.split('-').map((val: string) => +val);
|
||||||
date = new Date(y, m - 1, d);
|
date = new Date(y, m - 1, d);
|
||||||
|
} else if ((typeof value === 'string') && (match = value.match(ISO8601_DATE_REGEX))) {
|
||||||
|
date = isoStringToDate(match);
|
||||||
} else {
|
} else {
|
||||||
date = new Date(value);
|
date = new Date(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isDate(date)) {
|
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);
|
return formatDate(date, format, locale || this.locale, timezone);
|
||||||
}
|
}
|
||||||
@ -180,9 +178,12 @@ export function isoStringToDate(match: RegExpMatchArray): Date {
|
|||||||
const date = new Date(0);
|
const date = new Date(0);
|
||||||
let tzHour = 0;
|
let tzHour = 0;
|
||||||
let tzMin = 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 dateSetter = match[8] ? date.setUTCFullYear : date.setFullYear;
|
||||||
const timeSetter = match[8] ? date.setUTCHours : date.setHours;
|
const timeSetter = match[8] ? date.setUTCHours : date.setHours;
|
||||||
|
|
||||||
|
// if there is a timezone defined like "+01:00" or "+0100"
|
||||||
if (match[9]) {
|
if (match[9]) {
|
||||||
tzHour = +(match[9] + match[10]);
|
tzHour = +(match[9] + match[10]);
|
||||||
tzMin = +(match[9] + match[11]);
|
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'))
|
() => expect(pipe.transform('2017-05-07T22:14:39', 'dd-MM-yyyy HH:mm'))
|
||||||
.toMatch(/07-05-2017 \d{2}:\d{2}/));
|
.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:
|
// test for the following bugs:
|
||||||
// https://github.com/angular/angular/issues/16624
|
// https://github.com/angular/angular/issues/16624
|
||||||
// https://github.com/angular/angular/issues/17478
|
// https://github.com/angular/angular/issues/17478
|
||||||
|
Loading…
x
Reference in New Issue
Block a user