|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -import 'src/utils.dart'; |
| 15 | +export 'src/clock.dart'; |
| 16 | +export 'src/default.dart'; |
| 17 | + |
| 18 | +import 'src/default.dart'; |
16 | 19 |
|
17 | 20 | /// Returns current time. |
18 | 21 | @Deprecated("Pass around an instance of Clock instead.") |
19 | 22 | typedef DateTime TimeFunction(); |
20 | 23 |
|
21 | | -/// Return current system time. |
| 24 | +/// Returns the current system time. |
22 | 25 | @Deprecated("Use new DateTime.now() instead.") |
23 | 26 | DateTime systemTime() => new DateTime.now(); |
24 | 27 |
|
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(); |
169 | 31 |
|
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