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

Side by Side Diff: frog/leg/lib/mockimpl.dart

Issue 9568011: Unify Date.fromString and support more Iso 8601. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase. Created 8 years, 9 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/js_helper.dart ('k') | frog/leg/ssa/codegen.dart » ('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) 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 // Mocks of classes and interfaces that Leg cannot read directly. 5 // Mocks of classes and interfaces that Leg cannot read directly.
6 6
7 // TODO(ahe): Remove this file. 7 // TODO(ahe): Remove this file.
8 8
9 class JSSyntaxRegExp implements RegExp { 9 class JSSyntaxRegExp implements RegExp {
10 final String pattern; 10 final String pattern;
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 _asJs(); 229 _asJs();
230 } 230 }
231 231
232 DateImplementation.now() 232 DateImplementation.now()
233 : timeZone = new TimeZone.local(), 233 : timeZone = new TimeZone.local(),
234 value = Primitives.dateNow() { 234 value = Primitives.dateNow() {
235 _asJs(); 235 _asJs();
236 } 236 }
237 237
238 factory DateImplementation.fromString(String formattedString) { 238 factory DateImplementation.fromString(String formattedString) {
239 // JavaScript's parse function is not specified and there are differences 239 // Read in (a subset of) ISO 8601.
240 // between the different implementations. Make sure we can at least read in 240 // Examples:
241 // Dart's output: try to read in (a subset of) ISO 8601 first. If that fails 241 // - "2012-02-27 13:27:00"
242 // fall back to JavaScript's implementation. 242 // - "2012-02-27 13:27:00.423z"
243 final RegExp re = 243 // - "20120227 13:27:00"
244 const RegExp(@'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d) (\d\d):(\d\d):(\d\d)( ?:.(\d{1,3}))? ?([zZ]?)$'); 244 // - "20120227T132700"
245 // - "20120227"
246 // - "2012-02-27T14Z"
247 // - "-123450101 00:00:00 Z" // In the year -12345.
248 final RegExp re = const RegExp(
249 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' + // The day part.
250 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:.(\d{1,5}))?)?)? ?([zZ])?)?$');
245 Match match = re.firstMatch(formattedString); 251 Match match = re.firstMatch(formattedString);
246 if (match !== null) { 252 if (match !== null) {
253 int parseIntOrZero(String matched) {
254 // TODO(floitsch): we should not need to test against the empty string.
255 if (matched === null || matched == "") return 0;
256 return Math.parseInt(matched);
257 }
258
247 int years = Math.parseInt(match[1]); 259 int years = Math.parseInt(match[1]);
248 int month = Math.parseInt(match[2]); 260 int month = Math.parseInt(match[2]);
249 int day = Math.parseInt(match[3]); 261 int day = Math.parseInt(match[3]);
250 int hours = Math.parseInt(match[4]); 262 int hours = parseIntOrZero(match[4]);
251 int minutes = Math.parseInt(match[5]); 263 int minutes = parseIntOrZero(match[5]);
252 int seconds = Math.parseInt(match[6]); 264 int seconds = parseIntOrZero(match[6]);
253 int milliseconds = 0; 265 bool addOneMillisecond = false;
254 if (match[7] !== null) { 266 int milliseconds = parseIntOrZero(match[7]);
255 milliseconds = Math.parseInt(match[7]); 267 if (milliseconds != 0) {
256 if (match[7].length == 1) { 268 if (match[7].length == 1) {
257 milliseconds *= 100; 269 milliseconds *= 100;
258 } else if (match[7].length == 2) { 270 } else if (match[7].length == 2) {
259 milliseconds *= 10; 271 milliseconds *= 10;
272 } else if (match[7].length == 3) {
273 // Do nothing.
274 } else if (match[7].length == 4) {
275 addOneMillisecond = ((milliseconds % 10) >= 5);
276 milliseconds ~/= 10;
260 } else { 277 } else {
261 assert(match[7].length == 3); 278 assert(match[7].length == 5);
279 addOneMillisecond = ((milliseconds %100) >= 50);
280 milliseconds ~/= 100;
281 }
282 if (addOneMillisecond && milliseconds < 999) {
283 addOneMillisecond = false;
284 milliseconds++;
262 } 285 }
263 } 286 }
287 // TODO(floitsch): we should not need to test against the empty string.
264 bool isUtc = (match[8] !== null) && (match[8] != ""); 288 bool isUtc = (match[8] !== null) && (match[8] != "");
265 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); 289 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local();
266 return new DateImplementation.withTimeZone( 290 int epochValue = Primitives.valueFromDecomposedDate(
267 years, month, day, hours, minutes, seconds, milliseconds, timezone); 291 years, month, day, hours, minutes, seconds, milliseconds, isUtc);
292 if (epochValue === null) {
293 throw new IllegalArgumentException(formattedString);
294 }
295 if (addOneMillisecond) epochValue++;
296 return new DateImplementation.fromEpoch(epochValue, timezone);
268 } else { 297 } else {
269 return new DateImplementation.fromEpoch(formattedString, 298 throw new IllegalArgumentException(formattedString);
270 new TimeZone.local());
271 } 299 }
272 } 300 }
273 301
274 const DateImplementation.fromEpoch(this.value, this.timeZone); 302 const DateImplementation.fromEpoch(this.value, this.timeZone);
275 303
276 bool operator ==(other) { 304 bool operator ==(other) {
277 if (!(other is DateImplementation)) return false; 305 if (!(other is DateImplementation)) return false;
278 return (value == other.value) && (timeZone == other.timeZone); 306 return (value == other.value) && (timeZone == other.timeZone);
279 } 307 }
280 308
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 340
313 bool isLocalTime() { 341 bool isLocalTime() {
314 return !timeZone.isUtc; 342 return !timeZone.isUtc;
315 } 343 }
316 344
317 bool isUtc() { 345 bool isUtc() {
318 return timeZone.isUtc; 346 return timeZone.isUtc;
319 } 347 }
320 348
321 String toString() { 349 String toString() {
350 String y = fourDigits(year);
322 String m = twoDigits(month); 351 String m = twoDigits(month);
323 String d = twoDigits(day); 352 String d = twoDigits(day);
324 String h = twoDigits(hours); 353 String h = twoDigits(hours);
325 String min = twoDigits(minutes); 354 String min = twoDigits(minutes);
326 String sec = twoDigits(seconds); 355 String sec = twoDigits(seconds);
327 String ms = threeDigits(milliseconds); 356 String ms = threeDigits(milliseconds);
328 if (timeZone.isUtc) { 357 if (timeZone.isUtc) {
329 return "$year-$m-$d $h:$min:$sec.${ms}Z"; 358 return "$y-$m-$d $h:$min:$sec.${ms}Z";
330 } else { 359 } else {
331 return "$year-$m-$d $h:$min:$sec.$ms"; 360 return "$y-$m-$d $h:$min:$sec.$ms";
332 } 361 }
333 } 362 }
334 363
335 // Adds the [duration] to this Date instance. 364 // Adds the [duration] to this Date instance.
336 Date add(Duration duration) { 365 Date add(Duration duration) {
337 checkNull(duration); 366 checkNull(duration);
338 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, 367 return new DateImplementation.fromEpoch(value + duration.inMilliseconds,
339 timeZone); 368 timeZone);
340 } 369 }
341 370
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 } 449 }
421 String twoDigitMinutes = 450 String twoDigitMinutes =
422 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR)); 451 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR));
423 String twoDigitSeconds = 452 String twoDigitSeconds =
424 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE)); 453 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE));
425 String threeDigitMs = 454 String threeDigitMs =
426 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND)); 455 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND));
427 return "${inHours}:${twoDigitMinutes}:${twoDigitSeconds}.${threeDigitMs}"; 456 return "${inHours}:${twoDigitMinutes}:${twoDigitSeconds}.${threeDigitMs}";
428 } 457 }
429 } 458 }
OLDNEW
« no previous file with comments | « frog/leg/lib/js_helper.dart ('k') | frog/leg/ssa/codegen.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698