-
Notifications
You must be signed in to change notification settings - Fork 189
feat: render 12/24 hour format according to user's preference and add hourCycle option
#329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
e3a6c33
2942825
85a9943
96cb9c5
b2e15aa
e52f34b
343328c
2db2686
a2d1259
f5dd512
0f48a5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1982,6 +1982,7 @@ suite('relative-time', function () { | |
| const el = document.createElement('relative-time') | ||
| el.setAttribute('lang', 'es-ES') | ||
| el.setAttribute('time-zone', 'Europe/Madrid') | ||
| el.setAttribute('hour-cycle', 'h23') | ||
|
|
||
| el.setAttribute('datetime', '2023-01-15T17:00:00.000Z') | ||
| await Promise.resolve() | ||
|
|
@@ -2815,6 +2816,134 @@ suite('relative-time', function () { | |
| } | ||
| }) | ||
|
|
||
| suite('[hourCycle]', function () { | ||
| test('formats with 24-hour cycle when hour-cycle is h23', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('format', 'datetime') | ||
| el.setAttribute('hour', 'numeric') | ||
| el.setAttribute('minute', '2-digit') | ||
| el.setAttribute('hour-cycle', 'h23') | ||
| await Promise.resolve() | ||
| assert.notMatch(el.shadowRoot.textContent, /AM|PM/i) | ||
| assert.match(el.shadowRoot.textContent, /15:00/) | ||
| }) | ||
|
Comment on lines
+2820
to
+2831
|
||
|
|
||
| test('formats with 12-hour cycle when hour-cycle is h12', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('format', 'datetime') | ||
| el.setAttribute('hour', 'numeric') | ||
| el.setAttribute('minute', '2-digit') | ||
| el.setAttribute('hour-cycle', 'h12') | ||
| await Promise.resolve() | ||
| assert.match(el.shadowRoot.textContent, /3:00/) | ||
| assert.match(el.shadowRoot.textContent, /PM/i) | ||
| }) | ||
|
|
||
| test('formats with 12-hour cycle when hour-cycle is h11', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('format', 'datetime') | ||
| el.setAttribute('hour', 'numeric') | ||
| el.setAttribute('minute', '2-digit') | ||
| el.setAttribute('hour-cycle', 'h11') | ||
| await Promise.resolve() | ||
| assert.match(el.shadowRoot.textContent, /3:00/) | ||
| assert.match(el.shadowRoot.textContent, /PM/i) | ||
| }) | ||
|
|
||
| test('formats with 24-hour cycle when hour-cycle is h24', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('format', 'datetime') | ||
| el.setAttribute('hour', 'numeric') | ||
| el.setAttribute('minute', '2-digit') | ||
| el.setAttribute('hour-cycle', 'h24') | ||
| await Promise.resolve() | ||
| assert.notMatch(el.shadowRoot.textContent, /AM|PM/i) | ||
| }) | ||
silverwind marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test('title uses hour-cycle setting', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('hour-cycle', 'h23') | ||
| await Promise.resolve() | ||
| assert.notMatch(el.getAttribute('title'), /AM|PM/i) | ||
| assert.match(el.getAttribute('title'), /15/) | ||
| }) | ||
|
|
||
| test('inherits hour-cycle from ancestor', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('format', 'datetime') | ||
| el.setAttribute('hour', 'numeric') | ||
| el.setAttribute('minute', '2-digit') | ||
| const div = document.createElement('div') | ||
| div.setAttribute('hour-cycle', 'h23') | ||
| div.appendChild(el) | ||
| document.body.appendChild(div) | ||
| await Promise.resolve() | ||
| assert.notMatch(el.shadowRoot.textContent, /AM|PM/i) | ||
| assert.match(el.shadowRoot.textContent, /15:00/) | ||
| div.remove() | ||
| }) | ||
|
|
||
| test('inherits hour-cycle from documentElement', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('format', 'datetime') | ||
| el.setAttribute('hour', 'numeric') | ||
| el.setAttribute('minute', '2-digit') | ||
| document.documentElement.setAttribute('hour-cycle', 'h23') | ||
| await Promise.resolve() | ||
| assert.notMatch(el.shadowRoot.textContent, /AM|PM/i) | ||
| assert.match(el.shadowRoot.textContent, /15:00/) | ||
| document.documentElement.removeAttribute('hour-cycle') | ||
| }) | ||
|
|
||
| test('element attribute overrides ancestor', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('format', 'datetime') | ||
| el.setAttribute('hour', 'numeric') | ||
| el.setAttribute('minute', '2-digit') | ||
| el.setAttribute('hour-cycle', 'h12') | ||
| const div = document.createElement('div') | ||
| div.setAttribute('hour-cycle', 'h23') | ||
| div.appendChild(el) | ||
| document.body.appendChild(div) | ||
| await Promise.resolve() | ||
| assert.match(el.shadowRoot.textContent, /3:00/) | ||
| assert.match(el.shadowRoot.textContent, /PM/i) | ||
| div.remove() | ||
| }) | ||
|
|
||
| test('re-renders when hour-cycle attribute changes', async () => { | ||
| const el = document.createElement('relative-time') | ||
| el.setAttribute('datetime', '2020-01-01T15:00:00.000Z') | ||
| el.setAttribute('time-zone', 'UTC') | ||
| el.setAttribute('format', 'datetime') | ||
| el.setAttribute('hour', 'numeric') | ||
| el.setAttribute('minute', '2-digit') | ||
| el.setAttribute('hour-cycle', 'h12') | ||
| await Promise.resolve() | ||
| assert.match(el.shadowRoot.textContent, /PM/i) | ||
| el.setAttribute('hour-cycle', 'h23') | ||
| await Promise.resolve() | ||
| assert.notMatch(el.shadowRoot.textContent, /AM|PM/i) | ||
| assert.match(el.shadowRoot.textContent, /15:00/) | ||
| }) | ||
| }) | ||
|
|
||
| suite('[timeZone]', function () { | ||
| test('updates when the time-zone attribute is set', async () => { | ||
| const el = document.createElement('relative-time') | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.