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

Resolves #25693
This commit is contained in:
Sean Perkins
2022-08-01 16:22:56 -04:00
committed by GitHub
parent 86b7000bcd
commit 11c69c8df5
2 changed files with 66 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import {
addTimePadding,
getMonthAndYear,
getLocalizedDayPeriod,
getLocalizedTime,
} from '../utils/format';
describe('generateDayAriaLabel()', () => {
@ -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');
});
});

View File

@ -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,
})
)
);
};
/**