Bug 1789905 - add 'week' support to mozIntl's formatbestUnit code, r=jfkthame

Differential Revision: https://phabricator.services.mozilla.com/D157162
This commit is contained in:
Gijs Kruitbosch 2022-09-15 07:53:17 +00:00
Родитель 7e5e4979c4
Коммит c7b7551a00
2 изменённых файлов: 26 добавлений и 32 удалений

Просмотреть файл

@ -70,7 +70,7 @@ function defineCachedGetter(obj, prop, get) {
* @param {Function} get - Function that will be used as a getter.
*/
function defineGetter(obj, prop, get) {
Object.defineProperty(obj, prop, { get });
Object.defineProperty(obj, prop, { get, enumerable: true });
}
/**
@ -122,10 +122,10 @@ function bestFit(absDiff) {
switch (true) {
case absDiff.years > 0 && absDiff.months > threshold.month:
return "year";
case absDiff.months > 0 && absDiff.days > threshold.day:
case absDiff.months > 0 && absDiff.weeks > threshold.week:
return "month";
// case absDiff.months > 0 && absDiff.weeks > threshold.week: return "month";
// case absDiff.weeks > 0 && absDiff.days > threshold.day: return "week";
case absDiff.weeks > 0 && absDiff.days > threshold.day:
return "week";
case absDiff.days > 0 && absDiff.hours > threshold.hour:
return "day";
case absDiff.hours > 0 && absDiff.minutes > threshold.minute:
@ -144,8 +144,8 @@ function bestFit(absDiff) {
*/
const threshold = {
month: 2, // at least 2 months before using year.
// week: 4, // at least 4 weeks before using month.
day: 6, // at least 6 days before using month.
week: 3, // at least 3 weeks before using month.
day: 6, // at least 6 days before using week.
hour: 6, // at least 6 hours before using day.
minute: 59, // at least 59 minutes before using hour.
second: 59, // at least 59 seconds before using minute.
@ -776,6 +776,9 @@ class MozRelativeTimeFormat extends Intl.RelativeTimeFormat {
defineCachedGetter(diff, "months", function() {
return this.years * 12 + date.getMonth() - now.getMonth();
});
defineCachedGetter(diff, "weeks", function() {
return Math.trunc(this.days / 7);
});
defineCachedGetter(diff, "days", function() {
return Math.trunc((startOf(date, "day") - startOf(now, "day")) / day);
});
@ -797,25 +800,11 @@ class MozRelativeTimeFormat extends Intl.RelativeTimeFormat {
_: {},
};
defineGetter(absDiff, "years", function() {
return Math.abs(diff.years);
});
defineGetter(absDiff, "months", function() {
return Math.abs(diff.months);
});
defineGetter(absDiff, "days", function() {
return Math.abs(diff.days);
});
defineGetter(absDiff, "hours", function() {
return Math.abs(diff.hours);
});
defineGetter(absDiff, "minutes", function() {
return Math.abs(diff.minutes);
});
defineGetter(absDiff, "seconds", function() {
return Math.abs(diff.seconds);
});
for (let prop of Object.keys(diff)) {
defineGetter(absDiff, prop, function() {
return Math.abs(diff[prop]);
});
}
const unit = bestFit(absDiff);
switch (unit) {
@ -823,6 +812,8 @@ class MozRelativeTimeFormat extends Intl.RelativeTimeFormat {
return this.format(diff.years, unit);
case "month":
return this.format(diff.months, unit);
case "week":
return this.format(diff.weeks, unit);
case "day":
return this.format(diff.days, unit);
case "hour":

Просмотреть файл

@ -98,12 +98,15 @@ function test_rtf_formatBestUnit() {
{
// format days-distant dates
let anchor = new Date("2016-04-10 12:00:00");
testRTFBestUnit(anchor, "2016-04-01 00:00", "9 days ago");
testRTFBestUnit(anchor, "2016-04-01 00:00", "last week");
testRTFBestUnit(anchor, "2016-04-05 00:00", "5 days ago");
testRTFBestUnit(anchor, "2016-04-09 18:00", "yesterday");
testRTFBestUnit(anchor, "2016-04-11 09:00", "tomorrow");
testRTFBestUnit(anchor, "2016-04-30 23:59", "in 20 days");
testRTFBestUnit(anchor, "2016-03-31 23:59", "last month");
testRTFBestUnit(anchor, "2016-05-01 00:00", "next month");
testRTFBestUnit(anchor, "2016-04-30 23:59", "in 2 weeks");
testRTFBestUnit(anchor, "2016-03-31 23:59", "last week");
testRTFBestUnit(anchor, "2016-04-18 23:59", "next week");
testRTFBestUnit(anchor, "2016-03-03 23:59", "last month");
testRTFBestUnit(anchor, "2016-05-12 00:00", "next month");
anchor = new Date("2016-04-06 12:00");
testRTFBestUnit(anchor, "2016-03-31 23:59", "6 days ago");
@ -117,13 +120,13 @@ function test_rtf_formatBestUnit() {
let anchor = new Date("2016-04-10 12:00:00");
testRTFBestUnit(anchor, "2016-01-01 00:00", "3 months ago");
testRTFBestUnit(anchor, "2016-03-01 00:00", "last month");
testRTFBestUnit(anchor, "2016-05-01 00:00", "next month");
testRTFBestUnit(anchor, "2016-05-11 00:00", "next month");
testRTFBestUnit(anchor, "2016-12-01 23:59", "in 8 months");
anchor = new Date("2017-01-12 18:30");
testRTFBestUnit(anchor, "2016-12-29 18:30", "last month");
testRTFBestUnit(anchor, "2016-12-14 18:30", "last month");
anchor = new Date("2016-12-29 18:30");
anchor = new Date("2016-12-14 18:30");
testRTFBestUnit(anchor, "2017-01-12 18:30", "next month");
anchor = new Date("2016-02-28 12:00");