Chromium Code Reviews| 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 part of utilslib; | 5 part of utilslib; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * General purpose date/time utilities. | 8 * General purpose date/time utilities. |
| 9 */ | 9 */ |
| 10 class DateUtils { | 10 class DateUtils { |
| 11 // TODO(jmesserly): localized strings | 11 // TODO(jmesserly): localized strings |
| 12 static const WEEKDAYS = const ['Monday', 'Tuesday', 'Wednesday', 'Thursday', | 12 static const WEEKDAYS = const ['Monday', 'Tuesday', 'Wednesday', 'Thursday', |
| 13 'Friday', 'Saturday', 'Sunday']; | 13 'Friday', 'Saturday', 'Sunday']; |
| 14 | 14 |
| 15 static const YESTERDAY = 'Yesterday'; | 15 static const YESTERDAY = 'Yesterday'; |
| 16 | 16 |
| 17 static const MS_IN_WEEK = DateTime.DAYS_IN_WEEK * Duration.MILLISECONDS_PER_DA Y; | 17 static const MS_IN_WEEK = DateTime.DAYS_PER_WEEK * Duration.MILLISECONDS_PER_D AY; |
|
Anders Johnsen
2013/04/15 10:28:06
Long line.
floitsch
2013/04/15 11:28:00
Done.
| |
| 18 | 18 |
| 19 // TODO(jmesserly): workaround for missing DateTime.fromDate in Dartium | 19 // TODO(jmesserly): workaround for missing DateTime.fromDate in Dartium |
| 20 // Remove this once that is implemented. See b/5055106 | 20 // Remove this once that is implemented. See b/5055106 |
| 21 // Parse a string like: "Mon, 27 Jun 2011 15:22:00 -0700" | 21 // Parse a string like: "Mon, 27 Jun 2011 15:22:00 -0700" |
| 22 static DateTime fromString(String text) { | 22 static DateTime fromString(String text) { |
| 23 final parts = text.split(' '); | 23 final parts = text.split(' '); |
| 24 if (parts.length == 1) { | 24 if (parts.length == 1) { |
| 25 return _parseIsoDate(text); | 25 return _parseIsoDate(text); |
| 26 } | 26 } |
| 27 | 27 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 | 150 |
| 151 // TODO(jmesserly): this is a workaround for unimplemented DateTime.weekday | 151 // TODO(jmesserly): this is a workaround for unimplemented DateTime.weekday |
| 152 // Code inspired by v8/src/date.js | 152 // Code inspired by v8/src/date.js |
| 153 static int getWeekday(DateTime dateTime) { | 153 static int getWeekday(DateTime dateTime) { |
| 154 final unixTimeStart = new DateTime(1970, 1, 1, 0, 0, 0, 0); | 154 final unixTimeStart = new DateTime(1970, 1, 1, 0, 0, 0, 0); |
| 155 int msSince1970 = dateTime.difference(unixTimeStart).inMilliseconds; | 155 int msSince1970 = dateTime.difference(unixTimeStart).inMilliseconds; |
| 156 int daysSince1970 = msSince1970 ~/ Duration.MILLISECONDS_PER_DAY; | 156 int daysSince1970 = msSince1970 ~/ Duration.MILLISECONDS_PER_DAY; |
| 157 // 1970-1-1 was Thursday | 157 // 1970-1-1 was Thursday |
| 158 return ((daysSince1970 + DateTime.THURSDAY) % DateTime.DAYS_IN_WEEK); | 158 return ((daysSince1970 + DateTime.THURSDAY) % DateTime.DAYS_PER_WEEK); |
| 159 } | 159 } |
| 160 | 160 |
| 161 /** Formats a time in H:MM A format */ | 161 /** Formats a time in H:MM A format */ |
| 162 // TODO(jmesserly): should get 12 vs 24 hour clock setting from the locale | 162 // TODO(jmesserly): should get 12 vs 24 hour clock setting from the locale |
| 163 static String toHourMinutesString(Duration duration) { | 163 static String toHourMinutesString(Duration duration) { |
| 164 assert(duration.inDays == 0); | 164 assert(duration.inDays == 0); |
| 165 int hours = duration.inHours; | 165 int hours = duration.inHours; |
| 166 String a; | 166 String a; |
| 167 if (hours >= 12) { | 167 if (hours >= 12) { |
| 168 a = 'pm'; | 168 a = 'pm'; |
| 169 if (hours != 12) { | 169 if (hours != 12) { |
| 170 hours -= 12; | 170 hours -= 12; |
| 171 } | 171 } |
| 172 } else { | 172 } else { |
| 173 a = 'am'; | 173 a = 'am'; |
| 174 if (hours == 0) { | 174 if (hours == 0) { |
| 175 hours += 12; | 175 hours += 12; |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 String twoDigits(int n) { | 178 String twoDigits(int n) { |
| 179 if (n >= 10) return "${n}"; | 179 if (n >= 10) return "${n}"; |
| 180 return "0${n}"; | 180 return "0${n}"; |
| 181 } | 181 } |
| 182 String mm = | 182 String mm = |
| 183 twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR)); | 183 twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR)); |
| 184 return "${hours}:${mm} ${a}"; | 184 return "${hours}:${mm} ${a}"; |
| 185 } | 185 } |
| 186 } | 186 } |
| OLD | NEW |