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

Side by Side 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 comment. 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « frog/leg/lib/mockimpl.dart ('k') | tests/corelib/corelib.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // TODO(jmesserly): the native class should be the real JS Date. 5 // TODO(jmesserly): the native class should be the real JS Date.
6 // TODO(jimhug): Making the date value non-lazy might be a good path there. 6 // TODO(jimhug): Making the date value non-lazy might be a good path there.
7 class DateImplementation implements Date { 7 class DateImplementation implements Date {
8 final int value; 8 final int value;
9 final TimeZoneImplementation timeZone; 9 final TimeZoneImplementation timeZone;
10 10
(...skipping 24 matching lines...) Expand all
35 timeZone.isUtc) { 35 timeZone.isUtc) {
36 _asJs(); 36 _asJs();
37 } 37 }
38 38
39 DateImplementation.now() 39 DateImplementation.now()
40 : timeZone = new TimeZone.local(), 40 : timeZone = new TimeZone.local(),
41 value = _now() { 41 value = _now() {
42 _asJs(); 42 _asJs();
43 } 43 }
44 44
45 DateImplementation.fromString(String formattedString) 45 factory DateImplementation.fromString(String formattedString) {
46 : timeZone = new TimeZone.local(), 46 // JavaScript's parse function is not specified and there are differences
47 value = _valueFromString(formattedString) { 47 // between the different implementations. Make sure we can at least read in
48 _asJs(); 48 // Dart's output: try to read in (a subset of) ISO 8601 first. If that fails
49 // fall back to JavaScript's implementation.
50 final RegExp re =
51 const RegExp(@'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d) (\d\d):(\d\d):(\d\d)( ?:.(\d{1,3}))? ?([zZ]?)$');
52 Match match = re.firstMatch(formattedString);
53 if (match !== null) {
54 int years = Math.parseInt(match[1]);
55 int month = Math.parseInt(match[2]);
56 int day = Math.parseInt(match[3]);
57 int hours = Math.parseInt(match[4]);
58 int minutes = Math.parseInt(match[5]);
59 int seconds = Math.parseInt(match[6]);
60 int milliseconds = 0;
61 if (match[7] !== null) {
62 milliseconds = Math.parseInt(match[7]);
63 if (match[7].length == 1) {
64 milliseconds *= 100;
65 } else if (match[7].length == 2) {
66 milliseconds *= 10;
67 } else {
68 assert(match[7].length == 3);
69 }
70 }
71 bool isUtc = match[8] !== null;
72 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local();
73 return new DateImplementation.withTimeZone(
74 years, month, day, hours, minutes, seconds, milliseconds, timezone);
75 } else {
76 return new DateImplementation.fromEpoch(formattedString,
77 new TimeZone.local());
78 }
49 } 79 }
50 80
51 const DateImplementation.fromEpoch(this.value, this.timeZone); 81 const DateImplementation.fromEpoch(this.value, this.timeZone);
52 82
53 bool operator ==(other) { 83 bool operator ==(other) {
54 if (!(other is DateImplementation)) return false; 84 if (!(other is DateImplementation)) return false;
55 return (value == other.value) && (timeZone == other.timeZone); 85 return (value == other.value) && (timeZone == other.timeZone);
56 } 86 }
57 87
58 int compareTo(Date other) { 88 int compareTo(Date other) {
59 return value.compareTo(other.value); 89 return value.compareTo(other.value);
60 } 90 }
61 91
62 Date changeTimeZone(TimeZone targetTimeZone) { 92 Date changeTimeZone(TimeZone targetTimeZone) {
63 if (targetTimeZone == null) { 93 if (targetTimeZone == null) {
64 targetTimeZone = new TimeZoneImplementation.local(); 94 targetTimeZone = new TimeZoneImplementation.local();
65 } 95 }
66 return new Date.fromEpoch(value, targetTimeZone); 96 return new Date.fromEpoch(value, targetTimeZone);
67 } 97 }
68 98
69 int get year() native 99 int get year() native
70 '''return this.isUtc ? this._asJs().getUTCFullYear() : 100 '''return this.isUtc() ? this._asJs().getUTCFullYear() :
71 this._asJs().getFullYear();'''; 101 this._asJs().getFullYear();''' {
102 isUtc();
103 _asJs();
104 }
72 105
73 int get month() native 106 int get month() native
74 '''return this.isUtc ? this._asJs().getMonth() + 1 : 107 '''return this.isUtc() ? this._asJs().getUTCMonth() + 1 :
75 this._asJs().getMonth() + 1;'''; 108 this._asJs().getMonth() + 1;''' {
109 isUtc();
110 _asJs();
111 }
76 112
77 int get day() native 113 int get day() native
78 'return this.isUtc ? this._asJs().getUTCDate() : this._asJs().getDate()'; 114 '''return this.isUtc() ? this._asJs().getUTCDate() :
115 this._asJs().getDate();''' {
116 isUtc();
117 _asJs();
118 }
79 119
80 int get hours() native 120 int get hours() native
81 'return this.isUtc ? this._asJs().getUTCHours() : this._asJs().getHours()'; 121 '''return this.isUtc() ? this._asJs().getUTCHours() :
122 this._asJs().getHours();''' {
123 isUtc();
124 _asJs();
125 }
82 126
83 int get minutes() native 127 int get minutes() native
84 'return this.isUtc ? this._asJs().getUTCMinutes() : this._asJs().getMinutes( )'; 128 '''return this.isUtc() ? this._asJs().getUTCMinutes() :
129 this._asJs().getMinutes();''' {
130 isUtc();
131 _asJs();
132 }
85 133
86 int get seconds() native 134 int get seconds() native
87 'return this.isUtc ? this._asJs().getUTCSeconds() : this._asJs().getSeconds( )'; 135 '''return this.isUtc() ? this._asJs().getUTCSeconds() :
136 this._asJs().getSeconds();''' {
137 isUtc();
138 _asJs();
139 }
88 140
89 int get milliseconds() native 141 int get milliseconds() native
90 '''return this.isUtc ? this._asJs().getUTCMilliseconds() : 142 '''return this.isUtc() ? this._asJs().getUTCMilliseconds() :
91 this._asJs().getMilliseconds();'''; 143 this._asJs().getMilliseconds();''' {
144 isUtc();
145 _asJs();
146 }
92 147
93 // Adjust by one because JS weeks start on Sunday. 148 // Adjust by one because JS weeks start on Sunday.
94 int get weekday() native ''' 149 int get weekday() native '''
95 var day = this.isUtc ? this._asJs().getUTCDay() : this._asJs().getDay(); 150 var day = this.isUtc() ? this._asJs().getUTCDay() : this._asJs().getDay();
96 return (day + 6) % 7;'''; 151 return (day + 6) % 7;''';
97 152
98 // TODO(jimhug): Could this please be getters? 153 // TODO(jimhug): Could this please be getters?
99 bool isLocalTime() { 154 bool isLocalTime() {
100 return !timeZone.isUtc; 155 return !timeZone.isUtc;
101 } 156 }
102 157
103 bool isUtc() { 158 bool isUtc() {
104 return timeZone.isUtc; 159 return timeZone.isUtc;
105 } 160 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 return isUtc == other.isUtc; 239 return isUtc == other.isUtc;
185 } 240 }
186 241
187 String toString() { 242 String toString() {
188 if (isUtc) return "TimeZone (UTC)"; 243 if (isUtc) return "TimeZone (UTC)";
189 return "TimeZone (Local)"; 244 return "TimeZone (Local)";
190 } 245 }
191 246
192 final bool isUtc; 247 final bool isUtc;
193 } 248 }
OLDNEW
« no previous file with comments | « frog/leg/lib/mockimpl.dart ('k') | tests/corelib/corelib.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698