Repository navigation
[material_ui] Keep UTC dates in UTC in DateUtils #13898
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
Open
matthewalberto
wants to merge
2
commits into
flutter:main
Choose a base branch
from
matthewalberto:material-ui-dateutils-preserve-utc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/material_ui/pending_changelogs/change_2026_12_04_date_utils_utc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| changelog: | | ||
| - Fixes `DateUtils.dateOnly`, `DateUtils.datesOnly`, `DateUtils.addDaysToDate` and `DateUtils.addMonthsToMonthDate` returning a local date for a UTC date. They now return a UTC date for a UTC date. | ||
| version: patch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:material_ui/material_ui.dart'; | ||
|
|
||
| void main() { | ||
| group('DateUtils', () { | ||
| test('dateOnly keeps a UTC date in UTC', () { | ||
| final DateTime result = DateUtils.dateOnly(DateTime.utc(1970, 1, 1, 23, 59)); | ||
| expect(result.isUtc, isTrue); | ||
| expect(result, DateTime.utc(1970)); | ||
| expect(DateUtils.dateOnly(DateTime.utc(1970)).toUtc(), DateTime.utc(1970)); | ||
| }); | ||
|
|
||
| test('dateOnly keeps a local date in local time', () { | ||
| final DateTime result = DateUtils.dateOnly(DateTime(1970, 1, 1, 23, 59)); | ||
| expect(result.isUtc, isFalse); | ||
| expect(result, DateTime(1970)); | ||
| }); | ||
|
|
||
| test('datesOnly keeps UTC dates in UTC', () { | ||
| final DateTimeRange result = DateUtils.datesOnly( | ||
| DateTimeRange(start: DateTime.utc(1970, 1, 1, 23), end: DateTime.utc(1970, 1, 2, 1)), | ||
| ); | ||
| expect(result.start.isUtc, isTrue); | ||
| expect(result.end.isUtc, isTrue); | ||
| expect(result, DateTimeRange(start: DateTime.utc(1970), end: DateTime.utc(1970, 1, 2))); | ||
| }); | ||
|
|
||
| test('datesOnly keeps local dates in local time', () { | ||
| final DateTimeRange result = DateUtils.datesOnly( | ||
| DateTimeRange(start: DateTime(1970, 1, 1, 23), end: DateTime(1970, 1, 2, 1)), | ||
| ); | ||
| expect(result, DateTimeRange(start: DateTime(1970), end: DateTime(1970, 1, 2))); | ||
| }); | ||
|
|
||
| test('datesOnly of a range with one UTC and one local date returns local dates', () { | ||
| // At UTC+8 the UTC start would otherwise land after the local end, which | ||
| // the DateTimeRange constructor rejects. | ||
| final DateTimeRange result = DateUtils.datesOnly( | ||
| DateTimeRange(start: DateTime.utc(2023, 1, 2, 1), end: DateTime(2023, 1, 2, 10)), | ||
| ); | ||
| expect(result.start.isUtc, isFalse); | ||
| expect(result.end.isUtc, isFalse); | ||
| expect(result, DateTimeRange(start: DateTime(2023, 1, 2), end: DateTime(2023, 1, 2))); | ||
| }); | ||
|
|
||
| test('addDaysToDate keeps a UTC date in UTC', () { | ||
| final DateTime result = DateUtils.addDaysToDate(DateTime.utc(2026, 1, 31, 23), 1); | ||
| expect(result.isUtc, isTrue); | ||
| expect(result, DateTime.utc(2026, 2)); | ||
| expect(DateUtils.addDaysToDate(DateTime.utc(2026, 3, 1, 12), -1), DateTime.utc(2026, 2, 28)); | ||
| }); | ||
|
|
||
| test('addDaysToDate keeps a local date in local time', () { | ||
| final DateTime result = DateUtils.addDaysToDate(DateTime(2026, 1, 31, 23), 1); | ||
| expect(result.isUtc, isFalse); | ||
| expect(result, DateTime(2026, 2)); | ||
| }); | ||
|
|
||
| test('addMonthsToMonthDate keeps a UTC date in UTC', () { | ||
| final DateTime result = DateUtils.addMonthsToMonthDate(DateTime.utc(2026, 12, 31, 23), 1); | ||
| expect(result.isUtc, isTrue); | ||
| expect(result, DateTime.utc(2027)); | ||
| expect(DateUtils.addMonthsToMonthDate(DateTime.utc(2026, 1, 15), -1), DateTime.utc(2025, 12)); | ||
| }); | ||
|
|
||
| test('addMonthsToMonthDate keeps a local date in local time', () { | ||
| final DateTime result = DateUtils.addMonthsToMonthDate(DateTime(2026, 12, 31, 23), 1); | ||
| expect(result.isUtc, isFalse); | ||
| expect(result, DateTime(2027)); | ||
| }); | ||
|
|
||
| test('isSameDay and isSameMonth read each date in its own time zone', () { | ||
| expect(DateUtils.isSameDay(DateTime.utc(2026, 1, 1, 23), DateTime(2026)), isTrue); | ||
| expect(DateUtils.isSameDay(DateTime(2026), DateTime.utc(2026, 1, 1, 23)), isTrue); | ||
| expect(DateUtils.isSameDay(DateTime.utc(2026, 1, 1, 23), DateTime(2026, 1, 2)), isFalse); | ||
| expect(DateUtils.isSameMonth(DateTime.utc(2026, 1, 31, 23), DateTime(2026)), isTrue); | ||
| expect(DateUtils.isSameMonth(DateTime.utc(2026, 1, 31, 23), DateTime(2026, 2)), isFalse); | ||
| }); | ||
| }); | ||
|
|
||
| group('GregorianCalendarDelegate', () { | ||
| const delegate = GregorianCalendarDelegate(); | ||
|
|
||
| test('returns local dates, also for a UTC argument', () { | ||
| expect(delegate.getDay(2026, 1, 31), DateTime(2026, 1, 31)); | ||
| expect(delegate.getMonth(2026, 1), DateTime(2026)); | ||
| expect(delegate.dateOnly(DateTime.utc(2026, 1, 31, 23)), delegate.getDay(2026, 1, 31)); | ||
| expect(delegate.addDaysToDate(DateTime.utc(2026, 1, 31, 23), 1), delegate.getDay(2026, 2, 1)); | ||
| expect( | ||
| delegate.addMonthsToMonthDate(DateTime.utc(2026, 12, 31, 23), 1), | ||
| delegate.getMonth(2027, 1), | ||
| ); | ||
| expect( | ||
| delegate.datesOnly( | ||
| DateTimeRange(start: DateTime.utc(2026, 1, 1, 23), end: DateTime.utc(2026, 1, 31, 23)), | ||
| ), | ||
| DateTimeRange(start: delegate.getDay(2026, 1, 1), end: delegate.getDay(2026, 1, 31)), | ||
| ); | ||
| }); | ||
|
|
||
| testWidgets('CalendarDatePicker can select the first and last day of a UTC range', ( | ||
| WidgetTester tester, | ||
| ) async { | ||
| DateTime? selected; | ||
| await tester.pumpWidget( | ||
| MaterialApp( | ||
| home: Material( | ||
| child: CalendarDatePicker( | ||
| initialDate: DateTime.utc(2026, 1, 15), | ||
| firstDate: DateTime.utc(2026), | ||
| lastDate: DateTime.utc(2026, 1, 31), | ||
| onDateChanged: (DateTime value) => selected = value, | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| await tester.tap(find.text('1')); | ||
| await tester.pumpAndSettle(); | ||
| expect(selected, DateTime(2026)); | ||
|
|
||
| await tester.tap(find.text('31')); | ||
| await tester.pumpAndSettle(); | ||
| expect(selected, DateTime(2026, 1, 31)); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
DateUtils.dateOnlynow returning a UTC date for a UTC input and a local date for a local input, callingDateUtils.datesOnlywith aDateTimeRangethat has mixed timezones (one UTC and one local) can now cause a runtime assertion failure in theDateTimeRangeconstructor.\n\n### Example Scenario (in UTC+5 timezone):\n1.start=DateTime.utc(2023, 1, 2, 1)(UTC, which is2023-01-02 01:00:00.000Z)\n2.end=DateTime(2023, 1, 2, 10)(Local, which is2023-01-02 05:00:00.000Z)\n3.DateTimeRange(start: start, end: end)is successfully constructed becausestartis beforeend.\n4. CallingDateUtils.datesOnly(range)will:\n - ConvertstarttoDateTime.utc(2023, 1, 2)(2023-01-02 00:00:00.000Z)\n - ConvertendtoDateTime(2023, 1, 2)local (2023-01-01 19:00:00.000Zin UTC)\n5. The newDateTimeRangeconstructor will fail withAssertion failed: !start.isAfter(end)because2023-01-02 00:00:00.000Zis after2023-01-01 19:00:00.000Z.\n\n### Recommendation:\nConsider documenting this limitation inDateUtils.datesOnly, or updateDateUtils.datesOnlyto 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.
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
DateTimeRangerejects that. I added a test that fails on the first commit, anddatesOnlynow returns local dates for such a range, as it did before. Pushed in bd78e3d.