| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 int hours = Math.parseInt(timeParts[0]); | 49 int hours = Math.parseInt(timeParts[0]); |
| 50 int minutes = Math.parseInt(timeParts[1]); | 50 int minutes = Math.parseInt(timeParts[1]); |
| 51 int seconds = Math.parseInt(timeParts[2]); | 51 int seconds = Math.parseInt(timeParts[2]); |
| 52 | 52 |
| 53 // TODO(jmesserly): TimeZone is not implemented in Dartium. This ugly | 53 // TODO(jmesserly): TimeZone is not implemented in Dartium. This ugly |
| 54 // hack applies the timezone from the string to the final time | 54 // hack applies the timezone from the string to the final time |
| 55 int zoneOffset = Math.parseInt(parts[5]) ~/ 100; | 55 int zoneOffset = Math.parseInt(parts[5]) ~/ 100; |
| 56 | 56 |
| 57 // Pretend it's a UTC time | 57 // Pretend it's a UTC time |
| 58 Date result = new Date.withTimeZone( | 58 Date result = new Date( |
| 59 year, month, day, hours, minutes, seconds, 0, new TimeZone.utc()); | 59 year, month, day, hours, minutes, seconds, 0, isUtc: true); |
| 60 // Shift it to the proper zone, but it's still a UTC time | 60 // Shift it to the proper zone, but it's still a UTC time |
| 61 result = result.subtract(new Duration(hours: zoneOffset)); | 61 result = result.subtract(new Duration(hours: zoneOffset)); |
| 62 // Then render it as a local time | 62 // Then render it as a local time |
| 63 return result.changeTimeZone(new TimeZone.local()); | 63 return result.toLocal(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 /** Parse a string like: 2011-07-19T22:03:04.000Z */ | 66 /** Parse a string like: 2011-07-19T22:03:04.000Z */ |
| 67 // TODO(jmesserly): workaround for Date.fromDate, which has issues: | 67 // TODO(jmesserly): workaround for Date.fromDate, which has issues: |
| 68 // * on Dart VM it doesn't handle all of ISO 8601. See b/5055106. | 68 // * on Dart VM it doesn't handle all of ISO 8601. See b/5055106. |
| 69 // * on DartC it doesn't work on Safari. See b/5062557. | 69 // * on DartC it doesn't work on Safari. See b/5062557. |
| 70 // Remove this once that function is fully implemented | 70 // Remove this once that function is fully implemented |
| 71 static Date _parseIsoDate(String text) { | 71 static Date _parseIsoDate(String text) { |
| 72 void ensure(bool value) { | 72 void ensure(bool value) { |
| 73 if (!value) { | 73 if (!value) { |
| 74 throw 'bad date format, expected YYYY-MM-DDTHH:MM:SS.mmmZ: ' + text; | 74 throw 'bad date format, expected YYYY-MM-DDTHH:MM:SS.mmmZ: ' + text; |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 TimeZone zone; | 78 bool isUtc = text.endsWith('Z'); |
| 79 if (text.endsWith('Z')) { | 79 if (isUtc) { |
| 80 text = text.substring(0, text.length - 1); | 80 text = text.substring(0, text.length - 1); |
| 81 zone = new TimeZone.utc(); | |
| 82 } else { | |
| 83 zone = new TimeZone.local(); | |
| 84 } | 81 } |
| 85 | 82 |
| 86 final parts = text.split('T'); | 83 final parts = text.split('T'); |
| 87 ensure(parts.length == 2); | 84 ensure(parts.length == 2); |
| 88 | 85 |
| 89 final date = parts[0].split('-'); | 86 final date = parts[0].split('-'); |
| 90 ensure(date.length == 3); | 87 ensure(date.length == 3); |
| 91 | 88 |
| 92 final time = parts[1].split(':'); | 89 final time = parts[1].split(':'); |
| 93 ensure(time.length == 3); | 90 ensure(time.length == 3); |
| 94 | 91 |
| 95 final seconds = time[2].split('.'); | 92 final seconds = time[2].split('.'); |
| 96 ensure(seconds.length >= 1 && seconds.length <= 2); | 93 ensure(seconds.length >= 1 && seconds.length <= 2); |
| 97 int milliseconds = 0; | 94 int milliseconds = 0; |
| 98 if (seconds.length == 2) { | 95 if (seconds.length == 2) { |
| 99 milliseconds = Math.parseInt(seconds[1]); | 96 milliseconds = Math.parseInt(seconds[1]); |
| 100 } | 97 } |
| 101 | 98 |
| 102 return new Date.withTimeZone( | 99 return new Date( |
| 103 Math.parseInt(date[0]), | 100 Math.parseInt(date[0]), |
| 104 Math.parseInt(date[1]), | 101 Math.parseInt(date[1]), |
| 105 Math.parseInt(date[2]), | 102 Math.parseInt(date[2]), |
| 106 Math.parseInt(time[0]), | 103 Math.parseInt(time[0]), |
| 107 Math.parseInt(time[1]), | 104 Math.parseInt(time[1]), |
| 108 Math.parseInt(seconds[0]), | 105 Math.parseInt(seconds[0]), |
| 109 milliseconds, | 106 milliseconds, |
| 110 zone); | 107 isUtc: isUtc); |
| 111 } | 108 } |
| 112 | 109 |
| 113 /** | 110 /** |
| 114 * A date/time formatter that takes into account the current date/time: | 111 * A date/time formatter that takes into account the current date/time: |
| 115 * - if it's from today, just show the time | 112 * - if it's from today, just show the time |
| 116 * - if it's from yesterday, just show 'Yesterday' | 113 * - if it's from yesterday, just show 'Yesterday' |
| 117 * - if it's from the same week, just show the weekday | 114 * - if it's from the same week, just show the weekday |
| 118 * - otherwise, show just the date | 115 * - otherwise, show just the date |
| 119 */ | 116 */ |
| 120 static String toRecentTimeString(Date then) { | 117 static String toRecentTimeString(Date then) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 173 } |
| 177 String twoDigits(int n) { | 174 String twoDigits(int n) { |
| 178 if (n >= 10) return "${n}"; | 175 if (n >= 10) return "${n}"; |
| 179 return "0${n}"; | 176 return "0${n}"; |
| 180 } | 177 } |
| 181 String mm = | 178 String mm = |
| 182 twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR)); | 179 twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR)); |
| 183 return "${hours}:${mm} ${a}"; | 180 return "${hours}:${mm} ${a}"; |
| 184 } | 181 } |
| 185 } | 182 } |
| OLD | NEW |