| // Copyright 2013 Google Inc. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| part of quiver.time; |
| |
| /// Returns current time. |
| typedef DateTime TimeFunction(); |
| |
| /// Provides points in time relative to the current point in time, for example: |
| /// now, 2 days ago, 4 weeks from now, etc. |
| /// |
| /// This class is designed with testability in mind. The current point in time |
| /// (or [now()]) is defined by a [TimeFunction]. By supplying your own time |
| /// function or by using fixed clock (see constructors), you can control |
| /// exactly what time a [Clock] returns and base your test expectations on |
| /// that. See specific constructors for how to supply time functions. |
| class Clock { |
| |
| final TimeFunction _time; |
| |
| /// Creates [Clock] based on the system clock. |
| Clock() : _time = (() => new DateTime.now()); |
| |
| /// Creates [Clock] based on user-defined [TimeFunction]. For example, in |
| /// unit-tests you might want to control what time it is now and set date and |
| /// time expectations. |
| Clock.custom(this._time); |
| |
| /// Creates [Clock] that returns fixed [time] value. Useful in unit-tests. |
| Clock.fixed(DateTime time) : _time = (() => time); |
| |
| /// Returns current time. |
| DateTime now() => _time(); |
| |
| /// Returns the point in time [Duration] amount of time ago. |
| DateTime ago(Duration duration) => now().subtract(duration); |
| |
| /// Returns the point in time [Duration] amount of time from now. |
| DateTime fromNow(Duration duration) => now().add(duration); |
| |
| /// Return the point in time [micros] microseconds ago. |
| DateTime microsAgo(int micros) => ago(new Duration(microseconds: micros)); |
| |
| /// Return the point in time [micros] microseconds from now. |
| DateTime microsFromNow(int micros) => microsAgo(-micros); |
| |
| /// Return the point in time [millis] milliseconds ago. |
| DateTime millisAgo(int millis) => ago(new Duration(milliseconds: millis)); |
| |
| /// Return the point in time [millis] milliseconds from now. |
| DateTime millisFromNow(int millis) => millisAgo(-millis); |
| |
| /// Return the point in time [seconds] ago. |
| DateTime secondsAgo(int seconds) => ago(new Duration(seconds: seconds)); |
| |
| /// Return the point in time [seconds] from now. |
| DateTime secondsFromNow(int seconds) => secondsAgo(-seconds); |
| |
| /// Return the point in time [minutes] ago. |
| DateTime minutesAgo(int minutes) => ago(new Duration(minutes: minutes)); |
| |
| /// Return the point in time [minutes] from now. |
| DateTime minutesFromNow(int minutes) => minutesAgo(-minutes); |
| |
| /// Return the point in time [hours] ago. |
| DateTime hoursAgo(int hours) => ago(new Duration(hours: hours)); |
| |
| /// Return the point in time [hours] from now. |
| DateTime hoursFromNow(int hours) => hoursAgo(-hours); |
| |
| /// Return the point in time [days] ago. |
| DateTime daysAgo(int days) => ago(new Duration(days: days)); |
| |
| /// Return the point in time [days] from now. |
| DateTime daysFromNow(int days) => daysAgo(-days); |
| |
| /// Return the point in time [weeks] ago. |
| DateTime weeksAgo(int weeks) => ago(new Duration(days: 7 * weeks)); |
| |
| /// Return the point in time [weeks] from now. |
| DateTime weeksFromNow(int weeks) => weeksAgo(-weeks); |
| |
| /// Return the point in time [months] ago on the same date. |
| DateTime monthsAgo(int months) { |
| var time = now(); |
| return new DateTime( |
| time.year, |
| time.month - months, |
| time.day, |
| time.hour, |
| time.minute, |
| time.second, |
| time.millisecond |
| ); |
| } |
| |
| /// Return the point in time [months] from now on the same date. |
| DateTime monthsFromNow(int months) => monthsAgo(-months); |
| |
| /// Return the point in time [years] ago on the same date. |
| DateTime yearsAgo(int years) { |
| var time = now(); |
| return new DateTime( |
| time.year - years, |
| time.month, |
| time.day, |
| time.hour, |
| time.minute, |
| time.second, |
| time.millisecond |
| ); |
| } |
| |
| /// Return the point in time [years] from now on the same date. |
| DateTime yearsFromNow(int years) => yearsAgo(-years); |
| } |