github / relative-time-element

Web component extensions to the standard <time> element.
https://github.github.io/relative-time-element/examples/
MIT License
3.53k stars 171 forks source link

update rounding rules for months/years in Duration #245

Closed keithamus closed 1 year ago

keithamus commented 1 year ago

Currently the main release of relative time uses a 90% rounding rule for dates.

Relative times are colloquial and so every user interprets them in subtly different ways. There are a few techniques we have used in the past:

Right now the 90% threshold is our latest iteration but it has problems with large units of time, chiefly months and years. At the time of writing, for example, dates from April 2021 will be marked as "last year" despite being much closer to "2 years ago".

This PR changes the logic just for months and years to use a similar mechanism to that described in Temporal.Duration.round. We take the relativeTo which is a date, and we execute calendar math on the date object - working out the delta of the current month and rounding based on calendar year instead of a rounding of months.

This means for dates that have a delta of months or years, they will perform calendar operations, and those dates will be relative to the calendar year. In other words a date from 2022 read in 2023 will only ever read as "last year" and not "2 years ago", meanwhile a date from 2021 read in 2023 will never say "last year" and will always say "2 years ago".

primer-css commented 1 year ago

:wave: Hello and thanks for pinging us! This issue or PR has been added to our inbox and a Design Infrastructure first responder will review it soon.