| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class DateImplementation implements Date { | 5 class DateImplementation implements Date { |
| 6 final int millisecondsSinceEpoch; | 6 final int millisecondsSinceEpoch; |
| 7 final bool isUtc; | 7 final bool isUtc; |
| 8 | 8 |
| 9 factory DateImplementation.fromString(String formattedString) { | 9 factory DateImplementation.fromString(String formattedString) { |
| 10 // Read in (a subset of) ISO 8601. | 10 // Read in (a subset of) ISO 8601. |
| 11 // Examples: | 11 // Examples: |
| 12 // - "2012-02-27 13:27:00" | 12 // - "2012-02-27 13:27:00" |
| 13 // - "2012-02-27 13:27:00.423z" | 13 // - "2012-02-27 13:27:00.423z" |
| 14 // - "20120227 13:27:00" | 14 // - "20120227 13:27:00" |
| 15 // - "20120227T132700" | 15 // - "20120227T132700" |
| 16 // - "20120227" | 16 // - "20120227" |
| 17 // - "2012-02-27T14Z" | 17 // - "2012-02-27T14Z" |
| 18 // - "-123450101 00:00:00 Z" // In the year -12345. | 18 // - "-123450101 00:00:00 Z" // In the year -12345. |
| 19 final RegExp re = const RegExp( | 19 final RegExp re = const RegExp( |
| 20 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. | 20 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. |
| 21 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); | 21 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); |
| 22 Match match = re.firstMatch(formattedString); | 22 Match match = re.firstMatch(formattedString); |
| 23 if (match !== null) { | 23 if (match !== null) { |
| 24 int parseIntOrZero(String matched) { | 24 int parseIntOrZero(String matched) { |
| 25 // TODO(floitsch): we should not need to test against the empty string. | 25 // TODO(floitsch): we should not need to test against the empty string. |
| 26 if (matched === null || matched == "") return 0; | 26 if (matched === null || matched == "") return 0; |
| 27 return Math.parseInt(matched); | 27 return Math.parseInt(matched); |
| 28 } | 28 } |
| 29 | 29 |
| 30 double parseDoubleOrZero(String matched) { | 30 double parseDoubleOrZero(String matched) { |
| 31 // TODO(floitsch): we should not need to test against the empty string. | 31 // TODO(floitsch): we should not need to test against the empty string. |
| 32 if (matched === null || matched == "") return 0.0; | 32 if (matched === null || matched == "") return 0.0; |
| 33 return Math.parseDouble(matched); | 33 return Math.parseDouble(matched); |
| 34 } | 34 } |
| 35 | 35 |
| 36 int years = Math.parseInt(match[1]); | 36 int years = Math.parseInt(match[1]); |
| 37 int month = Math.parseInt(match[2]); | 37 int month = Math.parseInt(match[2]); |
| 38 int day = Math.parseInt(match[3]); | 38 int day = Math.parseInt(match[3]); |
| 39 int hour = parseIntOrZero(match[4]); | 39 int hour = parseIntOrZero(match[4]); |
| 40 int minute = parseIntOrZero(match[5]); | 40 int minute = parseIntOrZero(match[5]); |
| 41 int second = parseIntOrZero(match[6]); | 41 int second = parseIntOrZero(match[6]); |
| 42 bool addOneMillisecond = false; | 42 bool addOneMillisecond = false; |
| 43 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); | 43 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); |
| 44 if (millisecond == 1000) { | 44 if (millisecond == 1000) { |
| 45 addOneMillisecond = true; | 45 addOneMillisecond = true; |
| 46 millisecond = 999; | 46 millisecond = 999; |
| 47 } | 47 } |
| 48 // TODO(floitsch): we should not need to test against the empty string. | 48 // TODO(floitsch): we should not need to test against the empty string. |
| 49 bool isUtc = (match[8] !== null) && (match[8] != ""); | 49 bool isUtc = (match[8] !== null) && (match[8] != ""); |
| 50 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | 50 int millisecondsSinceEpoch = Primitives.valueFromDecomposedDate( |
| 51 years, month, day, hour, minute, second, millisecond, isUtc); | 51 years, month, day, hour, minute, second, millisecond, isUtc); |
| 52 if (millisecondsSinceEpoch === null) { | 52 if (millisecondsSinceEpoch === null) { |
| 53 throw new IllegalArgumentException(formattedString); | 53 throw new IllegalArgumentException(formattedString); |
| 54 } | 54 } |
| 55 if (addOneMillisecond) millisecondsSinceEpoch++; | 55 if (addOneMillisecond) millisecondsSinceEpoch++; |
| 56 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc); | 56 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc); |
| 57 } else { | 57 } else { |
| 58 throw new IllegalArgumentException(formattedString); | 58 throw new IllegalArgumentException(formattedString); |
| 59 } | 59 } |
| 60 } | 60 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 String min = twoDigits(minute); | 131 String min = twoDigits(minute); |
| 132 String sec = twoDigits(second); | 132 String sec = twoDigits(second); |
| 133 String ms = threeDigits(millisecond); | 133 String ms = threeDigits(millisecond); |
| 134 if (isUtc) { | 134 if (isUtc) { |
| 135 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | 135 return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
| 136 } else { | 136 } else { |
| 137 return "$y-$m-$d $h:$min:$sec.$ms"; | 137 return "$y-$m-$d $h:$min:$sec.$ms"; |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 | 140 |
| 141 /** Returns a new [Date] with the [duration] added to [this]. */ | 141 // Adds the [duration] to this Date instance. |
| 142 Date add(Duration duration) { | 142 Date add(Duration duration) { |
| 143 int ms = millisecondsSinceEpoch; | 143 int ms = millisecondsSinceEpoch; |
| 144 return new Date.fromMillisecondsSinceEpoch( | 144 return new Date.fromMillisecondsSinceEpoch( |
| 145 ms + duration.inMilliseconds, isUtc); | 145 ms + duration.inMilliseconds, isUtc); |
| 146 } | 146 } |
| 147 | 147 |
| 148 /** Returns a new [Date] with the [duration] subtracted from [this]. */ | 148 // Subtracts the [duration] from this Date instance. |
| 149 Date subtract(Duration duration) { | 149 Date subtract(Duration duration) { |
| 150 int ms = millisecondsSinceEpoch; | 150 int ms = millisecondsSinceEpoch; |
| 151 return new Date.fromMillisecondsSinceEpoch( | 151 return new Date.fromMillisecondsSinceEpoch( |
| 152 ms - duration.inMilliseconds, isUtc); | 152 ms - duration.inMilliseconds, isUtc); |
| 153 } | 153 } |
| 154 | 154 |
| 155 /** Returns a [Duration] with the difference of [this] and [other]. */ | 155 // Returns a [Duration] with the difference of [this] and [other]. |
| 156 Duration difference(Date other) { | 156 Duration difference(Date other) { |
| 157 int ms = millisecondsSinceEpoch; | 157 int ms = millisecondsSinceEpoch; |
| 158 int otherMs = other.millisecondsSinceEpoch; | 158 int otherMs = other.millisecondsSinceEpoch; |
| 159 return new Duration(milliseconds: ms - otherMs); | 159 return new Duration(milliseconds: ms - otherMs); |
| 160 } | 160 } |
| 161 | 161 |
| 162 external DateImplementation(int year, | 162 external DateImplementation(int year, |
| 163 [int month, | 163 [int month, |
| 164 int day, | 164 int day, |
| 165 int hour, | 165 int hour, |
| 166 int minute, | 166 int minute, |
| 167 int second, | 167 int second, |
| 168 int millisecond, | 168 int millisecond, |
| 169 bool isUtc]); | 169 bool isUtc]); |
| 170 external DateImplementation.now(); | 170 external DateImplementation.now(); |
| 171 external static int _brokenDownDateToMillisecondsSinceEpoch( | |
| 172 int years, int month, int day, int hour, int minute, int second, | |
| 173 int millisecond, bool isUtc); | |
| 174 external String get timeZoneName(); | 171 external String get timeZoneName(); |
| 175 external Duration get timeZoneOffset(); | 172 external Duration get timeZoneOffset(); |
| 176 external int get year(); | 173 external int get year(); |
| 177 external int get month(); | 174 external int get month(); |
| 178 external int get day(); | 175 external int get day(); |
| 179 external int get hour(); | 176 external int get hour(); |
| 180 external int get minute(); | 177 external int get minute(); |
| 181 external int get second(); | 178 external int get second(); |
| 182 external int get millisecond(); | 179 external int get millisecond(); |
| 183 external int get weekday(); | 180 external int get weekday(); |
| 184 } | 181 } |
| OLD | NEW |