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

Side by Side Diff: frog/lib/date_implementation.dart

Issue 10548047: Remove frog from the repository. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Move test and update apidoc.gyp. Created 8 years, 6 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/lib/corelib_impl.dart ('k') | frog/lib/frog_coreimpl_sources.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
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.
7 class DateImplementation implements Date {
8 final int value;
9 final TimeZone timeZone;
10
11 factory DateImplementation(int years,
12 [int month = 1,
13 int day = 1,
14 int hours = 0,
15 int minutes = 0,
16 int seconds = 0,
17 int milliseconds = 0,
18 bool isUtc = false]) {
19 return new DateImplementation.withTimeZone(
20 years, month, day,
21 hours, minutes, seconds, milliseconds,
22 isUtc ? const TimeZone.utc()
23 : new TimeZone.local());
24 }
25
26 DateImplementation.withTimeZone(int years,
27 int month,
28 int day,
29 int hours,
30 int minutes,
31 int seconds,
32 int milliseconds,
33 TimeZone timeZone)
34 : this.timeZone = timeZone,
35 value = _valueFromDecomposed(years, month, day,
36 hours, minutes, seconds, milliseconds,
37 timeZone.isUtc) {
38 if (value === null) throw new IllegalArgumentException();
39 _asJs();
40 }
41
42 DateImplementation.now()
43 : timeZone = new TimeZone.local(),
44 value = _now() {
45 _asJs();
46 }
47
48 factory DateImplementation.fromString(String formattedString) {
49 // Read in (a subset of) ISO 8601.
50 // Examples:
51 // - "2012-02-27 13:27:00"
52 // - "2012-02-27 13:27:00.423z"
53 // - "20120227 13:27:00"
54 // - "20120227T132700"
55 // - "20120227"
56 // - "2012-02-27T14Z"
57 // - "-123450101 00:00:00 Z" // In the year -12345.
58 final RegExp re = const RegExp(
59 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part.
60 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$');
61 Match match = re.firstMatch(formattedString);
62 if (match !== null) {
63 int parseIntOrZero(String matched) {
64 // TODO(floitsch): we should not need to test against the empty string.
65 if (matched === null || matched == "") return 0;
66 return Math.parseInt(matched);
67 }
68
69 double parseDoubleOrZero(String matched) {
70 // TODO(floitsch): we should not need to test against the empty string.
71 if (matched === null || matched == "") return 0.0;
72 return Math.parseDouble(matched);
73 }
74
75 int years = Math.parseInt(match[1]);
76 int month = Math.parseInt(match[2]);
77 int day = Math.parseInt(match[3]);
78 int hours = parseIntOrZero(match[4]);
79 int minutes = parseIntOrZero(match[5]);
80 int seconds = parseIntOrZero(match[6]);
81 bool addOneMillisecond = false;
82 int milliseconds = (parseDoubleOrZero(match[7]) * 1000).round().toInt();
83 if (milliseconds == 1000) {
84 addOneMillisecond = true;
85 milliseconds = 999;
86 }
87 // TODO(floitsch): we should not need to test against the empty string.
88 bool isUtc = (match[8] !== null) && (match[8] != "");
89 int epochValue = _valueFromDecomposed(
90 years, month, day, hours, minutes, seconds, milliseconds, isUtc);
91 if (epochValue === null) {
92 throw new IllegalArgumentException(formattedString);
93 }
94 if (addOneMillisecond) epochValue++;
95 return new DateImplementation.fromEpoch(epochValue, isUtc);
96 } else {
97 throw new IllegalArgumentException(formattedString);
98 }
99 }
100
101 DateImplementation.fromEpoch(this.value, [bool isUtc = false])
102 : this.timeZone = isUtc ? const TimeZone.utc() : new TimeZone.local();
103
104 bool operator ==(other) {
105 if (other is !DateImplementation) return false;
106 return (value == other.value);
107 }
108
109 bool operator <(Date other) => value < other.value;
110
111 bool operator <=(Date other) => value <= other.value;
112
113 bool operator >(Date other) => value > other.value;
114
115 bool operator >=(Date other) => value >= other.value;
116
117 int compareTo(Date other) {
118 return value.compareTo(other.value);
119 }
120
121 int hashCode() {
122 return value;
123 }
124
125 Date toLocal() {
126 if (isUtc()) return changeTimeZone(new TimeZone.local());
127 return this;
128 }
129
130 Date toUtc() {
131 if (isUtc()) return this;
132 return changeTimeZone(const TimeZone.utc());
133 }
134
135 Date changeTimeZone(TimeZone targetTimeZone) {
136 if (targetTimeZone == null) {
137 targetTimeZone = new TimeZone.local();
138 }
139 return new Date.fromEpoch(value, targetTimeZone.isUtc);
140 }
141
142 String get timeZoneName() { throw "Unimplemented"; }
143 Duration get timeZoneOffset() { throw "Unimplemented"; }
144
145 int get year() native
146 '''return this.isUtc() ? this._asJs().getUTCFullYear() :
147 this._asJs().getFullYear();''' {
148 isUtc();
149 _asJs();
150 }
151
152 int get month() native
153 '''return this.isUtc() ? this._asJs().getUTCMonth() + 1 :
154 this._asJs().getMonth() + 1;''' {
155 isUtc();
156 _asJs();
157 }
158
159 int get day() native
160 '''return this.isUtc() ? this._asJs().getUTCDate() :
161 this._asJs().getDate();''' {
162 isUtc();
163 _asJs();
164 }
165
166 int get hours() native
167 '''return this.isUtc() ? this._asJs().getUTCHours() :
168 this._asJs().getHours();''' {
169 isUtc();
170 _asJs();
171 }
172
173 int get minutes() native
174 '''return this.isUtc() ? this._asJs().getUTCMinutes() :
175 this._asJs().getMinutes();''' {
176 isUtc();
177 _asJs();
178 }
179
180 int get seconds() native
181 '''return this.isUtc() ? this._asJs().getUTCSeconds() :
182 this._asJs().getSeconds();''' {
183 isUtc();
184 _asJs();
185 }
186
187 int get milliseconds() native
188 '''return this.isUtc() ? this._asJs().getUTCMilliseconds() :
189 this._asJs().getMilliseconds();''' {
190 isUtc();
191 _asJs();
192 }
193
194 // Adjust by one because JS weeks start on Sunday.
195 int get weekday() native '''
196 var day = this.isUtc() ? this._asJs().getUTCDay() : this._asJs().getDay();
197 return (day + 6) % 7;''';
198
199 bool isUtc() {
200 return timeZone.isUtc;
201 }
202
203 String toString() {
204 String fourDigits(int n) {
205 int absN = n.abs();
206 String sign = n < 0 ? "-" : "";
207 if (absN >= 1000) return "$n";
208 if (absN >= 100) return "${sign}0$absN";
209 if (absN >= 10) return "${sign}00$absN";
210 if (absN >= 1) return "${sign}000$absN";
211 }
212 String threeDigits(int n) {
213 if (n >= 100) return "${n}";
214 if (n >= 10) return "0${n}";
215 return "00${n}";
216 }
217 String twoDigits(int n) {
218 if (n >= 10) return "${n}";
219 return "0${n}";
220 }
221
222 String y = fourDigits(year);
223 String m = twoDigits(month);
224 String d = twoDigits(day);
225 String h = twoDigits(hours);
226 String min = twoDigits(minutes);
227 String sec = twoDigits(seconds);
228 String ms = threeDigits(milliseconds);
229 if (timeZone.isUtc) {
230 return "$y-$m-$d $h:$min:$sec.${ms}Z";
231 } else {
232 return "$y-$m-$d $h:$min:$sec.$ms";
233 }
234 }
235
236 // TODO(jimhug): Why not use operators here?
237 // Adds the [duration] to this Date instance.
238 Date add(Duration duration) {
239 return new DateImplementation.fromEpoch(value + duration.inMilliseconds,
240 timeZone.isUtc);
241 }
242
243 // Subtracts the [duration] from this Date instance.
244 Date subtract(Duration duration) {
245 return new DateImplementation.fromEpoch(value - duration.inMilliseconds,
246 timeZone.isUtc);
247 }
248
249 // Returns a [Duration] with the difference of [this] and [other].
250 Duration difference(Date other) {
251 return new Duration(milliseconds: value - other.value);
252 }
253
254 // TODO(floitsch): Use real exception object.
255 static int _valueFromDecomposed(int years, int month, int day,
256 int hours, int minutes, int seconds,
257 int milliseconds, bool isUtc) native
258 '''var jsMonth = month - 1;
259 var date = new Date(0);
260 if (isUtc) {
261 date.setUTCFullYear(years, jsMonth, day);
262 date.setUTCHours(hours, minutes, seconds, milliseconds);
263 } else {
264 date.setFullYear(years, jsMonth, day);
265 date.setHours(hours, minutes, seconds, milliseconds);
266 }
267 var value = date.valueOf();
268 if (isNaN(value)) return (void 0);
269 return value;''';
270
271 static int _valueFromString(String str) native
272 '''var value = Date.parse(str);
273 if (isNaN(value)) throw Error("Invalid Date");
274 return value;''';
275
276 static int _now() native "return new Date().valueOf();";
277
278 // Lazily keep a JS Date stored in the dart object.
279 _asJs() native '''
280 if (!this.date) {
281 this.date = new Date(this.value);
282 }
283 return this.date;''';
284 }
285
286 class TimeZone {
287 const TimeZone.utc() : this.isUtc = true;
288 const TimeZone.local() : this.isUtc = false;
289
290 bool operator ==(other) {
291 if (other is !TimeZone) return false;
292 return isUtc == other.isUtc;
293 }
294
295 String toString() {
296 if (isUtc) return "TimeZone (UTC)";
297 return "TimeZone (Local)";
298 }
299
300 final bool isUtc;
301 }
OLDNEW
« no previous file with comments | « frog/lib/corelib_impl.dart ('k') | frog/lib/frog_coreimpl_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698