Skip to content

Commit c6a1061

Browse files
Goddchenmosuem
andauthored
fix(clock): keep micros in monthsAgo, monthsFromNow and yearsAgo (#1202)
Signed-off-by: Goddchen <[email protected]> Co-authored-by: Moritz <[email protected]>
1 parent f1f8ac1 commit c6a1061

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

pkgs/clock/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.3-wip
2+
3+
* Keep microseconds when using `monthsAgo`, `monthsFromNow` and `yearsAgo`
4+
15
## 1.1.2
26

37
* Require Dart 3.4

pkgs/clock/lib/src/clock.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Clock {
139139
var year = time.year - (months + 12 - time.month) ~/ 12;
140140
var day = clampDayOfMonth(year: year, month: month, day: time.day);
141141
return DateTime(year, month, day, time.hour, time.minute, time.second,
142-
time.millisecond);
142+
time.millisecond, time.microsecond);
143143
}
144144

145145
/// Return the point in time [months] from now on the same date.
@@ -152,7 +152,7 @@ class Clock {
152152
var year = time.year + (months + time.month - 1) ~/ 12;
153153
var day = clampDayOfMonth(year: year, month: month, day: time.day);
154154
return DateTime(year, month, day, time.hour, time.minute, time.second,
155-
time.millisecond);
155+
time.millisecond, time.microsecond);
156156
}
157157

158158
/// Return the point in time [years] ago on the same date.
@@ -164,7 +164,7 @@ class Clock {
164164
var year = time.year - years;
165165
var day = clampDayOfMonth(year: year, month: time.month, day: time.day);
166166
return DateTime(year, time.month, day, time.hour, time.minute, time.second,
167-
time.millisecond);
167+
time.millisecond, time.microsecond);
168168
}
169169

170170
/// Return the point in time [years] from now on the same date.

pkgs/clock/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: clock
2-
version: 1.1.2
2+
version: 1.1.3-wip
33
description: A fakeable wrapper for dart:core clock APIs.
44
repository: https://github.com/dart-lang/tools/tree/main/pkgs/clock
55
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aclock

pkgs/clock/test/clock_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,22 @@ void main() {
207207
expect(clock.yearsFromNow(30), date(2043, 1, 1));
208208
expect(clock.yearsFromNow(1000), date(3013, 1, 1));
209209
});
210+
211+
group('micros', () {
212+
test('should keep micros for monthsAgo', () {
213+
expect(Clock.fixed(DateTime(2024, 2, 1, 0, 0, 0, 0, 123)).monthsAgo(1),
214+
Clock.fixed(DateTime(2024, 1, 1, 0, 0, 0, 0, 123)).now());
215+
});
216+
217+
test('should keep micros for monthsFromNow', () {
218+
expect(
219+
Clock.fixed(DateTime(2024, 2, 1, 0, 0, 0, 0, 123)).monthsFromNow(1),
220+
Clock.fixed(DateTime(2024, 3, 1, 0, 0, 0, 0, 123)).now());
221+
});
222+
223+
test('should keep micros for yearsAgo', () {
224+
expect(Clock.fixed(DateTime(2024, 2, 1, 0, 0, 0, 0, 123)).yearsAgo(1),
225+
Clock.fixed(DateTime(2023, 2, 1, 0, 0, 0, 0, 123)).now());
226+
});
227+
});
210228
}

0 commit comments

Comments
 (0)