Skip to content

Commit

Permalink
[pickers] Fix DST issue with America/Asuncion timezone and `Adapter…
Browse files Browse the repository at this point in the history
…Moment` (#15552)
  • Loading branch information
flaviendelangle authored and web-flow committed Nov 28, 2024
1 parent bbed5a3 commit 1dd80e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,25 @@ export class AdapterMoment implements MuiPickersAdapter<Moment, string> {

let count = 0;
let current = start;
let currentDayOfYear = current.get('dayOfYear');
const nestedWeeks: Moment[][] = [];

while (current.isBefore(end)) {
const weekNumber = Math.floor(count / 7);
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ describe('<DateCalendar /> - 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(<DateCalendar timezone="America/Asuncion" value={adapter.date('2024-10-10')} />);
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', () => {
Expand Down

0 comments on commit 1dd80e3

Please sign in to comment.