From 3296d6a62ee9f346171f9e2143e3fad460777171 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 7 Oct 2025 15:43:24 +0000 Subject: [PATCH 1/6] Initial plan From a5f15c44ccadc5ca71a7b5790eacd1bf9b4c334d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 7 Oct 2025 15:50:48 +0000 Subject: [PATCH 2/6] Add comprehensive DateTime extension tests (277 lines) Co-authored-by: YoungMayor <47315212+YoungMayor@users.noreply.github.com> --- test/extensions/datetime_test.dart | 278 ++++++++++++++++++++++++++++- 1 file changed, 277 insertions(+), 1 deletion(-) diff --git a/test/extensions/datetime_test.dart b/test/extensions/datetime_test.dart index ab73b3a..77d97d3 100644 --- a/test/extensions/datetime_test.dart +++ b/test/extensions/datetime_test.dart @@ -1 +1,277 @@ -void main() {} +import 'package:flutter_test/flutter_test.dart'; +import 'package:mayr_extensions/mayr_extensions.dart'; + +void main() { + group('DateTime Manipulation Extensions', () { + final baseDate = DateTime(2025, 1, 15, 10, 30, 45); + + group('Adding time units', () { + test('addDays adds correct number of days', () { + expect(baseDate.addDays(5), DateTime(2025, 1, 20, 10, 30, 45)); + expect(baseDate.addDays(-5), DateTime(2025, 1, 10, 10, 30, 45)); + expect(baseDate.addDays(0), baseDate); + }); + + test('addMonths adds correct number of months', () { + expect(baseDate.addMonths(1), DateTime(2025, 2, 15, 10, 30, 45)); + expect(baseDate.addMonths(12), DateTime(2026, 1, 15, 10, 30, 45)); + expect(baseDate.addMonths(-1), DateTime(2024, 12, 15, 10, 30, 45)); + }); + + test('addYears adds correct number of years', () { + expect(baseDate.addYears(1), DateTime(2026, 1, 15, 10, 30, 45)); + expect(baseDate.addYears(10), DateTime(2035, 1, 15, 10, 30, 45)); + expect(baseDate.addYears(-5), DateTime(2020, 1, 15, 10, 30, 45)); + }); + + test('addHours adds correct number of hours', () { + expect(baseDate.addHours(2), DateTime(2025, 1, 15, 12, 30, 45)); + expect(baseDate.addHours(24), DateTime(2025, 1, 16, 10, 30, 45)); + expect(baseDate.addHours(-2), DateTime(2025, 1, 15, 8, 30, 45)); + }); + + test('addMinutes adds correct number of minutes', () { + expect(baseDate.addMinutes(30), DateTime(2025, 1, 15, 11, 0, 45)); + expect(baseDate.addMinutes(60), DateTime(2025, 1, 15, 11, 30, 45)); + expect(baseDate.addMinutes(-15), DateTime(2025, 1, 15, 10, 15, 45)); + }); + + test('addSeconds adds correct number of seconds', () { + expect(baseDate.addSeconds(15), DateTime(2025, 1, 15, 10, 31, 0)); + expect(baseDate.addSeconds(60), DateTime(2025, 1, 15, 10, 31, 45)); + expect(baseDate.addSeconds(-30), DateTime(2025, 1, 15, 10, 30, 15)); + }); + }); + + group('Subtracting time units', () { + test('subDays subtracts correct number of days', () { + expect(baseDate.subDays(5), DateTime(2025, 1, 10, 10, 30, 45)); + expect(baseDate.subDays(15), DateTime(2024, 12, 31, 10, 30, 45)); + expect(baseDate.subDays(0), baseDate); + }); + + test('subMonths subtracts correct number of months', () { + expect(baseDate.subMonths(1), DateTime(2024, 12, 15, 10, 30, 45)); + expect(baseDate.subMonths(12), DateTime(2024, 1, 15, 10, 30, 45)); + expect(baseDate.subMonths(0), baseDate); + }); + + test('subYears subtracts correct number of years', () { + expect(baseDate.subYears(1), DateTime(2024, 1, 15, 10, 30, 45)); + expect(baseDate.subYears(5), DateTime(2020, 1, 15, 10, 30, 45)); + expect(baseDate.subYears(0), baseDate); + }); + + test('subHours subtracts correct number of hours', () { + expect(baseDate.subHours(2), DateTime(2025, 1, 15, 8, 30, 45)); + expect(baseDate.subHours(11), DateTime(2025, 1, 14, 23, 30, 45)); + expect(baseDate.subHours(0), baseDate); + }); + + test('subMinutes subtracts correct number of minutes', () { + expect(baseDate.subMinutes(15), DateTime(2025, 1, 15, 10, 15, 45)); + expect(baseDate.subMinutes(31), DateTime(2025, 1, 15, 9, 59, 45)); + expect(baseDate.subMinutes(0), baseDate); + }); + + test('subSeconds subtracts correct number of seconds', () { + expect(baseDate.subSeconds(15), DateTime(2025, 1, 15, 10, 30, 30)); + expect(baseDate.subSeconds(46), DateTime(2025, 1, 15, 10, 29, 59)); + expect(baseDate.subSeconds(0), baseDate); + }); + }); + }); + + group('DateTime Check Extensions', () { + test('isInFuture returns true for future dates', () { + final futureDate = DateTime.now().add(Duration(days: 1)); + expect(futureDate.isInFuture, isTrue); + }); + + test('isInFuture returns false for past dates', () { + final pastDate = DateTime.now().subtract(Duration(days: 1)); + expect(pastDate.isInFuture, isFalse); + }); + + test('isInPast returns true for past dates', () { + final pastDate = DateTime.now().subtract(Duration(days: 1)); + expect(pastDate.isInPast, isTrue); + }); + + test('isInPast returns false for future dates', () { + final futureDate = DateTime.now().add(Duration(days: 1)); + expect(futureDate.isInPast, isFalse); + }); + + test('isSameDay returns true for same day dates', () { + final date1 = DateTime(2025, 1, 15, 10, 30); + final date2 = DateTime(2025, 1, 15, 18, 45); + expect(date1.isSameDay(date2), isTrue); + }); + + test('isSameDay returns false for different day dates', () { + final date1 = DateTime(2025, 1, 15); + final date2 = DateTime(2025, 1, 16); + expect(date1.isSameDay(date2), isFalse); + }); + + test('isToday returns true for today', () { + final today = DateTime.now(); + expect(today.isToday, isTrue); + }); + + test('isToday returns false for other days', () { + final yesterday = DateTime.now().subtract(Duration(days: 1)); + expect(yesterday.isToday, isFalse); + }); + + test('isTomorrow returns true for tomorrow', () { + final tomorrow = DateTime.now().add(Duration(days: 1)); + expect(tomorrow.isTomorrow, isTrue); + }); + + test('isTomorrow returns false for other days', () { + final today = DateTime.now(); + expect(today.isTomorrow, isFalse); + }); + + test('isYesterday returns true for yesterday', () { + final yesterday = DateTime.now().subtract(Duration(days: 1)); + expect(yesterday.isYesterday, isTrue); + }); + + test('isYesterday returns false for other days', () { + final today = DateTime.now(); + expect(today.isYesterday, isFalse); + }); + }); + + group('DateTime Utility Extensions', () { + test('startOfDay returns midnight of the same day', () { + final date = DateTime(2025, 1, 15, 10, 30, 45); + final startOfDay = date.startOfDay(); + expect(startOfDay, DateTime(2025, 1, 15, 0, 0, 0)); + expect(startOfDay.hour, 0); + expect(startOfDay.minute, 0); + expect(startOfDay.second, 0); + }); + + test('startOfDay works for dates at midnight', () { + final midnight = DateTime(2025, 1, 15); + expect(midnight.startOfDay(), midnight); + }); + }); + + group('DateTime Formatting Extensions', () { + final testDate = DateTime(2025, 1, 15, 14, 30, 45); + + test('format returns correctly formatted date', () { + expect(testDate.format('yyyy-MM-dd'), '2025-01-15'); + expect(testDate.format('dd/MM/yyyy'), '15/01/2025'); + expect(testDate.format('MMM dd, yyyy'), 'Jan 15, 2025'); + }); + + test('toTimeString returns time without seconds by default', () { + expect(testDate.toTimeString(), '14:30'); + }); + + test('toTimeString returns time with seconds when specified', () { + expect(testDate.toTimeString(withSeconds: true), '14:30:45'); + }); + + test('toDayOrdinal returns correct ordinal suffix', () { + expect(DateTime(2025, 1, 1).toDayOrdinal(), '1st'); + expect(DateTime(2025, 1, 2).toDayOrdinal(), '2nd'); + expect(DateTime(2025, 1, 3).toDayOrdinal(), '3rd'); + expect(DateTime(2025, 1, 4).toDayOrdinal(), '4th'); + expect(DateTime(2025, 1, 11).toDayOrdinal(), '11th'); + expect(DateTime(2025, 1, 12).toDayOrdinal(), '12th'); + expect(DateTime(2025, 1, 13).toDayOrdinal(), '13th'); + expect(DateTime(2025, 1, 21).toDayOrdinal(), '21st'); + expect(DateTime(2025, 1, 22).toDayOrdinal(), '22nd'); + expect(DateTime(2025, 1, 23).toDayOrdinal(), '23rd'); + expect(DateTime(2025, 1, 31).toDayOrdinal(), '31st'); + }); + + test('toShortDate returns formatted short date', () { + // Format is "E (day ordinal) MMM" + final result = testDate.toShortDate(); + expect(result, contains('Wed')); + expect(result, contains('15th')); + expect(result, contains('Jan')); + }); + + test('toTimeAgoString returns time ago format', () { + final recentDate = DateTime.now().subtract(Duration(minutes: 5)); + final result = recentDate.toTimeAgoString(); + expect(result, isNotEmpty); + // The exact format depends on the get_time_ago package + }); + }); + + group('DateTime Time of Day Extensions', () { + test('isNight returns true for night hours (0-6)', () { + expect(DateTime(2025, 1, 15, 0, 0).isNight, isTrue); + expect(DateTime(2025, 1, 15, 3, 30).isNight, isTrue); + expect(DateTime(2025, 1, 15, 5, 59).isNight, isTrue); + expect(DateTime(2025, 1, 15, 6, 0).isNight, isFalse); + }); + + test('isMorning returns true for morning hours (0-12)', () { + expect(DateTime(2025, 1, 15, 0, 0).isMorning, isTrue); + expect(DateTime(2025, 1, 15, 6, 0).isMorning, isTrue); + expect(DateTime(2025, 1, 15, 11, 59).isMorning, isTrue); + expect(DateTime(2025, 1, 15, 12, 0).isMorning, isFalse); + }); + + test('isAfternoon returns true for afternoon hours (12-18)', () { + expect(DateTime(2025, 1, 15, 12, 0).isAfternoon, isTrue); + expect(DateTime(2025, 1, 15, 15, 30).isAfternoon, isTrue); + expect(DateTime(2025, 1, 15, 17, 59).isAfternoon, isTrue); + expect(DateTime(2025, 1, 15, 18, 0).isAfternoon, isFalse); + }); + + test('isEvening returns true for evening hours (18-24)', () { + expect(DateTime(2025, 1, 15, 18, 0).isEvening, isTrue); + expect(DateTime(2025, 1, 15, 20, 30).isEvening, isTrue); + expect(DateTime(2025, 1, 15, 23, 59).isEvening, isTrue); + expect(DateTime(2025, 1, 15, 0, 0).isEvening, isFalse); + }); + }); + + group('DateTime Age Extensions', () { + test('toAge returns correct age', () { + final birthDate = DateTime.now().subtract(Duration(days: 365 * 25)); + expect(birthDate.toAge(), 25); + }); + + test('isAgeEqualTo returns true for matching age', () { + final birthDate = DateTime.now().subtract(Duration(days: 365 * 30)); + expect(birthDate.isAgeEqualTo(30), isTrue); + expect(birthDate.isAgeEqualTo(25), isFalse); + }); + + test('isAgeOlder returns true when age is greater', () { + final birthDate = DateTime.now().subtract(Duration(days: 365 * 30)); + expect(birthDate.isAgeOlder(25), isTrue); + expect(birthDate.isAgeOlder(30), isFalse); + expect(birthDate.isAgeOlder(35), isFalse); + }); + + test('isAgeYounger returns true when age is less', () { + final birthDate = DateTime.now().subtract(Duration(days: 365 * 30)); + expect(birthDate.isAgeYounger(35), isTrue); + expect(birthDate.isAgeYounger(30), isFalse); + expect(birthDate.isAgeYounger(25), isFalse); + }); + + test('isAgeBetween returns true when age is in range', () { + final birthDate = DateTime.now().subtract(Duration(days: 365 * 30)); + expect(birthDate.isAgeBetween(25, 35), isTrue); + expect(birthDate.isAgeBetween(30, 35), isTrue); + expect(birthDate.isAgeBetween(25, 30), isTrue); + expect(birthDate.isAgeBetween(20, 25), isFalse); + expect(birthDate.isAgeBetween(35, 40), isFalse); + }); + }); +} From 905aa8d2e94605aa8712526ba39b3459b80547a0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 7 Oct 2025 15:53:42 +0000 Subject: [PATCH 3/6] Update documentation: fix DateTime method names and add missing startOfDay Co-authored-by: YoungMayor <47315212+YoungMayor@users.noreply.github.com> --- CHANGELOG.md | 10 ++++++++++ README.md | 11 +++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a618809..23341ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Improved +- Added comprehensive test coverage for DateTime extensions + - Tests for all manipulation methods (addDays, addMonths, addYears, subDays, etc.) + - Tests for check methods (isToday, isTomorrow, isYesterday, isInFuture, isInPast) + - Tests for formatting methods (format, toTimeString, toShortDate, toDayOrdinal) + - Tests for time of day checks (isMorning, isAfternoon, isEvening, isNight) + - Tests for age calculations (toAge, isAgeOlder, isAgeYounger, isAgeBetween) + ## [0.4.0] - 6th October, 2025 ### Added diff --git a/README.md b/README.md index f7d0d8d..20a84cc 100644 --- a/README.md +++ b/README.md @@ -96,12 +96,15 @@ true.not; // false - `isAfternoon` – Checks if the time is between 12:00 PM and 5:59 PM. - `isMorning` – Checks if the time is before 12:00 PM. -- `isEvening` – Checks if the time is between 6:00 PM and 8:59 PM. -- `isNight` – Checks if the time is after 9:00 PM. +- `isEvening` – Checks if the time is between 6:00 PM and 11:59 PM. +- `isNight` – Checks if the time is between midnight and 5:59 AM. - `isToday` / `isTomorrow` / `isYesterday` – Quickly check the relation to the current day. - `isSameDay(DateTime other)` – Returns `true` if the date is the same calendar day as `other`. - `isInPast` / `isInFuture` – Check if the datetime is before or after now. +#### 🔧 Utilities +- `startOfDay()` – Returns the start of the day (midnight) for the datetime. + #### 🔧 Manipulators - `addDays(int)` / `addMonths(int)` / `addYears(int)` – Add to the datetime. @@ -117,7 +120,7 @@ true.not; // false #### 🧠 Time to String -- `toFormat(String format)` – Fully custom format using `intl`. +- `format(String format)` – Fully custom format using `intl`. > Popular date and time formats included in the [MayrDateTimeFormats] class. > > Currently includes: @@ -131,7 +134,7 @@ true.not; // false - `toDayOrdinal()` – Get the day of the month with ordinal (e.g. `1st`, `22nd`, `31st`). - `toTimeAgoString()` – Human-readable "time ago" format (e.g. "2 days ago"). - `toTimeString()` – Convert to time only (e.g. `14:35` or `14:35:59`). -- `toShortDate()` – Returns a short formatted date string (e.g. `Apr 25, 2025`). +- `toShortDate()` – Returns a short formatted date string (e.g. `Wed 15th Jan`). ------------------------------------------------------------------------------- From dbd9fb7267c3fe9031018c70f6547d3fc5ba5531 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 7 Oct 2025 15:57:19 +0000 Subject: [PATCH 4/6] Add comprehensive dartdoc comments to widget extensions Co-authored-by: YoungMayor <47315212+YoungMayor@users.noreply.github.com> --- lib/src/extensions/widget.dart | 120 +++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/lib/src/extensions/widget.dart b/lib/src/extensions/widget.dart index aa4300a..4f10329 100644 --- a/lib/src/extensions/widget.dart +++ b/lib/src/extensions/widget.dart @@ -1,24 +1,66 @@ part of './../extensions.dart'; extension MayrWidgetExtensions on Widget { + /// Wraps this widget in a [Center] widget. + /// + /// Example: + /// ```dart + /// Text('Hello').center(); + /// ``` Center center({double? heightFactor, double? widthFactor}) => Center(heightFactor: heightFactor, widthFactor: widthFactor, child: this); + /// Wraps this widget in an [Expanded] widget. + /// + /// Example: + /// ```dart + /// Text('Hello').expanded(2); // Takes 2x the space of other expanded widgets + /// ``` Expanded expanded([int flex = 1]) => Expanded(flex: flex, child: this); + /// Wraps this widget in a [Flexible] widget. + /// + /// Example: + /// ```dart + /// Text('Hello').flexible(flex: 2, fit: FlexFit.tight); + /// ``` Flexible flexible({int flex = 1, FlexFit fit = FlexFit.loose}) => Flexible(flex: 1, fit: fit, child: this); + /// Wraps this widget with an [InkWellManager] for touch interactions. + /// + /// Example: + /// ```dart + /// Text('Click me').inkWellManager(() => print('Tapped!')); + /// ``` InkWellManager inkWellManager( void Function()? callback, { Color color = Colors.transparent, }) => InkWellManager(this, callback, color: color); + /// Wraps this widget in an [Opacity] widget. + /// + /// Example: + /// ```dart + /// Text('Hello').opacity(0.5); // Makes text semi-transparent + /// ``` Opacity opacity(double opacity) => Opacity(opacity: opacity, child: this); + /// Wraps this widget in a [SizedBox] with optional width and height. + /// + /// Example: + /// ```dart + /// Text('Hello').sizedBox(width: 100, height: 50); + /// ``` Widget sizedBox({double? width, double? height}) => SizedBox(height: height, width: width, child: this); + /// Wraps this widget in a [ConstrainedBox] with optional constraints. + /// + /// Example: + /// ```dart + /// Text('Hello').constrained(maxWidth: 200, minHeight: 50); + /// ``` Widget constrained({ double? maxHeight, double? maxWidth, @@ -36,19 +78,49 @@ extension MayrWidgetExtensions on Widget { } extension MayrWidgetClipExtensions on Widget { + /// Wraps this widget in a [ClipRect] widget. + /// + /// Example: + /// ```dart + /// Image.network('url').clipRect(); + /// ``` ClipRect clipRect() => ClipRect(child: this); + /// Wraps this widget in a [ClipRRect] widget with custom border radius. + /// + /// Example: + /// ```dart + /// Image.network('url').clipRRect(BorderRadius.circular(20)); + /// ``` ClipRRect clipRRect(BorderRadiusGeometry borderRadius) => ClipRRect(borderRadius: borderRadius, child: this); + /// Wraps this widget in a [ClipRRect] widget with circular border radius. + /// + /// Example: + /// ```dart + /// Image.network('url').clipRounded(15); // 15 pixel radius + /// ``` ClipRRect clipRounded([double radius = 12]) => clipRRect(BorderRadius.circular(radius)); } extension MayrWidgetPaddingExtensions on Widget { + /// Wraps this widget in a [Padding] widget with equal padding on all sides. + /// + /// Example: + /// ```dart + /// Text('Hello').paddingAll(16.0); + /// ``` Widget paddingAll(double padding) => Padding(padding: EdgeInsets.all(padding), child: this); + /// Wraps this widget in a [Padding] widget with symmetric padding. + /// + /// Example: + /// ```dart + /// Text('Hello').paddingSymmetric(horizontal: 20, vertical: 10); + /// ``` Widget paddingSymmetric({double horizontal = 0.0, double vertical = 0.0}) => Padding( padding: EdgeInsets.symmetric( @@ -58,6 +130,12 @@ extension MayrWidgetPaddingExtensions on Widget { child: this, ); + /// Wraps this widget in a [Padding] widget with specific padding on each side. + /// + /// Example: + /// ```dart + /// Text('Hello').paddingOnly(left: 10, top: 5, right: 10, bottom: 5); + /// ``` Widget paddingOnly({ double left = 0.0, double top = 0.0, @@ -73,22 +151,64 @@ extension MayrWidgetPaddingExtensions on Widget { child: this, ); + /// Wraps this widget in a [Padding] widget with zero padding. + /// + /// Example: + /// ```dart + /// Text('Hello').paddingZero(); + /// ``` Widget paddingZero() => Padding(padding: EdgeInsets.zero, child: this); } extension MayrWidgetPositionExtensions on Widget { + /// Wraps this widget in an [Align] widget with the specified alignment. + /// + /// Example: + /// ```dart + /// Text('Hello').positionAlign(Alignment.topLeft); + /// ``` Align positionAlign(AlignmentGeometry alignment) => Align(alignment: alignment, child: this); + /// Wraps this widget in a [Positioned.fill] widget. + /// + /// Example: + /// ```dart + /// Container().positionedFill(); + /// ``` Positioned positionedFill() => Positioned.fill(child: this); } extension MayrWidgetVisibilityExtensions on Widget { + /// Hides this widget if the [condition] is true, otherwise shows it. + /// + /// Example: + /// ```dart + /// Text('Hello').hideIf(isHidden); + /// ``` Widget hideIf(bool condition) => showIf(!condition); + /// Hides this widget unless the [condition] is true. + /// + /// Example: + /// ```dart + /// Text('Hello').hideUnless(isVisible); + /// ``` Widget hideUnless(bool condition) => showIf(condition); + /// Shows this widget if the [condition] is true, otherwise hides it. + /// + /// Example: + /// ```dart + /// Text('Hello').showIf(isVisible); + /// ``` Widget showIf(bool condition) => condition ? this : const SizedBox.shrink(); + /// Shows this widget unless the [condition] is true. + /// + /// Example: + /// ```dart + /// Text('Hello').showUnless(isHidden); + /// ``` Widget showUnless(bool condition) => showIf(!condition); } From 80f32ac8a05aea07ecc0abc8b4c298ee8dc64117 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 7 Oct 2025 16:27:21 +0000 Subject: [PATCH 5/6] Fix addMonths, addYears, subMonths, subYears to preserve time components Co-authored-by: YoungMayor <47315212+YoungMayor@users.noreply.github.com> --- lib/src/extensions/date_time.dart | 44 ++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/src/extensions/date_time.dart b/lib/src/extensions/date_time.dart index be3d80f..3bf6d3f 100644 --- a/lib/src/extensions/date_time.dart +++ b/lib/src/extensions/date_time.dart @@ -75,13 +75,31 @@ extension MayrDateTimeManipulationExtensions on DateTime { DateTime addMinutes(int minutes) => add(Duration(minutes: minutes)); /// Add months to a [DateTime] - DateTime addMonths(int months) => DateTime(year, month + months, day); + DateTime addMonths(int months) => DateTime( + year, + month + months, + day, + hour, + minute, + second, + millisecond, + microsecond, + ); /// Add seconds to a [DateTime] DateTime addSeconds(int seconds) => add(Duration(seconds: seconds)); /// Add years to a [DateTime] - DateTime addYears(int years) => DateTime(year + years, month, day); + DateTime addYears(int years) => DateTime( + year + years, + month, + day, + hour, + minute, + second, + millisecond, + microsecond, + ); /// Subtract days from a [DateTime] DateTime subDays(int days) => subtract(Duration(days: days)); @@ -93,13 +111,31 @@ extension MayrDateTimeManipulationExtensions on DateTime { DateTime subMinutes(int minutes) => subtract(Duration(minutes: minutes)); /// Subtract months from a [DateTime] - DateTime subMonths(int months) => DateTime(year, month - months, day); + DateTime subMonths(int months) => DateTime( + year, + month - months, + day, + hour, + minute, + second, + millisecond, + microsecond, + ); /// Subtract seconds from a [DateTime] DateTime subSeconds(int seconds) => subtract(Duration(seconds: seconds)); /// Subtract years from a [DateTime] - DateTime subYears(int years) => DateTime(year - years, month, day); + DateTime subYears(int years) => DateTime( + year - years, + month, + day, + hour, + minute, + second, + millisecond, + microsecond, + ); } extension MayrDateTimeToStringExtensions on DateTime { From 27d62786f4e68470cb92f2c6b8a3ab10a38669db Mon Sep 17 00:00:00 2001 From: YoungMayor Date: Tue, 7 Oct 2025 17:38:10 +0100 Subject: [PATCH 6/6] Refactor DateTime extension formatting Reformatted the constructors in addMonths, addYears, subMonths, and subYears methods for improved readability and consistency in the MayrDateTimeManipulationExtensions extension. --- lib/src/extensions/date_time.dart | 72 +++++++++++++++---------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/lib/src/extensions/date_time.dart b/lib/src/extensions/date_time.dart index 3bf6d3f..a386bf4 100644 --- a/lib/src/extensions/date_time.dart +++ b/lib/src/extensions/date_time.dart @@ -76,30 +76,30 @@ extension MayrDateTimeManipulationExtensions on DateTime { /// Add months to a [DateTime] DateTime addMonths(int months) => DateTime( - year, - month + months, - day, - hour, - minute, - second, - millisecond, - microsecond, - ); + year, + month + months, + day, + hour, + minute, + second, + millisecond, + microsecond, + ); /// Add seconds to a [DateTime] DateTime addSeconds(int seconds) => add(Duration(seconds: seconds)); /// Add years to a [DateTime] DateTime addYears(int years) => DateTime( - year + years, - month, - day, - hour, - minute, - second, - millisecond, - microsecond, - ); + year + years, + month, + day, + hour, + minute, + second, + millisecond, + microsecond, + ); /// Subtract days from a [DateTime] DateTime subDays(int days) => subtract(Duration(days: days)); @@ -112,30 +112,30 @@ extension MayrDateTimeManipulationExtensions on DateTime { /// Subtract months from a [DateTime] DateTime subMonths(int months) => DateTime( - year, - month - months, - day, - hour, - minute, - second, - millisecond, - microsecond, - ); + year, + month - months, + day, + hour, + minute, + second, + millisecond, + microsecond, + ); /// Subtract seconds from a [DateTime] DateTime subSeconds(int seconds) => subtract(Duration(seconds: seconds)); /// Subtract years from a [DateTime] DateTime subYears(int years) => DateTime( - year - years, - month, - day, - hour, - minute, - second, - millisecond, - microsecond, - ); + year - years, + month, + day, + hour, + minute, + second, + millisecond, + microsecond, + ); } extension MayrDateTimeToStringExtensions on DateTime {