Chromium Code Reviews| Index: frog/leg/lib/mockimpl.dart |
| diff --git a/frog/leg/lib/mockimpl.dart b/frog/leg/lib/mockimpl.dart |
| index 9f37f8f4d262aa63b2d9852f97ff4b02817356f0..556cb81a07dde5eaf21130d99d56f1664728b943 100644 |
| --- a/frog/leg/lib/mockimpl.dart |
| +++ b/frog/leg/lib/mockimpl.dart |
| @@ -236,38 +236,66 @@ class DateImplementation implements Date { |
| } |
| factory DateImplementation.fromString(String formattedString) { |
| - // JavaScript's parse function is not specified and there are differences |
| - // between the different implementations. Make sure we can at least read in |
| - // Dart's output: try to read in (a subset of) ISO 8601 first. If that fails |
| - // fall back to JavaScript's implementation. |
| - final RegExp re = |
| - const RegExp(@'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d) (\d\d):(\d\d):(\d\d)(?:.(\d{1,3}))? ?([zZ]?)$'); |
| + // Read in (a subset of) ISO 8601. |
| + // Examples: |
| + // - "2012-02-27 13:27:00" |
| + // - "2012-02-27 13:27:00.423z" |
| + // - "20120227 13:27:00" |
| + // - "20120227T132700" |
| + // - "20120227" |
| + // - "2012-02-27T14Z" |
| + // - "-123450101 00:00:00 Z" // In the year -12345. |
| + final RegExp re = const RegExp( |
| + @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' + // The day part. |
| + @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:.(\d{1,5}))?)?)? ?([zZ])?)?$'); |
| Match match = re.firstMatch(formattedString); |
| if (match !== null) { |
| + int parseIntOrZero(String matched) { |
| + // TODO(floitsch): we should not need to test against the empty string. |
| + if (matched === null || matched == "") return 0; |
| + return Math.parseInt(matched); |
| + } |
| + |
| int years = Math.parseInt(match[1]); |
| int month = Math.parseInt(match[2]); |
| int day = Math.parseInt(match[3]); |
| - int hours = Math.parseInt(match[4]); |
| - int minutes = Math.parseInt(match[5]); |
| - int seconds = Math.parseInt(match[6]); |
| - int milliseconds = 0; |
| - if (match[7] !== null) { |
| - milliseconds = Math.parseInt(match[7]); |
| + int hours = parseIntOrZero(match[4]); |
| + int minutes = parseIntOrZero(match[5]); |
| + int seconds = parseIntOrZero(match[6]); |
| + bool addOneMillisecond = false; |
| + int milliseconds = parseIntOrZero(match[7]); |
| + if (milliseconds != 0) { |
| if (match[7].length == 1) { |
| milliseconds *= 100; |
| } else if (match[7].length == 2) { |
| milliseconds *= 10; |
| + } else if (match[7].length == 3) { |
| + // Do nothing. |
| + } else if (match[7].length == 4) { |
| + addOneMillisecond = ((milliseconds % 10) >= 5); |
| + milliseconds ~/= 10; |
| } else { |
| - assert(match[7].length == 3); |
| + assert(match[7].length == 5); |
| + addOneMillisecond = ((milliseconds %100) >= 50); |
|
kasperl
2012/03/02 13:34:10
%100 -> % 100
floitsch
2012/03/02 17:00:29
Done.
|
| + milliseconds ~/= 100; |
| + } |
| + if (addOneMillisecond && milliseconds < 999) { |
| + addOneMillisecond = false; |
| + milliseconds++; |
| } |
| } |
| + // TODO(floitsch): we should not need to test against the empty string. |
| bool isUtc = (match[8] !== null) && (match[8] != ""); |
| TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); |
| - return new DateImplementation.withTimeZone( |
| - years, month, day, hours, minutes, seconds, milliseconds, timezone); |
| + int epochValue = Primitives.valueFromDecomposedDate( |
| + years, month, day, hours, minutes, seconds, milliseconds, isUtc); |
| + if (epochValue === null) { |
| + throw new IllegalArgumentException(formattedString); |
| + } |
| + if (addOneMillisecond) epochValue++; |
| + return new DateImplementation.fromEpoch(epochValue, timezone); |
| } else { |
| - return new DateImplementation.fromEpoch(formattedString, |
| - new TimeZone.local()); |
| + throw new IllegalArgumentException(formattedString); |
| } |
| } |
| @@ -319,6 +347,7 @@ class DateImplementation implements Date { |
| } |
| String toString() { |
| + String y = fourDigits(year); |
| String m = twoDigits(month); |
| String d = twoDigits(day); |
| String h = twoDigits(hours); |
| @@ -326,9 +355,9 @@ class DateImplementation implements Date { |
| String sec = twoDigits(seconds); |
| String ms = threeDigits(milliseconds); |
| if (timeZone.isUtc) { |
| - return "$year-$m-$d $h:$min:$sec.${ms}Z"; |
| + return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
| } else { |
| - return "$year-$m-$d $h:$min:$sec.$ms"; |
| + return "$y-$m-$d $h:$min:$sec.$ms"; |
| } |
| } |