From 9a0f42f4f5ebf5866daa3875a5e65de0b430eb35 Mon Sep 17 00:00:00 2001
From: Keith Cirkel
Date: Wed, 16 Nov 2022 17:03:05 +0000
Subject: [PATCH] add elapsed time format
---
README.md | 76 ++++++++++++++++++++++++------------
examples/index.html | 35 ++++++++++++-----
src/duration-format.ts | 21 +++++++++-
src/relative-time-element.ts | 42 +++++++++++++++-----
test/relative-time.js | 22 ++++++++++-
5 files changed, 150 insertions(+), 46 deletions(-)
diff --git a/README.md b/README.md
index a037dd0..1ba2ef0 100644
--- a/README.md
+++ b/README.md
@@ -69,8 +69,10 @@ So, a relative date phrase is used for up to a month and then the actual date is
| Property Name | Attribute Name | Possible Values | Default Value |
|:---------------|:-----------------|:-----------------------------------------------------------------------------------------|:-----------------------|
| `datetime` | `datetime` | `string` | - |
+| `format` | `format` | `'auto'|'micro'|'elapsed'|string` | 'auto' |
| `date` | - | `Date \| null` | - |
| `tense` | `tense` | `'auto'\|'past'\|'future'` | `'auto'` |
+| `precision` | `precision` | `'year'|'month'|'day'|'hour'|'minute'|'second'` | `'second'` |
| `threshold` | `threshold` | `string` | `'P30D'` |
| `prefix` | `prefix` | `string` | `'on'` |
| `second` | `second` | `'numeric'\|'2-digit'\|undefined` | `undefined` |
@@ -100,6 +102,39 @@ This is the datetime that the element is meant to represent. This must be a vali
```
+##### format (`'auto'|'micro'|'elapsed'|string`, default: `'auto'`)
+
+The default format is `auto`, but this can be changed to `micro` or `elapsed`.
+
+The default `auto` format will display dates relative to the current time (unless they are past the `threshold` value - see below). The values are rounded to display a single unit, for example if the time between the given `datetime` and the current wall clock time exceeds a day, then the format will _only_ ouput in days, and will not display hours, minutes or seconds.
+
+The `micro` format which will display relative dates (within the threshold) in a more compact format. Similar to `auto`, the `micro` format rounds values to the nearest largest value. Additionally, `micro` format will not round _lower_ than 1 minute, as such a `datetime` which is less than a minute from the current wall clock time will display `'1m'`.
+
+The `elapsed` format will always non-rounded units of time, where any non-zero unit of time is displayed in the compact notation format. This format is also absolute, and so tense does not apply. This can be useful for displaying actively running timers.
+
+| `format=auto` | `format=micro` | `format=elapsed` |
+|:-------------:|:--------------:|:----------------:|
+| in 2 years | 2y | 2y 10d 3h 20m 8s |
+| 2 years ago | 2y | 2y 10d 3h 20m 8s |
+| in 30 days | 30d | 30d 4h 20m 8s |
+| 21 minutes ago| 21m | 21m 30s |
+| 37 seconds ago| 1m | 37s |
+
+Additionally, format accepts a [strftime](https://strftime.org/) compatible format. Providing a strftime format will override all other attributes on the element, and the time will be displayed formatted based on the strftime value. This can be useful, for example, to dynamically remove relative formatting based on a user action.
+
+```html
+
+
+
+```
+
+```html
+
+
+
+```
+
+
##### tense (`'auto'|'past'|'future'`, default: `auto`)
If `format` is anything other than `'auto'` or `'micro'` then this value will be ignored.
@@ -118,6 +153,21 @@ Tense can be used to fix relative-time to always display a date's relative tense
```
+#### precision (`'year'|'month'|'day'|'hour'|'minute'|'second'`, default: `'second'`)
+
+If `format` is anything other than `'elapsed'` then this value will be ignored.
+
+Precision can be used to limit the display of an `elapsed` formatted time. By default the `elapsed` time will display times down to the `second` level of precision. Changing this value will truncate the display to the respective unit, and smaller units will be elided. Some examples:
+
+| `precision=` | Display |
+|:-------------:|:-------------------:|
+| seconds | 2y 6m 10d 3h 20m 8s |
+| minutes | 2y 6m 10d 3h 20m |
+| hours | 2y 6m 10d 3h |
+| days | 2y 6m 10d |
+| months | 2y 6m |
+| years | 2y |
+
##### threshold (`string`, default: `P30D`)
If `tense` is anything other than `'auto'` or `format` is anything other than `'auto'` or `'micro'` then this value will be ignored.
@@ -154,32 +204,6 @@ When formatting an absolute date (see above `threshold` for more details) it can
For dates outside of the specified `threshold`, the formatting of the date can be configured using these attributes. The values for these attributes are passed to [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat):
-##### format (`'auto'|'micro'|string`, default: `'auto'`)
-
-The default format is `auto`, but this can be changed to `micro` which will display relative dates (within the threshold) in a more compact format. Micro format will not display relative times shorter than 1 minute (`'1m'`). Some examples:
-
-| `format=auto` | `format=micro` |
-|:-------------:|:--------------:|
-| in 2 years | 2y |
-| 2 yars ago | 2y |
-| in 30 days | 30d |
-| 21 minutes ago| 21m |
-| 37 seconds ago| 1m |
-
-Additionally, format accepts a [strftime](https://strftime.org/) compatible format. Providing a strftime format will override all other attributes on the element, and the time will be displayed formatted based on the strftime value. This can be useful, for example, to dynamically remove relative formatting based on a user action.
-
-```html
-
-
-
-```
-
-```html
-
-
-
-```
-
##### lang
Lang is a [built-in global attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang). Relative Time will use this to provide an applicable language to the `Intl` APIs. If the individual element does not have a `lang` attribute then it will traverse upwards in the tree to find the closest element that does, or default the lang to `en`.
diff --git a/examples/index.html b/examples/index.html
index af52d4f..f0ba169 100644
--- a/examples/index.html
+++ b/examples/index.html
@@ -82,6 +82,22 @@ Future Date
+
+ Time until:
+
+ Oops! This browser doesn't support Web Components.
+
+
+
+
+ Time until (micro format):
+
+ Oops! This browser doesn't support Web Components.
+
+
+
+
+ Dynamic Formats
Nearby, dynamic, relative time:
@@ -89,6 +105,7 @@ Future Date
+
Nearby, dynamic, relative time:
@@ -103,23 +120,22 @@ Future Date
-
-
- Time until:
-
+ How long you've been on this page
+
Oops! This browser doesn't support Web Components.
-
+
- Time until (micro format):
-
+ Countdown timer until 2038 bug:
+
Oops! This browser doesn't support Web Components.
-
-
+
+
+