Skip to content

Commit 944384a

Browse files
[pickers] Fix DST issue with America/Asuncion timezone and AdapterMoment (@flaviendelangle) (#15653)
Co-authored-by: Flavien DELANGLE <[email protected]>
1 parent f812e20 commit 944384a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts

+11
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,25 @@ export class AdapterMoment implements MuiPickersAdapter<Moment, string> {
522522

523523
let count = 0;
524524
let current = start;
525+
let currentDayOfYear = current.get('dayOfYear');
525526
const nestedWeeks: Moment[][] = [];
526527

527528
while (current.isBefore(end)) {
528529
const weekNumber = Math.floor(count / 7);
529530
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
530531
nestedWeeks[weekNumber].push(current);
531532

533+
const prevDayOfYear = currentDayOfYear;
532534
current = this.addDays(current, 1);
535+
currentDayOfYear = current.get('dayOfYear');
536+
537+
// If there is a TZ change at midnight, adding 1 day may only increase the date by 23 hours to 11pm
538+
// To fix, bump the date into the next day (add 12 hours) and then revert to the start of the day
539+
// See https://github.com/moment/moment/issues/4743#issuecomment-811306874 for context.
540+
if (prevDayOfYear === currentDayOfYear) {
541+
current = current.add(12, 'h').startOf('day');
542+
}
543+
533544
count += 1;
534545
}
535546

packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ describe('<DateCalendar /> - Timezone', () => {
7272
).to.equal(30);
7373
});
7474

75+
// See https://github.com/mui/mui-x/issues/14730
76+
it('should not render duplicate days when leaving DST in America/Asuncion', () => {
77+
render(<DateCalendar timezone="America/Asuncion" value={adapter.date('2024-10-10')} />);
78+
expect(screen.getAllByRole('gridcell', { name: '5' })).to.have.length(1);
79+
});
80+
7581
TIMEZONE_TO_TEST.forEach((timezone) => {
7682
describe(`Timezone: ${timezone}`, () => {
7783
it('should use timezone prop for onChange when no value is provided', () => {

0 commit comments

Comments
 (0)