| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * General purpose date/time utilities. | 6 * General purpose date/time utilities. |
| 7 */ | 7 */ |
| 8 class DateUtils { | 8 class DateUtils { |
| 9 // TODO(jmesserly): localized strings | 9 // TODO(jmesserly): localized strings |
| 10 static final WEEKDAYS = const ['Monday', 'Tuesday', 'Wednesday', 'Thursday', | 10 static final WEEKDAYS = const ['Monday', 'Tuesday', 'Wednesday', 'Thursday', |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 */ | 116 */ |
| 117 static String toRecentTimeString(Date then) { | 117 static String toRecentTimeString(Date then) { |
| 118 bool datesAreEqual(Date d1, Date d2) { | 118 bool datesAreEqual(Date d1, Date d2) { |
| 119 return (d1.year == d2.year) && (d1.month == d2.month) && | 119 return (d1.year == d2.year) && (d1.month == d2.month) && |
| 120 (d1.day == d2.day); | 120 (d1.day == d2.day); |
| 121 } | 121 } |
| 122 | 122 |
| 123 final now = new Date.now(); | 123 final now = new Date.now(); |
| 124 if (datesAreEqual(then, now)) { | 124 if (datesAreEqual(then, now)) { |
| 125 return toHourMinutesString(new Duration( | 125 return toHourMinutesString(new Duration( |
| 126 0, then.hours, then.minutes, then.seconds, then.milliseconds)); | 126 0, then.hour, then.minute, then.second, then.millisecond)); |
| 127 } | 127 } |
| 128 | 128 |
| 129 final today = new Date(now.year, now.month, now.day, 0, 0, 0, 0); | 129 final today = new Date(now.year, now.month, now.day, 0, 0, 0, 0); |
| 130 Duration delta = today.difference(then); | 130 Duration delta = today.difference(then); |
| 131 if (delta.inMilliseconds < Duration.MILLISECONDS_PER_DAY) { | 131 if (delta.inMilliseconds < Duration.MILLISECONDS_PER_DAY) { |
| 132 return YESTERDAY; | 132 return YESTERDAY; |
| 133 } else if (delta.inMilliseconds < MS_IN_WEEK) { | 133 } else if (delta.inMilliseconds < MS_IN_WEEK) { |
| 134 return WEEKDAYS[getWeekday(then)]; | 134 return WEEKDAYS[getWeekday(then)]; |
| 135 } else { | 135 } else { |
| 136 // TODO(jmesserly): locale specific date format | 136 // TODO(jmesserly): locale specific date format |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 173 } |
| 174 String twoDigits(int n) { | 174 String twoDigits(int n) { |
| 175 if (n >= 10) return "${n}"; | 175 if (n >= 10) return "${n}"; |
| 176 return "0${n}"; | 176 return "0${n}"; |
| 177 } | 177 } |
| 178 String mm = | 178 String mm = |
| 179 twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR)); | 179 twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR)); |
| 180 return "${hours}:${mm} ${a}"; | 180 return "${hours}:${mm} ${a}"; |
| 181 } | 181 } |
| 182 } | 182 } |
| OLD | NEW |