Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 47 additions & 3 deletions packages/material_ui/lib/src/date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ abstract class CalendarDelegate<T extends DateTime> {
///
/// This delegate is the default calendar system for [CalendarDatePicker].
///
/// The dates it returns are in local time, even when they are made from a
/// UTC [DateTime]: [dateOnly], [addDaysToDate] and [addMonthsToMonthDate]
/// keep the year, month and day of their argument. [DateUtils] keeps a UTC
/// date in UTC.
///
/// See also:
/// * [CalendarDelegate], the base class for defining custom calendars.
/// * [CalendarDatePicker], which uses this delegate for date selection.
Expand All @@ -190,19 +195,26 @@ class GregorianCalendarDelegate extends CalendarDelegate<DateTime> {
@override
DateTime now() => DateTime.now();

// The date pickers build their days with getDay and getMonth, which return
// local dates. So unlike DateUtils, these keep returning local dates for a
// UTC argument: a UTC firstDate or lastDate must compare equal to, not
// hours before or after, the local day built for it.

@override
DateTime dateOnly(DateTime date) => DateUtils.dateOnly(date);
DateTime dateOnly(DateTime date) => DateTime(date.year, date.month, date.day);

@override
int monthDelta(DateTime startDate, DateTime endDate) => DateUtils.monthDelta(startDate, endDate);

@override
DateTime addMonthsToMonthDate(DateTime monthDate, int monthsToAdd) {
return DateUtils.addMonthsToMonthDate(monthDate, monthsToAdd);
return DateTime(monthDate.year, monthDate.month + monthsToAdd);
}

@override
DateTime addDaysToDate(DateTime date, int days) => DateUtils.addDaysToDate(date, days);
DateTime addDaysToDate(DateTime date, int days) {
return DateTime(date.year, date.month, date.day + days);
}

@override
int firstDayOffset(int year, int month, MaterialLocalizations localizations) {
Expand Down Expand Up @@ -266,7 +278,12 @@ abstract final class DateUtils {
/// Returns a [DateTime] with the date of the original, but time set to
/// midnight.
/// {@endtemplate}
///
/// The result is in UTC if [date] is in UTC, and in local time otherwise.
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);
}
Comment on lines 283 to 288

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.


Expand All @@ -278,13 +295,27 @@ abstract final class DateUtils {
/// * [dateOnly], which does the same thing for a single date.
/// {@endtemplate}
static DateTimeRange datesOnly(DateTimeRange range) {
if (range.start.isUtc != range.end.isUtc) {
// A range with one UTC and one local date keeps returning local dates, as
// it always did. Returning a UTC date for one end and a local date for the
// other could put the start after the end, which [DateTimeRange] rejects.
return DateTimeRange(
start: DateTime(range.start.year, range.start.month, range.start.day),
end: DateTime(range.end.year, range.end.month, range.end.day),
);
}
return DateTimeRange(start: dateOnly(range.start), end: dateOnly(range.end));
}

/// {@template material_ui.date.isSameDay}
/// Returns true if the two [DateTime] objects have the same day, month, and
/// year, or are both null.
/// {@endtemplate}
///
/// Each date is read in its own time zone: if one is in UTC and the other
/// in local time, their year, month and day are compared as they are,
/// without converting either one. To ask whether two moments fall on the
/// same local day, convert both first, for example with [DateTime.toLocal].
static bool isSameDay(DateTime? dateA, DateTime? dateB) {
return dateA?.year == dateB?.year && dateA?.month == dateB?.month && dateA?.day == dateB?.day;
}
Expand All @@ -293,6 +324,8 @@ abstract final class DateUtils {
/// Returns true if the two [DateTime] objects have the same month and
/// year, or are both null.
/// {@endtemplate}
///
/// As with [isSameDay], each date is read in its own time zone.
static bool isSameMonth(DateTime? dateA, DateTime? dateB) {
return dateA?.year == dateB?.year && dateA?.month == dateB?.month;
}
Expand Down Expand Up @@ -328,15 +361,26 @@ abstract final class DateUtils {
/// `date` would be January 15, 2019.
/// `futureDate` would be April 1, 2019 since it adds 3 months.
/// {@endtemplate}
///
/// The result is in UTC if [monthDate] is in UTC, and in local time
/// otherwise.
static DateTime addMonthsToMonthDate(DateTime monthDate, int monthsToAdd) {
if (monthDate.isUtc) {
return DateTime.utc(monthDate.year, monthDate.month + monthsToAdd);
}
return DateTime(monthDate.year, monthDate.month + monthsToAdd);
}

/// {@template material_ui.date.addDaysToDate}
/// Returns a [DateTime] with the added number of days and time set to
/// midnight.
/// {@endtemplate}
///
/// The result is in UTC if [date] is in UTC, and in local time otherwise.
static DateTime addDaysToDate(DateTime date, int days) {
if (date.isUtc) {
return DateTime.utc(date.year, date.month, date.day + days);
}
return DateTime(date.year, date.month, date.day + days);
}

Expand Down
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
131 changes: 131 additions & 0 deletions packages/material_ui/test/date_test.dart
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));
});
});
}