Skip to content

Commit 20851d9

Browse files
authored
Merge pull request flutter#2 from dart-lang/features
Add a zone-scoped clock and features from the old clock package
2 parents d001bf7 + 4522523 commit 20851d9

File tree

11 files changed

+656
-209
lines changed

11 files changed

+656
-209
lines changed

CHANGELOG.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
11
## 1.0.0
22

3-
* Initial version.
3+
This release contains the `Clock` class that was defined in [`quiver`][]. It's
4+
backwards-compatible with the `quiver` version, and *mostly*
5+
backwards-compatible with the old version of the `clock` package.
6+
7+
[`quiver`]: https://pub.dartlang.org/packages/quiver
8+
9+
### New Features
10+
11+
* A top-level `clock` field has been added that provides a default `Clock`
12+
implementation. It can be controlled by the `withClock()` function. It should
13+
generally be used in preference to manual dependency-injection, since it will
14+
work with the [`fake_async`][] package.
15+
16+
* A `Clock.stopwatch()` method has been added that creates a `Stopwatch` that
17+
uses the clock as its source of time.
18+
19+
[`fake_async`]: https://pub.dartlang.org/packages/fake_async
20+
21+
### Changes Relative to `clock` 0.1
22+
23+
* The top-level `new` getter and `getStopwatch()` methods are deprecated.
24+
`clock.new()` and `clock.stopwatch()` should be used instead.
25+
26+
* `Clock.getStopwatch()` is deprecated. `Clock.stopwatch()` should be used instead.
27+
28+
* The `isFinal` argument to `withClock()` is deprecated.
29+
30+
* `new Clock()` now takes an optional positional argument that returns the
31+
current time as a `DateTime` instead of its old arguments.
32+
33+
* `Clock.now()` is now a method rather than a getter.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'package:clock/clock.dart';
1515
1616
/// Runs [callback] and prints how long it took.
1717
T runWithTiming<T>(T callback()) {
18-
var stopwatch = clock.getStopwatch()..start();
18+
var stopwatch = clock.stopwatch()..start();
1919
var result = callback();
2020
print("It took ${stopwatch.elapsed}!");
2121
return result;

lib/clock.dart

Lines changed: 11 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -12,164 +12,23 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import 'src/utils.dart';
15+
export 'src/clock.dart';
16+
export 'src/default.dart';
17+
18+
import 'src/default.dart';
1619

1720
/// Returns current time.
1821
@Deprecated("Pass around an instance of Clock instead.")
1922
typedef DateTime TimeFunction();
2023

21-
/// Return current system time.
24+
/// Returns the current system time.
2225
@Deprecated("Use new DateTime.now() instead.")
2326
DateTime systemTime() => new DateTime.now();
2427

25-
/// A provider for the "current time" and points relative to the current time.
26-
///
27-
/// This class is designed with testability in mind. The current point in time
28-
/// (or [now()]) is defined by a function that returns a [DateTime]. By
29-
/// supplying your own time function or using [new Clock.fixed], you can control
30-
/// exactly what time a [Clock] returns and base your test expectations on that.
31-
class Clock {
32-
/// The function that's called to determine this clock's notion of the current
33-
/// time.
34-
final DateTime Function() _time;
35-
36-
/// Creates a clock based on the given [currentTime], or on the system clock
37-
/// by default.
38-
const Clock([DateTime currentTime()]) : _time = currentTime ?? systemTime;
39-
40-
/// Creates [Clock] that always considers the current time to be [time].
41-
Clock.fixed(DateTime time) : _time = (() => time);
42-
43-
/// Returns current time.
44-
DateTime now() => _time();
45-
46-
/// Returns the point in time [Duration] amount of time ago.
47-
DateTime agoBy(Duration duration) => now().subtract(duration);
48-
49-
/// Returns the point in time [Duration] amount of time from now.
50-
DateTime fromNowBy(Duration duration) => now().add(duration);
51-
52-
/// Returns the point in time that's given amount of time ago.
53-
///
54-
/// The amount of time is the sum of the individual parts.
55-
DateTime ago(
56-
{int days: 0,
57-
int hours: 0,
58-
int minutes: 0,
59-
int seconds: 0,
60-
int milliseconds: 0,
61-
int microseconds: 0}) =>
62-
agoBy(new Duration(
63-
days: days,
64-
hours: hours,
65-
minutes: minutes,
66-
seconds: seconds,
67-
milliseconds: milliseconds,
68-
microseconds: microseconds));
69-
70-
/// Returns the point in time that's given amount of time from now.
71-
///
72-
/// The amount of time is the sum of the individual parts.
73-
DateTime fromNow(
74-
{int days: 0,
75-
int hours: 0,
76-
int minutes: 0,
77-
int seconds: 0,
78-
int milliseconds: 0,
79-
int microseconds: 0}) =>
80-
fromNowBy(new Duration(
81-
days: days,
82-
hours: hours,
83-
minutes: minutes,
84-
seconds: seconds,
85-
milliseconds: milliseconds,
86-
microseconds: microseconds));
87-
88-
/// Return the point in time [microseconds] ago.
89-
DateTime microsAgo(int microseconds) => ago(microseconds: microseconds);
90-
91-
/// Return the point in time [microseconds] from now.
92-
DateTime microsFromNow(int microseconds) =>
93-
fromNow(microseconds: microseconds);
94-
95-
/// Return the point in time [milliseconds] ago.
96-
DateTime millisAgo(int milliseconds) => ago(milliseconds: milliseconds);
97-
98-
/// Return the point in time [milliseconds] from now.
99-
DateTime millisFromNow(int milliseconds) =>
100-
fromNow(milliseconds: milliseconds);
101-
102-
/// Return the point in time [seconds] ago.
103-
DateTime secondsAgo(int seconds) => ago(seconds: seconds);
104-
105-
/// Return the point in time [seconds] from now.
106-
DateTime secondsFromNow(int seconds) => fromNow(seconds: seconds);
107-
108-
/// Return the point in time [minutes] ago.
109-
DateTime minutesAgo(int minutes) => ago(minutes: minutes);
110-
111-
/// Return the point in time [minutes] from now.
112-
DateTime minutesFromNow(int minutes) => fromNow(minutes: minutes);
113-
114-
/// Return the point in time [hours] ago.
115-
DateTime hoursAgo(int hours) => ago(hours: hours);
116-
117-
/// Return the point in time [hours] from now.
118-
DateTime hoursFromNow(int hours) => fromNow(hours: hours);
119-
120-
/// Return the point in time [days] ago.
121-
DateTime daysAgo(int days) => ago(days: days);
122-
123-
/// Return the point in time [days] from now.
124-
DateTime daysFromNow(int days) => fromNow(days: days);
125-
126-
/// Return the point in time [weeks] ago.
127-
DateTime weeksAgo(int weeks) => ago(days: 7 * weeks);
128-
129-
/// Return the point in time [weeks] from now.
130-
DateTime weeksFromNow(int weeks) => fromNow(days: 7 * weeks);
131-
132-
/// Return the point in time [months] ago on the same date.
133-
///
134-
/// If the current day of the month isn't valid in the new month, the nearest
135-
/// valid day in the new month will be used.
136-
DateTime monthsAgo(int months) {
137-
var time = now();
138-
var month = (time.month - months - 1) % 12 + 1;
139-
var year = time.year - (months + 12 - time.month) ~/ 12;
140-
var day = clampDayOfMonth(year: year, month: month, day: time.day);
141-
return new DateTime(year, month, day, time.hour, time.minute, time.second,
142-
time.millisecond);
143-
}
144-
145-
/// Return the point in time [months] from now on the same date.
146-
///
147-
/// If the current day of the month isn't valid in the new month, the nearest
148-
/// valid day in the new month will be used.
149-
DateTime monthsFromNow(int months) {
150-
var time = now();
151-
var month = (time.month + months - 1) % 12 + 1;
152-
var year = time.year + (months + time.month - 1) ~/ 12;
153-
var day = clampDayOfMonth(year: year, month: month, day: time.day);
154-
return new DateTime(year, month, day, time.hour, time.minute, time.second,
155-
time.millisecond);
156-
}
157-
158-
/// Return the point in time [years] ago on the same date.
159-
///
160-
/// If the current day of the month isn't valid in the new year, the nearest
161-
/// valid day in the original month will be used.
162-
DateTime yearsAgo(int years) {
163-
var time = now();
164-
var year = time.year - years;
165-
var day = clampDayOfMonth(year: year, month: time.month, day: time.day);
166-
return new DateTime(year, time.month, day, time.hour, time.minute,
167-
time.second, time.millisecond);
168-
}
28+
/// Returns the current time as reported by [clock].
29+
@Deprecated("Use clock.now() instead.")
30+
DateTime get now => clock.now();
16931

170-
/// Return the point in time [years] from now on the same date.
171-
///
172-
/// If the current day of the month isn't valid in the new year, the nearest
173-
/// valid day in the original month will be used.
174-
DateTime yearsFromNow(int years) => yearsAgo(-years);
175-
}
32+
/// Returns a stopwatch that uses the current time as reported by [clock].
33+
@Deprecated("Use clock.stopwatch() instead.")
34+
Stopwatch getStopwatch() => clock.stopwatch();

0 commit comments

Comments
 (0)