Skip to content

Commit

Permalink
fix(datetime): display time in user's timezone after selection (#25694)
Browse files Browse the repository at this point in the history
Resolves #25693
  • Loading branch information
sean-perkins authored Aug 1, 2022
1 parent 86b7000 commit 11c69c8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
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,
})
)
);
};

/**
Expand Down

0 comments on commit 11c69c8

Please sign in to comment.