Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pickers] Fix DST issue with America/Asuncion timezone and AdapterMoment #15552

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original snippet uses .diff but it also work if you just check the dayOfYear property which is a lot cheaper to access.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is a way more efficient and easier to understand solution.
Great job. 👍


// 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
Loading