Skip to content
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

UI: rearrange logic for adding timezone in date-format helper #26693

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/26693.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: Remove possibility of returning an undefined timezone from date-format helper
```
12 changes: 9 additions & 3 deletions ui/lib/core/addon/helpers/date-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,21 @@ export function dateFormat([value, style], { withTimeZone = false }) {
return value || '';
}

const zone = withTimeZone ? formatTimeZone(date) : '';
return format(date, style) + zone;
}

// separate function for testing
export function formatTimeZone(date) {
let zone; // local timezone ex: 'PST'
try {
// passing undefined means default to the browser's locale
zone = ' ' + date.toLocaleTimeString(undefined, { timeZoneName: 'short' }).split(' ')[2];
Copy link
Contributor Author

@hellobontempo hellobontempo Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the space here so if toLocaleTimeString doesn't error but returns undefined again in the future for some reason, zone will be falsey

zone = date.toLocaleTimeString(undefined, { timeZoneName: 'short' }).split(' ')[2];
} catch (e) {
zone = '';
}
zone = withTimeZone ? zone : '';
return format(date, style) + zone;

return zone ? ` ${zone}` : '';
}

export default helper(dateFormat);
18 changes: 18 additions & 0 deletions ui/tests/integration/helpers/date-format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { find, render, settled } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { formatTimeZone } from 'core/helpers/date-format';

const TEST_DATE = new Date('2018-04-03T14:15:30');

Expand Down Expand Up @@ -68,4 +69,21 @@ module('Integration | Helper | date-format', function (hooks) {
await settled();
assert.dom(this.element).hasText('', 'renders empty string when falsey');
});

test('it formats timezone', async function (assert) {
assert.strictEqual(formatTimeZone('not a date'), '', 'returns an empty string for a non-date value');

// compute expected because we can't reliably assume timezone of the test suite
const expected = new Intl.DateTimeFormat('en', { timeZoneName: 'shortOffset' })
.format(TEST_DATE)
.split(',')[1];

const formatted = formatTimeZone(TEST_DATE);

assert.strictEqual(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we add one more assertion, since the expected value is computed, that makes sure the zone is NOT undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! Good call ⚡

formatted,
expected,
`formatted timezone: "${formatted}" equals expected: "${expected}"`
);
});
});
Loading