-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: Reimplement timeformat.distanceToNow() with standard browser API
The only remaining usage of date-fns is formatting relative times. Its main advantage is proper locales support. However, by now `Intl.RelativeTimeFormat` [1] is available on all browsers. It does not provide the "pick appropriate unit" functionality, but that's easy enough to implement ourselves. However, keep special-casing distances of less than a minute like date-fns does: APIs/UI delays, and the fact that we don't re-probe things like uptime or snapshot creation times every second make it impractical to render precise seconds, and in most cases it's also uninteresting. Drop the `addSuffix` parameter. This mostly correlated with whether the distance is positive or negative (i.e. rendering a future or past time), and `RelativeTimeFormat()` does the distinction between "in ..." and "... ago" automatically. There is currently no portable "format duration" API in the browser [2], so we have to adjust our UI to get along with the grammar change. The only remaining caller with `addSuffix=false` is the Storage page's `format_delay()` for the JobsPanel, which is a future time and thus "in ... minutes" is fine. Note: The only other external consumers of this (c-ostree and c-machines) use `addSuffix: true`, and thus the new behaviour is API compatible -- the extra `true` will simply be ignored, and the result continues to be "... ago". This was the last consumer of date-fns. This reduces the bundle size of packagekit, sosreport, storage, and systemd by 128 kB each in production mode, and 3 MB each in development mode. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat Fixes #20653
- Loading branch information
1 parent
51ec0e7
commit cb251b6
Showing
8 changed files
with
76 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters