Repository navigation
[material_ui] Keep UTC dates in UTC in DateUtils - #13898
matthewalberto wants to merge 2 commits into
Conversation
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
There was a problem hiding this comment.
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.
| 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); | ||
| } |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
DateUtils.dateOnly,datesOnly,addDaysToDateandaddMonthsToMonthDatebuilt a localDateTimefrom 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()gave1969-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.GregorianCalendarDelegatedoes not follow this change: itsdateOnly,addDaysToDateandaddMonthsToMonthDatestill return local dates. The pickers comparedateOnly(firstDate)anddateOnly(lastDate)with the local days built bygetDay, 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 callDateUtilsand running the new picker test withTZset: 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.isSameDayandisSameMonthare 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.darthas 11 tests. On the current code 4 of them fail (the UTC cases ofdateOnly,datesOnly,addDaysToDateandaddMonthsToMonthDate), at UTC, UTC+8 and UTC-5, because they checkisUtcas well as the date. The others pin the local cases, theisSameDay/isSameMonthbehaviour, the delegate returning local dates, and aCalendarDatePickerwith a UTCfirstDate/lastDate. All 11 pass with the change at UTC, UTC+8 and UTC-5. The fullmaterial_uitest suite gives the same results with and without the change, apart from the new tests.I formatted and analyzed the changed files with
dart formatandflutter analyze. I did not runflutter_plugin_tools formator 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
DateUtilsmethods and expects a local date back. I did not find any caller in this repository outsidedate.dart.Fixes flutter/flutter#132076
Pre-Review Checklist
[shared_preferences]///).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-assistbot 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
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