| OLD | NEW |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 part of quiver.time; | 15 part of quiver.time; |
| 16 | 16 |
| 17 /// Returns current time. | 17 /// Returns current time. |
| 18 typedef DateTime TimeFunction(); | 18 typedef DateTime TimeFunction(); |
| 19 | 19 |
| 20 /// Return current system time. | 20 /// Return current system time. |
| 21 DateTime systemTime() => new DateTime.now(); | 21 DateTime systemTime() => new DateTime.now(); |
| 22 | 22 |
| 23 /// Days in a month. This array uses 1-based month numbers, i.e. January is | 23 /// Days in a month. This array uses 1-based month numbers, i.e. January is |
| 24 /// the 1-st element in the array, not the 0-th. | 24 /// the 1-st element in the array, not the 0-th. |
| 25 const _DAYS_IN_MONTH = | 25 const _DAYS_IN_MONTH = const [ |
| 26 const [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; | 26 0, |
| 27 31, |
| 28 28, |
| 29 31, |
| 30 30, |
| 31 31, |
| 32 30, |
| 33 31, |
| 34 31, |
| 35 30, |
| 36 31, |
| 37 30, |
| 38 31 |
| 39 ]; |
| 27 | 40 |
| 28 int _daysInMonth(int year, int month) => (month == DateTime.FEBRUARY && | 41 int _daysInMonth(int year, int month) => |
| 29 _isLeapYear(year)) ? 29 : _DAYS_IN_MONTH[month]; | 42 (month == DateTime.FEBRUARY && _isLeapYear(year)) |
| 43 ? 29 |
| 44 : _DAYS_IN_MONTH[month]; |
| 30 | 45 |
| 31 bool _isLeapYear(int year) => | 46 bool _isLeapYear(int year) => |
| 32 (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); | 47 (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); |
| 33 | 48 |
| 34 /// Takes a [date] that may be outside the allowed range of dates for a given | 49 /// Takes a [date] that may be outside the allowed range of dates for a given |
| 35 /// [month] in a given [year] and returns the closest date that is within the | 50 /// [month] in a given [year] and returns the closest date that is within the |
| 36 /// allowed range. | 51 /// allowed range. |
| 37 /// | 52 /// |
| 38 /// For example: | 53 /// For example: |
| 39 /// | 54 /// |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 91 |
| 77 /// Returns the point in time [Duration] amount of time ago. | 92 /// Returns the point in time [Duration] amount of time ago. |
| 78 DateTime agoBy(Duration duration) => now().subtract(duration); | 93 DateTime agoBy(Duration duration) => now().subtract(duration); |
| 79 | 94 |
| 80 /// Returns the point in time [Duration] amount of time from now. | 95 /// Returns the point in time [Duration] amount of time from now. |
| 81 DateTime fromNowBy(Duration duration) => now().add(duration); | 96 DateTime fromNowBy(Duration duration) => now().add(duration); |
| 82 | 97 |
| 83 /// Returns the point in time that's given amount of time ago. The | 98 /// Returns the point in time that's given amount of time ago. The |
| 84 /// amount of time is the sum of individual parts. Parts are compatible with | 99 /// amount of time is the sum of individual parts. Parts are compatible with |
| 85 /// ones defined in [Duration]. | 100 /// ones defined in [Duration]. |
| 86 DateTime ago({int days: 0, int hours: 0, int minutes: 0, int seconds: 0, | 101 DateTime ago( |
| 87 int milliseconds: 0, int microseconds: 0}) => agoBy(new Duration( | 102 {int days: 0, |
| 103 int hours: 0, |
| 104 int minutes: 0, |
| 105 int seconds: 0, |
| 106 int milliseconds: 0, |
| 107 int microseconds: 0}) => |
| 108 agoBy(new Duration( |
| 88 days: days, | 109 days: days, |
| 89 hours: hours, | 110 hours: hours, |
| 90 minutes: minutes, | 111 minutes: minutes, |
| 91 seconds: seconds, | 112 seconds: seconds, |
| 92 milliseconds: milliseconds, | 113 milliseconds: milliseconds, |
| 93 microseconds: microseconds)); | 114 microseconds: microseconds)); |
| 94 | 115 |
| 95 /// Returns the point in time that's given amount of time from now. The | 116 /// Returns the point in time that's given amount of time from now. The |
| 96 /// amount of time is the sum of individual parts. Parts are compatible with | 117 /// amount of time is the sum of individual parts. Parts are compatible with |
| 97 /// ones defined in [Duration]. | 118 /// ones defined in [Duration]. |
| 98 DateTime fromNow({int days: 0, int hours: 0, int minutes: 0, int seconds: 0, | 119 DateTime fromNow( |
| 99 int milliseconds: 0, int microseconds: 0}) => fromNowBy(new Duration( | 120 {int days: 0, |
| 121 int hours: 0, |
| 122 int minutes: 0, |
| 123 int seconds: 0, |
| 124 int milliseconds: 0, |
| 125 int microseconds: 0}) => |
| 126 fromNowBy(new Duration( |
| 100 days: days, | 127 days: days, |
| 101 hours: hours, | 128 hours: hours, |
| 102 minutes: minutes, | 129 minutes: minutes, |
| 103 seconds: seconds, | 130 seconds: seconds, |
| 104 milliseconds: milliseconds, | 131 milliseconds: milliseconds, |
| 105 microseconds: microseconds)); | 132 microseconds: microseconds)); |
| 106 | 133 |
| 107 /// Return the point in time [micros] microseconds ago. | 134 /// Return the point in time [micros] microseconds ago. |
| 108 DateTime microsAgo(int micros) => ago(microseconds: micros); | 135 DateTime microsAgo(int micros) => ago(microseconds: micros); |
| 109 | 136 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 var time = now(); | 198 var time = now(); |
| 172 var y = time.year - years; | 199 var y = time.year - years; |
| 173 var d = _clampDate(time.day, y, time.month); | 200 var d = _clampDate(time.day, y, time.month); |
| 174 return new DateTime(y, time.month, d, time.hour, time.minute, time.second, | 201 return new DateTime(y, time.month, d, time.hour, time.minute, time.second, |
| 175 time.millisecond); | 202 time.millisecond); |
| 176 } | 203 } |
| 177 | 204 |
| 178 /// Return the point in time [years] from now on the same date. | 205 /// Return the point in time [years] from now on the same date. |
| 179 DateTime yearsFromNow(int years) => yearsAgo(-years); | 206 DateTime yearsFromNow(int years) => yearsAgo(-years); |
| 180 } | 207 } |
| OLD | NEW |