Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Unified Diff: frog/lib/date_implementation.dart

Issue 9466048: Implement (subset of) ISO 8601 for date-reading. Fix other bugs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add assert. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: frog/lib/date_implementation.dart
diff --git a/frog/lib/date_implementation.dart b/frog/lib/date_implementation.dart
index 6769e5d4d1fd9103c171ce3b3a7778cc9e4ed8b4..01e8b12365fddbbb0139030bb6013c9d561e7d7a 100644
--- a/frog/lib/date_implementation.dart
+++ b/frog/lib/date_implementation.dart
@@ -42,10 +42,38 @@ class DateImplementation implements Date {
_asJs();
}
- DateImplementation.fromString(String formattedString)
- : timeZone = new TimeZone.local(),
- value = _valueFromString(formattedString) {
- _asJs();
+ factory DateImplementation.fromString(String formattedString) {
+ // Always 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]?)$');
+ Match match = re.firstMatch(formattedString);
+ if (match !== null) {
+ 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]);
+ if (match[7].length == 1) {
+ milliseconds *= 100;
+ } else if (match[7].length == 2) {
+ milliseconds *= 10;
+ } else {
+ assert(match[7].length == 3);
+ }
+ }
+ bool isUtc = match[8] !== null;
+ TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local();
+ return new DateImplementation.withTimeZone(
+ years, month, day, hours, minutes, seconds, milliseconds, timezone);
+ } else {
+ return new DateImplementation.fromEpoch(formattedString,
+ new TimeZone.local());
+ }
}
const DateImplementation.fromEpoch(this.value, this.timeZone);
@@ -67,32 +95,57 @@ class DateImplementation implements Date {
}
int get year() native
- '''return this.isUtc ? this._asJs().getUTCFullYear() :
- this._asJs().getFullYear();''';
+ '''return this.isUtc() ? this._asJs().getUTCFullYear() :
+ this._asJs().getFullYear();''' {
+ isUtc();
+ _asJs();
+ }
int get month() native
- '''return this.isUtc ? this._asJs().getMonth() + 1 :
- this._asJs().getMonth() + 1;''';
+ '''return this.isUtc() ? this._asJs().getUTCMonth() + 1 :
+ this._asJs().getMonth() + 1;''' {
+ isUtc();
+ _asJs();
+ }
int get day() native
- 'return this.isUtc ? this._asJs().getUTCDate() : this._asJs().getDate()';
+ '''return this.isUtc() ? this._asJs().getUTCDate() :
+ this._asJs().getDate();''' {
+ isUtc();
+ _asJs();
+ }
int get hours() native
- 'return this.isUtc ? this._asJs().getUTCHours() : this._asJs().getHours()';
+ '''return this.isUtc() ? this._asJs().getUTCHours() :
+ this._asJs().getHours();''' {
+ isUtc();
+ _asJs();
+ }
int get minutes() native
- 'return this.isUtc ? this._asJs().getUTCMinutes() : this._asJs().getMinutes()';
+ '''return this.isUtc() ? this._asJs().getUTCMinutes() :
+ this._asJs().getMinutes();''' {
+ isUtc();
+ _asJs();
+ }
int get seconds() native
- 'return this.isUtc ? this._asJs().getUTCSeconds() : this._asJs().getSeconds()';
+ '''return this.isUtc() ? this._asJs().getUTCSeconds() :
+ this._asJs().getSeconds();''' {
+ isUtc();
+ _asJs();
+ }
int get milliseconds() native
- '''return this.isUtc ? this._asJs().getUTCMilliseconds() :
- this._asJs().getMilliseconds();''';
+ '''return this.isUtc() ? this._asJs().getUTCMilliseconds() :
+ this._asJs().getMilliseconds();''' {
+ isUtc();
+ _asJs();
+ }
// Adjust by one because JS weeks start on Sunday.
int get weekday() native '''
- var day = this.isUtc ? this._asJs().getUTCDay() : this._asJs().getDay();
+ var day = this.isUtc() ? this._asJs().getUTCDay() : this._asJs().getDay();
return (day + 6) % 7;''';
// TODO(jimhug): Could this please be getters?

Powered by Google App Engine
This is Rietveld 408576698