Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clock): keep micros in monthsAgo, monthsFromNow and yearsAgo #1202

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions pkgs/clock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.3-wip

* Keep microseconds when using `monthsAgo`, `monthsFromNow` and `yearsAgo`

## 1.1.2

* Require Dart 3.4
Expand Down
6 changes: 3 additions & 3 deletions pkgs/clock/lib/src/clock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Clock {
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.
Expand All @@ -152,7 +152,7 @@ class Clock {
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.
Expand All @@ -164,7 +164,7 @@ class Clock {
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.
Expand Down
2 changes: 1 addition & 1 deletion pkgs/clock/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions pkgs/clock/test/clock_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,22 @@ void main() {
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());
});
});
}