diff --git a/packages/material_ui/lib/src/date.dart b/packages/material_ui/lib/src/date.dart index c0029cd0efb..ee8520ad1ad 100644 --- a/packages/material_ui/lib/src/date.dart +++ b/packages/material_ui/lib/src/date.dart @@ -179,6 +179,11 @@ abstract class CalendarDelegate { /// /// 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. @@ -190,19 +195,26 @@ class GregorianCalendarDelegate extends CalendarDelegate { @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) { @@ -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); } @@ -278,6 +295,15 @@ 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)); } @@ -285,6 +311,11 @@ abstract final class DateUtils { /// 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; } @@ -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; } @@ -328,7 +361,13 @@ 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); } @@ -336,7 +375,12 @@ abstract final class DateUtils { /// 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); } diff --git a/packages/material_ui/pending_changelogs/change_2026_12_04_date_utils_utc.yaml b/packages/material_ui/pending_changelogs/change_2026_12_04_date_utils_utc.yaml new file mode 100644 index 00000000000..17a6dc7014d --- /dev/null +++ b/packages/material_ui/pending_changelogs/change_2026_12_04_date_utils_utc.yaml @@ -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 diff --git a/packages/material_ui/test/date_test.dart b/packages/material_ui/test/date_test.dart new file mode 100644 index 00000000000..ae8860b9a67 --- /dev/null +++ b/packages/material_ui/test/date_test.dart @@ -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)); + }); + }); +}