From d10bae995c64ae5cc0e9756a737e5bdee6f2fad7 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 e92f7c9a06fe..3201691862e9 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 a2dd2a4a14d7..ab26ec9edab8 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', () => {