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] Add proper support for UTC and timezones #8261

Merged
merged 30 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5a6c65a
[pickers] Add proper support for UTC and timezones
flaviendelangle Jun 5, 2023
d456bf2
Merge branch 'master' into luxon-utc
flaviendelangle Jun 5, 2023
575d8a2
Merge branch 'master' into luxon-utc
flaviendelangle Jun 6, 2023
6c9e59c
Work on tests
flaviendelangle Jun 6, 2023
2fa1254
Work on tests
flaviendelangle Jun 6, 2023
d18bd30
Code review: Alex
flaviendelangle Jun 7, 2023
d67dbb1
Merge branch 'master' into luxon-utc
flaviendelangle Jun 8, 2023
f311343
Work on TimeClock testing
flaviendelangle Jun 8, 2023
5cc9001
Merge branch 'master' into luxon-utc
flaviendelangle Jun 8, 2023
5f18842
Code review: José
flaviendelangle Jun 8, 2023
b6b5ec8
Merge branch 'master' into luxon-utc
flaviendelangle Jun 9, 2023
16429b5
Improve Luxon doc
flaviendelangle Jun 9, 2023
5ce179c
Merge branch 'master' into luxon-utc
flaviendelangle Jun 9, 2023
8bd671b
Empty
flaviendelangle Jun 9, 2023
2bfebe7
Empty
flaviendelangle Jun 9, 2023
c3a6534
Merge branch 'master' into luxon-utc
flaviendelangle Jun 13, 2023
4e24f38
Code review: Lukas
flaviendelangle Jun 13, 2023
3f16a16
Code review: Lukas
flaviendelangle Jun 13, 2023
ffbd38d
Add tests to DigitalClock
flaviendelangle Jun 13, 2023
dbcb4b7
Merge
flaviendelangle Jun 13, 2023
9ab2ab2
Improve test coverage
flaviendelangle Jun 14, 2023
662ccf4
Improve tests
flaviendelangle Jun 14, 2023
5485688
Merge branch 'master' into luxon-utc
flaviendelangle Jun 14, 2023
adcdbfa
Fix
flaviendelangle Jun 14, 2023
b403931
Improve doc
flaviendelangle Jun 14, 2023
90449b1
Merge branch 'master' into luxon-utc
flaviendelangle Jun 15, 2023
910fc37
Clean useValueWithTimezone
flaviendelangle Jun 15, 2023
932f1cd
Merge branch 'master' into luxon-utc
flaviendelangle Jun 19, 2023
acd18f2
Code review: José
flaviendelangle Jun 19, 2023
7db1884
Merge
flaviendelangle Jun 20, 2023
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
71 changes: 0 additions & 71 deletions docs/data/date-pickers/adapters-locale/adapters-locale.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,74 +250,3 @@ This prop is available on all components that render a day calendar, including t
The example below adds a dot at the end of each day in the calendar header:

{{"demo": "CustomDayOfWeekFormat.js"}}

## Use UTC dates

### With dayjs

To use UTC dates with `dayjs`, you have to:

1. Extend `dayjs` with its `utc` plugin:

```tsx
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);
```

2. Pass `dayjs.utc` to `LocalizationProvider` `dateLibInstance` prop:

```tsx
<LocalizationProvider dateAdapter={AdapterDayjs} dateLibInstance={dayjs.utc}>
{children}
</LocalizationProvider>
```

3. Always pass dates created with `dayjs.utc`:

```tsx
<DateTimePicker
// ✅ Valid props
value={dayjs.utc()}
minDate={dayjs.utc().startOf('month')}
// ❌ Invalid props
value={dayjs()}
minDate={dayjs().startOf('month')}
/>
```

{{"demo": "UTCDayjs.js", "defaultCodeOpen": false}}

### With moment

To use UTC dates with `moment`, you have to:

1. Pass `moment.utc` to `LocalizationProvider` `dateLibInstance` prop:

```tsx
<LocalizationProvider dateAdapter={AdapterMoment} dateLibInstance={moment.utc}>
{children}
</LocalizationProvider>
```

2. Always pass dates created with `moment.utc`:

```tsx
<DateTimePicker
// ✅ Valid props
value={moment.utc()}
minDate={moment.utc().startOf('month')}
// ❌ Invalid props
value={moment()}
minDate={moment().startOf('month')}
/>
```

{{"demo": "UTCMoment.js", "defaultCodeOpen": false}}

### Other libraries

UTC support is an ongoing topic.

We will update the documentation with examples using other date libraries once the support for them will be sufficient.
38 changes: 38 additions & 0 deletions docs/data/date-pickers/timezone/BasicTimezoneProp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as React from 'react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { TimePicker } from '@mui/x-date-pickers/TimePicker';

dayjs.extend(utc);
dayjs.extend(timezone);

export default function BasicTimezoneProp() {
const [value, setValue] = React.useState(dayjs.utc('2022-04-17T15:30'));

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Stack spacing={2}>
<TimePicker
value={value}
onChange={setValue}
timezone="America/New_York"
label={'Rendered in "America/New_York"'}
/>
<TimePicker
value={value}
onChange={setValue}
timezone="Europe/Paris"
label={'Rendered in "Europe/Paris"'}
/>
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
</Stack>
</LocalizationProvider>
);
}
40 changes: 40 additions & 0 deletions docs/data/date-pickers/timezone/BasicTimezoneProp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from 'react';
import dayjs, { Dayjs } from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { TimePicker } from '@mui/x-date-pickers/TimePicker';

dayjs.extend(utc);
dayjs.extend(timezone);

export default function BasicTimezoneProp() {
const [value, setValue] = React.useState<Dayjs | null>(
dayjs.utc('2022-04-17T15:30'),
);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Stack spacing={2}>
<TimePicker
value={value}
onChange={setValue}
timezone="America/New_York"
label={'Rendered in "America/New_York"'}
/>
<TimePicker
value={value}
onChange={setValue}
timezone="Europe/Paris"
label={'Rendered in "Europe/Paris"'}
/>
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
</Stack>
</LocalizationProvider>
);
}
15 changes: 15 additions & 0 deletions docs/data/date-pickers/timezone/BasicTimezoneProp.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<TimePicker
value={value}
onChange={setValue}
timezone="America/New_York"
label={'Rendered in "America/New_York"'}
/>
<TimePicker
value={value}
onChange={setValue}
timezone="Europe/Paris"
label={'Rendered in "Europe/Paris"'}
/>
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
33 changes: 33 additions & 0 deletions docs/data/date-pickers/timezone/BasicValueProp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as React from 'react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { TimePicker } from '@mui/x-date-pickers/TimePicker';

dayjs.extend(utc);
dayjs.extend(timezone);

export default function BasicValueProp() {
const [value, setValue] = React.useState(
dayjs.tz('2022-04-17T15:30', 'America/New_York'),
);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Stack spacing={2}>
<TimePicker
label={'Value timezone: "America/New_York"'}
value={value}
onChange={setValue}
/>
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
</Stack>
</LocalizationProvider>
);
}
33 changes: 33 additions & 0 deletions docs/data/date-pickers/timezone/BasicValueProp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as React from 'react';
import dayjs, { Dayjs } from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { TimePicker } from '@mui/x-date-pickers/TimePicker';

dayjs.extend(utc);
dayjs.extend(timezone);

export default function BasicValueProp() {
const [value, setValue] = React.useState<Dayjs | null>(
dayjs.tz('2022-04-17T15:30', 'America/New_York'),
);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Stack spacing={2}>
<TimePicker
label={'Value timezone: "America/New_York"'}
value={value}
onChange={setValue}
/>
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
</Stack>
</LocalizationProvider>
);
}
8 changes: 8 additions & 0 deletions docs/data/date-pickers/timezone/BasicValueProp.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<TimePicker
label={'Value timezone: "America/New_York"'}
value={value}
onChange={setValue}
/>
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
29 changes: 29 additions & 0 deletions docs/data/date-pickers/timezone/DayjsTimezone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as React from 'react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';

dayjs.extend(utc);
dayjs.extend(timezone);

export default function DayjsTimezone() {
const [value, setValue] = React.useState(
dayjs.tz('2022-04-17T15:30', 'America/New_York'),
);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Stack spacing={2}>
<DateTimePicker value={value} onChange={setValue} />
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
</Stack>
</LocalizationProvider>
);
}
29 changes: 29 additions & 0 deletions docs/data/date-pickers/timezone/DayjsTimezone.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as React from 'react';
import dayjs, { Dayjs } from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';

dayjs.extend(utc);
dayjs.extend(timezone);

export default function DayjsTimezone() {
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
const [value, setValue] = React.useState<Dayjs | null>(
dayjs.tz('2022-04-17T15:30', 'America/New_York'),
);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Stack spacing={2}>
<DateTimePicker value={value} onChange={setValue} />
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
</Stack>
</LocalizationProvider>
);
}
4 changes: 4 additions & 0 deletions docs/data/date-pickers/timezone/DayjsTimezone.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<DateTimePicker value={value} onChange={setValue} />
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
25 changes: 25 additions & 0 deletions docs/data/date-pickers/timezone/DayjsUTC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from 'react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';

dayjs.extend(utc);

export default function DayjsUTC() {
const [value, setValue] = React.useState(dayjs.utc('2022-04-17T15:30'));

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Stack spacing={2}>
<DateTimePicker value={value} onChange={setValue} />
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
</Stack>
</LocalizationProvider>
);
}
27 changes: 27 additions & 0 deletions docs/data/date-pickers/timezone/DayjsUTC.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as React from 'react';
import dayjs, { Dayjs } from 'dayjs';
import utc from 'dayjs/plugin/utc';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';

dayjs.extend(utc);

export default function DayjsUTC() {
const [value, setValue] = React.useState<Dayjs | null>(
dayjs.utc('2022-04-17T15:30'),
);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Stack spacing={2}>
<DateTimePicker value={value} onChange={setValue} />
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
</Stack>
</LocalizationProvider>
);
}
4 changes: 4 additions & 0 deletions docs/data/date-pickers/timezone/DayjsUTC.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<DateTimePicker value={value} onChange={setValue} />
<Typography>
Stored value: {value == null ? 'null' : value.format()}
</Typography>
Loading