Skip to content

Commit

Permalink
fix(common): format day-periods that cross midnight (#36611)
Browse files Browse the repository at this point in the history
When formatting a time with the `b` or `B` format codes, the rendered
string was not correctly handling day periods that spanned midnight.
Instead the logic was falling back to the default case of `AM`.

Now the logic has been updated so that it matches times that are within
a day period that spans midnight, so it will now render the correct
output, such as `at night` in the case of English.

Applications that are using either `formatDate()` or `DatePipe` and any
of the `b` or `B` format codes will be affected by this change.

Fixes #36566

PR Close #36611
  • Loading branch information
petebacondarwin authored and atscott committed Apr 16, 2020
1 parent 793a001 commit 1756cce
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
40 changes: 27 additions & 13 deletions packages/common/src/i18n/format_date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,40 @@ function getDateTranslation(
if (extended) {
const rules = getLocaleExtraDayPeriodRules(locale);
const dayPeriods = getLocaleExtraDayPeriods(locale, form, width);
let result;
rules.forEach((rule: Time|[Time, Time], index: number) => {
const index = rules.findIndex(rule => {
if (Array.isArray(rule)) {
// morning, afternoon, evening, night
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];
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;
}
} else { // noon or midnight
const {hours, minutes} = rule;
if (hours === currentHours && minutes === currentMinutes) {
result = dayPeriods[index];
if (rule.hours === currentHours && rule.minutes === currentMinutes) {
return true;
}
}
return false;
});
if (result) {
return result;
if (index !== -1) {
return dayPeriods[index];
}
}
// if no rules for the day periods, we use am/pm by default
Expand Down
18 changes: 18 additions & 0 deletions packages/common/test/i18n/format_date_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,31 @@ 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]);
});

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', () => {
Expand Down

0 comments on commit 1756cce

Please sign in to comment.