From 1dd80e38102d2fcdb0e50e63ce310d360ec045a0 Mon Sep 17 00:00:00 2001 From: Flavien DELANGLE Date: Thu, 28 Nov 2024 14:30:52 +0100 Subject: [PATCH] [pickers] Fix DST issue with `America/Asuncion` timezone and `AdapterMoment` (#15552) --- .../x-date-pickers/src/AdapterMoment/AdapterMoment.ts | 11 +++++++++++ .../DateCalendar/tests/timezone.DateCalendar.test.tsx | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts b/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts index 426d0404f487..ae2a19e4dc20 100644 --- a/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts +++ b/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts @@ -522,6 +522,7 @@ export class AdapterMoment implements MuiPickersAdapter { let count = 0; let current = start; + let currentDayOfYear = current.get('dayOfYear'); const nestedWeeks: Moment[][] = []; while (current.isBefore(end)) { @@ -529,7 +530,17 @@ export class AdapterMoment implements MuiPickersAdapter { nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || []; nestedWeeks[weekNumber].push(current); + const prevDayOfYear = currentDayOfYear; current = this.addDays(current, 1); + currentDayOfYear = current.get('dayOfYear'); + + // If there is a TZ change at midnight, adding 1 day may only increase the date by 23 hours to 11pm + // To fix, bump the date into the next day (add 12 hours) and then revert to the start of the day + // See https://github.com/moment/moment/issues/4743#issuecomment-811306874 for context. + if (prevDayOfYear === currentDayOfYear) { + current = current.add(12, 'h').startOf('day'); + } + count += 1; } diff --git a/packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx b/packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx index a8624667be44..5e40c52b9286 100644 --- a/packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx +++ b/packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx @@ -72,6 +72,12 @@ describe(' - Timezone', () => { ).to.equal(30); }); + // See https://github.com/mui/mui-x/issues/14730 + it('should not render duplicate days when leaving DST in America/Asuncion', () => { + render(); + expect(screen.getAllByRole('gridcell', { name: '5' })).to.have.length(1); + }); + TIMEZONE_TO_TEST.forEach((timezone) => { describe(`Timezone: ${timezone}`, () => { it('should use timezone prop for onChange when no value is provided', () => {