fix(clock): keep micros in monthsAgo, monthsFromNow and yearsAgo (#1202)
Signed-off-by: Goddchen <goddchen@gmail.com>
Co-authored-by: Moritz <mosum@google.com>
diff --git a/pkgs/clock/CHANGELOG.md b/pkgs/clock/CHANGELOG.md
index f372ada..eb4f9ab 100644
--- a/pkgs/clock/CHANGELOG.md
+++ b/pkgs/clock/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 1.1.3-wip
+
+* Keep microseconds when using `monthsAgo`, `monthsFromNow` and `yearsAgo`
+
## 1.1.2
* Require Dart 3.4
diff --git a/pkgs/clock/lib/src/clock.dart b/pkgs/clock/lib/src/clock.dart
index f6f47de..9cad995 100644
--- a/pkgs/clock/lib/src/clock.dart
+++ b/pkgs/clock/lib/src/clock.dart
@@ -139,7 +139,7 @@
var year = time.year - (months + 12 - time.month) ~/ 12;
var day = clampDayOfMonth(year: year, month: month, day: time.day);
return DateTime(year, month, day, time.hour, time.minute, time.second,
- time.millisecond);
+ time.millisecond, time.microsecond);
}
/// Return the point in time [months] from now on the same date.
@@ -152,7 +152,7 @@
var year = time.year + (months + time.month - 1) ~/ 12;
var day = clampDayOfMonth(year: year, month: month, day: time.day);
return DateTime(year, month, day, time.hour, time.minute, time.second,
- time.millisecond);
+ time.millisecond, time.microsecond);
}
/// Return the point in time [years] ago on the same date.
@@ -164,7 +164,7 @@
var year = time.year - years;
var day = clampDayOfMonth(year: year, month: time.month, day: time.day);
return DateTime(year, time.month, day, time.hour, time.minute, time.second,
- time.millisecond);
+ time.millisecond, time.microsecond);
}
/// Return the point in time [years] from now on the same date.
diff --git a/pkgs/clock/pubspec.yaml b/pkgs/clock/pubspec.yaml
index 605aa09..7d21e58 100644
--- a/pkgs/clock/pubspec.yaml
+++ b/pkgs/clock/pubspec.yaml
@@ -1,5 +1,5 @@
name: clock
-version: 1.1.2
+version: 1.1.3-wip
description: A fakeable wrapper for dart:core clock APIs.
repository: https://github.com/dart-lang/tools/tree/main/pkgs/clock
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aclock
diff --git a/pkgs/clock/test/clock_test.dart b/pkgs/clock/test/clock_test.dart
index c457153..f722739 100644
--- a/pkgs/clock/test/clock_test.dart
+++ b/pkgs/clock/test/clock_test.dart
@@ -207,4 +207,22 @@
expect(clock.yearsFromNow(30), date(2043, 1, 1));
expect(clock.yearsFromNow(1000), date(3013, 1, 1));
});
+
+ group('micros', () {
+ test('should keep micros for monthsAgo', () {
+ expect(Clock.fixed(DateTime(2024, 2, 1, 0, 0, 0, 0, 123)).monthsAgo(1),
+ Clock.fixed(DateTime(2024, 1, 1, 0, 0, 0, 0, 123)).now());
+ });
+
+ test('should keep micros for monthsFromNow', () {
+ expect(
+ Clock.fixed(DateTime(2024, 2, 1, 0, 0, 0, 0, 123)).monthsFromNow(1),
+ Clock.fixed(DateTime(2024, 3, 1, 0, 0, 0, 0, 123)).now());
+ });
+
+ test('should keep micros for yearsAgo', () {
+ expect(Clock.fixed(DateTime(2024, 2, 1, 0, 0, 0, 0, 123)).yearsAgo(1),
+ Clock.fixed(DateTime(2023, 2, 1, 0, 0, 0, 0, 123)).now());
+ });
+ });
}