-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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][DateRangePicker] timezones handled incorrectly with dayjs
when timezone extensions are enabled
#13290
Comments
dayjs
when timezone extensions are enabled
Hey @oskarkk and thanks for raising this issue. Let me have a look at what might be going wrong here. |
Ok, this happens because the value will be updated with a new timezone on change. |
OK, I found the issue after a bit of a search around the valueManagement and state.
When we change one value it gets set to the local timezone (using
IMHO we should go for the 1. option, since it will allow for ranges that span multiple timezones. Not a very widely used case, but it can still happen (e.g. in planning software) WDYT @mui/explore ? |
@oskarkk could you elaborate a bit more about your use case? As I see it, everything is handled correctly, when the bound values are created with appropriate functions. |
I would say that the timezones are not different, they're the same timezone, as they are (in my case) "Europe/Warsaw" and "system", and my system timezone is "Europe/Warsaw". My use case was that I had some fields that worked in user timezone, so I didn't have timezone and utc extensions for dayjs. Then I needed some additional fields that would be in a specific timezone, so I added timezone and utc extensions and made some datetime fields with timezone prop. Then the other fields without timezone prop started throwing errors.
You don't have to set any initial value to reproduce this issue, you can reproduce it with just this (this time with DateTimeRangePicker):
Somehow without default values the error show less frequently, I have to change the date more times before it breaks (changing month seems to fail faster). It seems to work without SingleInputDateTimeRangeField (but maybe it's even rarer then). |
@LukasTy on another note: const [value, setValue] = useState<[Dayjs | null, Dayjs | null]>([null, null]); it tries to get the timezone form Maybe we need to somehow update both values at the same time when using the single input range field. 🤷🏼 |
@oskarkk could you provide clear steps to reproduce the issue without having any value bound? 🤔 The root cause of the problem is that we are trying to "guess" the timezone of the value, when a
@michelengelen could provide step by step guide on how to cause the error given your example? |
@LukasTy using this code: import * as React from 'react';
import { Box } from '@mui/material';
import {
DateRangePicker,
LocalizationProvider,
SingleInputDateRangeField,
} from '@mui/x-date-pickers-pro';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import dayjs, { Dayjs } from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
dayjs.extend(timezone);
dayjs.extend(utc);
export default function App() {
const [value, setValue] = React.useState<[Dayjs | null, Dayjs | null]>([null, null]);
return (
<Box justifyContent={'center'} sx={{ width: '100%' }}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DateRangePicker
value={value}
onChange={setValue}
slots={{ field: SingleInputDateRangeField }}
sx={{ '& .MuiInputBase-root': { width: '250px' } }}
/>
</LocalizationProvider>
</Box>
);
} and then changing one of the values in the inputfield via keyboard will trigger that error. Screen.Recording.2024-05-31.at.16.07.36.mov |
@michelengelen I have tried the same code before writing a comment and it's strange that for me locally I do not get any error... 🤷 🙈 |
thats strange ... let me fetch master and try again! |
My bad ... I had some logging in place that caused that error. Sry! 🙇🏼 |
@LukasTy
Without timezone plugin the value doesn't have timezone, but will be presented in users timezone. So if I set value to Anyway, overall the field seems to work as I expect, but when entering by keyboard it somehow sets the timezone of one date to user's timezone (with continent/city, like Europe/Warsaw), and the other to "system", which I think is essentially the same, but maybe there are two places that set the timezone differently, but with the same intent of setting user's timezone - the same behavior as without plugins. |
@oskarkk Thank you for providing a detailed guide to reproduce the problem. 🙏 |
@oskarkk: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey. |
Steps to reproduce
Link to live example: https://codesandbox.io/p/sandbox/mui-daterangepicker-error-zphn44
Steps:
Sometimes it works, maybe depending on the initial value.
Current behavior
The error appears only when using dayjs timezone extensions (
dayjs.extend(timezone); dayjs.extend(utc);
).When I put a breakpoint on this line of code (rangeValueManager.getTimezone) I can see that one date has a timezone "system" and the other "Europe/Warsaw" (my system timezone), and that raises an exception.
Expected behavior
No response
Context
No response
Your environment
x-date-pickers-pro 7.5.1
material 5.15.14
(see codesandbox)
Search keywords: DateRangePicker timezone dayjs
The text was updated successfully, but these errors were encountered: