Skip to content

Commit 69bd240

Browse files
Merge pull request #321 from github/copilot/fix-320
Change month microformat from "m" to "mo" in narrow style formatting and fix micro format tense logic
2 parents 4d0059b + eeade77 commit 69bd240

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ This will used to determine the length of the unit names. This value is passed t
273273
| relative | narrow | in 1 mo. |
274274
| duration | long | 1 month, 2 days, 4 hours |
275275
| duration | short | 1 mth, 2 days, 4 hr |
276-
| duration | narrow | 1m 2d 4h |
276+
| duration | narrow | 1mo 2d 4h |
277277

278278
##### second, minute, hour, weekday, day, month, year, timeZoneName
279279

src/duration-format-ponyfill.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,15 @@ export default class DurationFormat {
110110
: unitStyle === 'numeric'
111111
? {}
112112
: {style: 'unit', unit: nfUnit, unitDisplay: unitStyle}
113-
list.push(new Intl.NumberFormat(locale, nfOpts).format(value))
113+
114+
let formattedValue = new Intl.NumberFormat(locale, nfOpts).format(value)
115+
116+
// Custom handling for narrow month formatting to use "mo" instead of "m"
117+
if (unit === 'months' && (unitStyle === 'narrow' || (style === 'narrow' && formattedValue.endsWith('m')))) {
118+
formattedValue = formattedValue.replace(/(\d+)m$/, '$1mo')
119+
}
120+
121+
list.push(formattedValue)
114122
}
115123
return new ListFormat(locale, {
116124
type: 'unit',

src/relative-time-element.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
170170
if (format === 'micro') {
171171
duration = roundToSingleUnit(duration)
172172
empty = microEmptyDuration
173-
if ((this.tense === 'past' && duration.sign !== -1) || (this.tense === 'future' && duration.sign !== 1)) {
173+
// Allow month-level durations to pass through even with mismatched tense
174+
if (
175+
duration.months === 0 &&
176+
((this.tense === 'past' && duration.sign !== -1) || (this.tense === 'future' && duration.sign !== 1))
177+
) {
174178
duration = microEmptyDuration
175179
}
176180
} else if ((tense === 'past' && duration.sign !== -1) || (tense === 'future' && duration.sign !== 1)) {

test/duration-format-ponyfill.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ suite('duration format ponyfill', function () {
3737
locale: 'en',
3838
style: 'narrow',
3939
parts: [
40-
{type: 'element', value: '1m'},
40+
{type: 'element', value: '1mo'},
4141
{type: 'literal', value: ' '},
4242
{type: 'element', value: '2d'},
4343
{type: 'literal', value: ' '},
@@ -92,6 +92,20 @@ suite('duration format ponyfill', function () {
9292
{type: 'element', value: '8s'},
9393
],
9494
},
95+
{
96+
duration: 'P1M2DT3M30S',
97+
locale: 'en',
98+
style: 'narrow',
99+
parts: [
100+
{type: 'element', value: '1mo'},
101+
{type: 'literal', value: ' '},
102+
{type: 'element', value: '2d'},
103+
{type: 'literal', value: ' '},
104+
{type: 'element', value: '3m'},
105+
{type: 'literal', value: ' '},
106+
{type: 'element', value: '30s'},
107+
],
108+
},
95109
])
96110

97111
for (const {duration, locale, parts, ...opts} of tests) {

test/relative-time.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,17 @@ suite('relative-time', function () {
561561
await Promise.resolve()
562562
assert.equal(time.shadowRoot.textContent, '1d')
563563
})
564+
565+
test('micro formats months', async () => {
566+
const datetime = new Date()
567+
datetime.setMonth(datetime.getMonth() - 2)
568+
const time = document.createElement('relative-time')
569+
time.setAttribute('tense', 'past')
570+
time.setAttribute('datetime', datetime)
571+
time.setAttribute('format', 'micro')
572+
await Promise.resolve()
573+
assert.equal(time.shadowRoot.textContent, '2mo')
574+
})
564575
})
565576

566577
suite('[tense=future]', function () {
@@ -1941,7 +1952,7 @@ suite('relative-time', function () {
19411952
datetime: '2022-09-24T14:46:00.000Z',
19421953
tense: 'future',
19431954
format: 'micro',
1944-
expected: '1m',
1955+
expected: '1mo',
19451956
},
19461957
{
19471958
datetime: '2022-10-23T14:46:00.000Z',
@@ -1991,7 +2002,7 @@ suite('relative-time', function () {
19912002
datetime: '2022-11-24T14:46:00.000Z',
19922003
tense: 'future',
19932004
format: 'micro',
1994-
expected: '1m',
2005+
expected: '1mo',
19952006
},
19962007
{
19972008
datetime: '2023-10-23T14:46:00.000Z',
@@ -2023,7 +2034,7 @@ suite('relative-time', function () {
20232034
datetime: '2022-11-24T14:46:00.000Z',
20242035
tense: 'past',
20252036
format: 'micro',
2026-
expected: '1m',
2037+
expected: '1mo',
20272038
},
20282039
{
20292040
datetime: '2022-10-25T14:46:00.000Z',
@@ -2073,7 +2084,7 @@ suite('relative-time', function () {
20732084
datetime: '2022-09-23T14:46:00.000Z',
20742085
tense: 'past',
20752086
format: 'micro',
2076-
expected: '1m',
2087+
expected: '1mo',
20772088
},
20782089
{
20792090
datetime: '2021-10-25T14:46:00.000Z',
@@ -2178,13 +2189,13 @@ suite('relative-time', function () {
21782189
{
21792190
datetime: '2021-10-30T14:46:00.000Z',
21802191
format: 'elapsed',
2181-
expected: '11m 29d',
2192+
expected: '11mo 29d',
21822193
},
21832194
{
21842195
datetime: '2021-10-30T14:46:00.000Z',
21852196
format: 'elapsed',
21862197
precision: 'month',
2187-
expected: '11m',
2198+
expected: '11mo',
21882199
},
21892200
{
21902201
datetime: '2021-10-29T14:46:00.000Z',

0 commit comments

Comments
 (0)