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 all 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);
12 changes: 12 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 @@ -41,6 +42,7 @@ module('Integration | Helper | date-format', function (hooks) {
});

test('displays time zone if withTimeZone=true', async function (assert) {
// this test may fail locally if you're in a non-US timezone
this.set('withTimezone', true);
this.set('timestampDate', TEST_DATE);

Expand Down Expand Up @@ -68,4 +70,14 @@ 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) {
// compute expected because otherwise this fails locally because of differing timezones
const expected = ` ${TEST_DATE.toLocaleTimeString(undefined, { timeZoneName: 'short' }).split(' ')[2]}`;
const actual = formatTimeZone(TEST_DATE);

assert.notStrictEqual(actual, undefined, 'formatted timezone is not undefined');
assert.strictEqual(formatTimeZone('not a date'), '', 'returns an empty string for a non-date value');
assert.strictEqual(actual, expected, `formatted timezone: "${actual}" equals expected: "${expected}"`);
});
});
Loading