revert: fix(common): format day-periods that cross midnight (#36611) (#36751)

This reverts commit 1756cced4a59bc1185653c6c9b8f661636fd6116.

The reason for this revert is because of the change being too risky for
a patch release of Angular. Changing date formatting proves to be a
breaking change and can only be apart of the next release.

PR Close #36751
This commit is contained in:
Matias Niemelä 2020-04-21 19:18:24 -07:00
parent b9b9cc2ba8
commit 8bd5374cfd
2 changed files with 13 additions and 45 deletions

View File

@ -277,40 +277,26 @@ function getDateTranslation(
if (extended) {
const rules = getLocaleExtraDayPeriodRules(locale);
const dayPeriods = getLocaleExtraDayPeriods(locale, form, width);
const index = rules.findIndex(rule => {
let result;
rules.forEach((rule: Time|[Time, Time], index: number) => {
if (Array.isArray(rule)) {
// morning, afternoon, evening, night
const [from, to] = rule;
const afterFrom = currentHours >= from.hours && currentMinutes >= from.minutes;
const beforeTo =
(currentHours < to.hours ||
(currentHours === to.hours && currentMinutes < to.minutes));
// We must account for normal rules that span a period during the day (e.g. 6am-9am)
// where `from` is less (earlier) than `to`. But also rules that span midnight (e.g.
// 10pm - 5am) where `from` is greater (later!) than `to`.
//
// In the first case the current time must be BOTH after `from` AND before `to`
// (e.g. 8am is after 6am AND before 10am).
//
// In the second case the current time must be EITHER after `from` OR before `to`
// (e.g. 4am is before 5am but not after 10pm; and 11pm is not before 5am but it is
// after 10pm).
if (from.hours < to.hours) {
if (afterFrom && beforeTo) {
return true;
}
} else if (afterFrom || beforeTo) {
return true;
const {hours: hoursFrom, minutes: minutesFrom} = rule[0];
const {hours: hoursTo, minutes: minutesTo} = rule[1];
if (currentHours >= hoursFrom && currentMinutes >= minutesFrom &&
(currentHours < hoursTo ||
(currentHours === hoursTo && currentMinutes < minutesTo))) {
result = dayPeriods[index];
}
} else { // noon or midnight
if (rule.hours === currentHours && rule.minutes === currentMinutes) {
return true;
const {hours, minutes} = rule;
if (hours === currentHours && minutes === currentMinutes) {
result = dayPeriods[index];
}
}
return false;
});
if (index !== -1) {
return dayPeriods[index];
if (result) {
return result;
}
}
// if no rules for the day periods, we use am/pm by default

View File

@ -202,19 +202,6 @@ describe('Format date', () => {
BBBBB: 'mi',
};
const midnightCrossingPeriods: any = {
b: 'night',
bb: 'night',
bbb: 'night',
bbbb: 'night',
bbbbb: 'night',
B: 'at night',
BB: 'at night',
BBB: 'at night',
BBBB: 'at night',
BBBBB: 'at night',
};
Object.keys(dateFixtures).forEach((pattern: string) => {
expectDateFormatAs(date, pattern, dateFixtures[pattern]);
});
@ -222,11 +209,6 @@ describe('Format date', () => {
Object.keys(isoStringWithoutTimeFixtures).forEach((pattern: string) => {
expectDateFormatAs(isoStringWithoutTime, pattern, isoStringWithoutTimeFixtures[pattern]);
});
const nightTime = new Date(2015, 5, 15, 2, 3, 1, 550);
Object.keys(midnightCrossingPeriods).forEach(pattern => {
expectDateFormatAs(nightTime, pattern, midnightCrossingPeriods[pattern]);
});
});
it('should format with timezones', () => {