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] DateCalendar shows duplicate dates for DST when using AdapterMoment #14730

Open
gurtajkhatra opened this issue Sep 25, 2024 · 1 comment
Assignees
Labels
bug 🐛 Something doesn't work component: pickers This is the name of the generic UI component, not the React module! time-zone Issues about time zone management

Comments

@gurtajkhatra
Copy link

gurtajkhatra commented Sep 25, 2024

Steps to reproduce

Link to live example: https://codesandbox.io/p/sandbox/y6cws5

Steps:

  1. Scroll to October with the timezone set to America/Asuncion on the StaticDatePicker
  2. October 5th is shown twice in the calendar and every day in October after that is shifted by a day (Ex Oct 8th is shown as a Wednesday, but it should be a Tuesday

CleanShot 2024-09-25 at 12 23 33

Current behavior

October 5th is shown twice in the StaticDatePicker for the America/Asuncion timezone causing all dates to be shifted by a day

Expected behavior

October 5th should only be shown on Saturday.

Context

No response

Your environment

See the code sandbox here: https://codesandbox.io/p/sandbox/y6cws5

Search keywords: daylight savings DST

@gurtajkhatra gurtajkhatra added bug 🐛 Something doesn't work status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Sep 25, 2024
@github-actions github-actions bot added the component: pickers This is the name of the generic UI component, not the React module! label Sep 25, 2024
@flaviendelangle flaviendelangle self-assigned this Sep 27, 2024
@flaviendelangle
Copy link
Member

Your example only seems to be broken with Moment, with Day.js it seems to work fine.
I found a very similar comment in an issue of the moment repo: moment/moment#4743 (comment)

This code snippet seems to work, but it's not the kind of thing I'm very happy to introduce in the codebase 😢

Here is the diff:

diff --git a/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts b/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts
index a6e5288f2..f49bcc678 100644
--- a/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts
+++ b/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts
@@ -528,7 +528,17 @@ export class AdapterMoment implements MuiPickersAdapter<Moment, string> {
       nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
       nestedWeeks[weekNumber].push(current);
 
+      const prev = current;
       current = this.addDays(current, 1);
+
+      // 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.
+      const diff = current.diff(prev, 'hours');
+      if (diff === 23 && prev.get('h') === 0) {
+        current = current.add(12, 'h').startOf('day');
+      }
+
       count += 1;
     }

I'm adding it to our board for grooming 👍

@flaviendelangle flaviendelangle removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 27, 2024
@flaviendelangle flaviendelangle changed the title StaticDatePicker shows duplicate dates for daylight savings and shifts the entire calendar by a day for the month [pickers] DateCalendar shows duplicate dates for DST when using AdapterMoment Sep 27, 2024
@LukasTy LukasTy added the time-zone Issues about time zone management label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: pickers This is the name of the generic UI component, not the React module! time-zone Issues about time zone management
Projects
None yet
Development

No branches or pull requests

3 participants