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

fix(datetime): display time in user's timezone after selection #25694

Merged
merged 5 commits into from
Aug 1, 2022
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
57 changes: 57 additions & 0 deletions core/src/components/datetime/test/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
addTimePadding,
getMonthAndYear,
getLocalizedDayPeriod,
getLocalizedTime,
} from '../utils/format';

describe('generateDayAriaLabel()', () => {
Expand Down Expand Up @@ -99,3 +100,59 @@ describe('getLocalizedDayPeriod', () => {
expect(getLocalizedDayPeriod('en-US', 'pm'));
});
});

describe('getLocalizedTime', () => {
describe('with a timezone offset', () => {
it('should ignore the offset and localize the time to PM', () => {
const datetimeParts = {
day: 1,
month: 1,
year: 2022,
hour: 13,
minute: 40,
tzOffset: -240,
};

expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('1:40 PM');
});

it('should ignore the offset and localize the time to AM', () => {
const datetimeParts = {
day: 1,
month: 1,
year: 2022,
hour: 9,
minute: 40,
tzOffset: -240,
};

expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('9:40 AM');
});
});

it('should localize the time to PM', () => {
const datetimeParts = {
day: 1,
month: 1,
year: 2022,
hour: 13,
minute: 40,
tzOffset: 0,
};

expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('1:40 PM');
});

it('should localize the time to AM', () => {
const datetimeParts = {
day: 1,
month: 1,
year: 2022,
hour: 9,
minute: 40,
tzOffset: 0,
};

expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('9:40 AM');
});
});
10 changes: 9 additions & 1 deletion core/src/components/datetime/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ export const getLocalizedTime = (locale: string, refParts: DatetimeParts, use24H
minute: 'numeric',
timeZone: 'UTC',
hour12: !use24Hour,
}).format(new Date(convertDataToISO(refParts)));
}).format(
new Date(
convertDataToISO({
...refParts,
// TODO: FW-1831 will remove the need to manually set the tzOffset to undefined
tzOffset: undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

When fixing FW-1831, would this issue also get resolved? The solution for that issue may be to remove any timezone processing from convertDataToISO. If this issue would get resolved, it might be worth adding a TODO to make sure we remove this additional line since it would be redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, should be resolved by any changes to convertDataToISO that ignores the timezone offset. I'll update to include the TODO.

})
)
);
};

/**
Expand Down