Skip to content

[material_ui] Keep UTC dates in UTC in DateUtils - #13898

Open
matthewalberto wants to merge 2 commits into
flutter:mainfrom
matthewalberto:material-ui-dateutils-preserve-utc
Open

matthewalberto wants to merge 2 commits into
flutter:mainfrom
matthewalberto:material-ui-dateutils-preserve-utc

Conversation

@matthewalberto

Copy link
Copy Markdown

DateUtils.dateOnly, datesOnly, addDaysToDate and addMonthsToMonthDate built a local DateTime from the year, month and day of a UTC date. The result was no longer in UTC and pointed at a different moment: at UTC+1, DateUtils.dateOnly(DateTime.utc(1970, 1, 1)).toUtc() gave 1969-12-31 23:00:00.000Z. They now return a UTC date for a UTC date, and a local date for a local date as before.

GregorianCalendarDelegate does not follow this change: its dateOnly, addDaysToDate and addMonthsToMonthDate still return local dates. The pickers compare dateOnly(firstDate) and dateOnly(lastDate) with the local days built by getDay, so a UTC result there would make the first day of the range unselectable east of UTC and the last day unselectable west of it. I checked this by making the delegate call DateUtils and running the new picker test with TZ set: at UTC+8, tapping the first day did nothing. At UTC-5, tapping the last day did nothing. With this PR the pickers behave as before.

isSameDay and isSameMonth are unchanged. The issue left open what to do when one date is UTC and the other local, so I kept today's behaviour: they compare the year, month and day of each date in its own time zone, and the docs now say so. @ilovelinux, you raised this case in the issue, so your view on that choice would help.

Tests: the new test/date_test.dart has 11 tests. On the current code 4 of them fail (the UTC cases of dateOnly, datesOnly, addDaysToDate and addMonthsToMonthDate), at UTC, UTC+8 and UTC-5, because they check isUtc as well as the date. The others pin the local cases, the isSameDay/isSameMonth behaviour, the delegate returning local dates, and a CalendarDatePicker with a UTC firstDate/lastDate. All 11 pass with the change at UTC, UTC+8 and UTC-5. The full material_ui test suite gives the same results with and without the change, apart from the new tests.

I formatted and analyzed the changed files with dart format and flutter analyze. I did not run flutter_plugin_tools format or its analyze step, because that command ran for 33 minutes on my machine with no output and I stopped it, so CI is the final check for those.

This is a behaviour change for code that passes a UTC date to these four DateUtils methods and expects a local date back. I did not find any caller in this repository outside date.dart.

Fixes flutter/flutter#132076

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2

DateUtils.dateOnly, datesOnly, addDaysToDate and addMonthsToMonthDate
built a local DateTime from the fields of a UTC date, so the result was
no longer in UTC and pointed at a different moment (at UTC+1,
DateUtils.dateOnly(DateTime.utc(1970)).toUtc() was 1969-12-31 23:00Z).
They now return a UTC date for a UTC date and a local date for a local
one.

GregorianCalendarDelegate keeps returning local dates. The pickers
compare dateOnly(firstDate) and dateOnly(lastDate) with the local days
built by getDay, so a UTC result there would disable the first day of
the range east of UTC and the last day west of it.

isSameDay and isSameMonth are unchanged; their docs now say that each
date is read in its own time zone.

Fixes flutter/flutter#132076
@github-actions github-actions Bot added p: material_ui triage-design Should be looked at in design triage labels Dec 5, 2026
@github-actions github-actions Bot added the P3 Issues that are less important to the Flutter project label Dec 5, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request modifies DateUtils methods (dateOnly, addMonthsToMonthDate, and addDaysToDate) to preserve the UTC timezone of the input DateTime rather than converting it to local time, while updating GregorianCalendarDelegate to consistently return local dates. Feedback points out that preserving UTC in DateUtils.dateOnly can lead to runtime assertion failures in DateTimeRange when DateUtils.datesOnly is called with mixed timezones, and recommends documenting this limitation or handling mixed timezones safely.

Comment on lines 283 to 288
static DateTime dateOnly(DateTime date) {
if (date.isUtc) {
return DateTime.utc(date.year, date.month, date.day);
}
return DateTime(date.year, date.month, date.day);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

With DateUtils.dateOnly now returning a UTC date for a UTC input and a local date for a local input, calling DateUtils.datesOnly with a DateTimeRange that has mixed timezones (one UTC and one local) can now cause a runtime assertion failure in the DateTimeRange constructor.\n\n### Example Scenario (in UTC+5 timezone):\n1. start = DateTime.utc(2023, 1, 2, 1) (UTC, which is 2023-01-02 01:00:00.000Z)\n2. end = DateTime(2023, 1, 2, 10) (Local, which is 2023-01-02 05:00:00.000Z)\n3. DateTimeRange(start: start, end: end) is successfully constructed because start is before end.\n4. Calling DateUtils.datesOnly(range) will:\n - Convert start to DateTime.utc(2023, 1, 2) (2023-01-02 00:00:00.000Z)\n - Convert end to DateTime(2023, 1, 2) local (2023-01-01 19:00:00.000Z in UTC)\n5. The new DateTimeRange constructor will fail with Assertion failed: !start.isAfter(end) because 2023-01-02 00:00:00.000Z is after 2023-01-01 19:00:00.000Z.\n\n### Recommendation:\nConsider documenting this limitation in DateUtils.datesOnly, or update DateUtils.datesOnly to handle mixed timezones safely (e.g., by converting both to local or UTC if they differ, or asserting/throwing a clearer error).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, thank you. A range with one UTC and one local date could end with its start after its end at UTC+8, and DateTimeRange rejects that. I added a test that fails on the first commit, and datesOnly now returns local dates for such a range, as it did before. Pushed in bd78e3d.

… local date

A range with a UTC start and a local end could end up with a start after
its end once each side was reduced to a date, which DateTimeRange rejects
with an assertion (at UTC+8: start 2023-01-02 01:00Z, end 2023-01-02 10:00
local). Such a range now returns local dates, as it did before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: material_ui P3 Issues that are less important to the Flutter project triage-design Should be looked at in design triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DateUtils lacks support for non-local time zones

1 participant