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

Side by Side Diff: samples/total/client/DateTimeUtils.dart

Issue 10534114: Reapply "Refactor Date implementation in VM." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 80chars 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 | « runtime/vm/os_win.cc ('k') | tests/co19/co19-leg.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 class DateUtils { 5 class DateUtils {
6 static final int DAYS_FROM_1900_TO_1970 = 25569; 6 static final int DAYS_FROM_1900_TO_1970 = 25569;
7 static final int MILLISECONDS_PER_DAY = 86400000; 7 static final int MILLISECONDS_PER_DAY = 86400000;
8 static final int MILLISECONDS_PER_HOUR = 3600000; 8 static final int MILLISECONDS_PER_HOUR = 3600000;
9 static Date _EPOCH; 9 static Date _EPOCH;
10 10
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 if (mmddyyyy.hasMatch(value)) { 58 if (mmddyyyy.hasMatch(value)) {
59 return true; 59 return true;
60 } 60 }
61 return false; 61 return false;
62 } 62 }
63 63
64 // Return the number of days between 1899-12-30 and the current time. 64 // Return the number of days between 1899-12-30 and the current time.
65 static double now() { 65 static double now() {
66 Date nowDate = new Date.now(); 66 Date nowDate = new Date.now();
67 int milliseconds = nowDate.difference(EPOCH).inMilliseconds; 67 int milliseconds = nowDate.difference(EPOCH).inMilliseconds;
68 milliseconds += MILLISECONDS_PER_HOUR; // FIXME - something is one hour behi nd 68 // We round the result to get rid of daylight saving differences.
69 double days = milliseconds / MILLISECONDS_PER_DAY; 69 double days = (milliseconds / MILLISECONDS_PER_DAY).round();
70 return days; 70 return days;
71 } 71 }
72 72
73 // Parse a date and return the number of days between 1899-12-30 and the given date. 73 // Parse a date and return the number of days between 1899-12-30 and the given date.
74 // The acceptable formats are as described in the comments for isDate(String). 74 // The acceptable formats are as described in the comments for isDate(String).
75 static double parseDate(String value) { 75 static double parseDate(String value) {
76 RegExp mmdd = const RegExp("^(\\d\\d?)[-/](\\d\\d?)\$"); 76 RegExp mmdd = const RegExp("^(\\d\\d?)[-/](\\d\\d?)\$");
77 Match m = mmdd.firstMatch(value); 77 Match m = mmdd.firstMatch(value);
78 if (m != null) { 78 if (m != null) {
79 int month = _parseInt(m[1]); 79 int month = _parseInt(m[1]);
80 int day = _parseInt(m[2]); 80 int day = _parseInt(m[2]);
81 Date nowDate = new Date.now(); 81 Date nowDate = new Date.now();
82 int thisYear = nowDate.year; 82 int thisYear = nowDate.year;
83 Date dateTime = new Date(thisYear, month, day, 0, 0, 0, 0); 83 Date dateTime = new Date(thisYear, month, day, 0, 0, 0, 0);
84 int milliseconds = dateTime.difference(EPOCH).inMilliseconds; 84 int milliseconds = dateTime.difference(EPOCH).inMilliseconds;
85 milliseconds += MILLISECONDS_PER_HOUR; // FIXME - something is one hour be hind 85 // We round the result to get rid of daylight saving differences.
86 double days = milliseconds / MILLISECONDS_PER_DAY; 86 double days = (milliseconds / MILLISECONDS_PER_DAY).round();
87 return days; 87 return days;
88 } 88 }
89 89
90 RegExp mmddyyyy = const RegExp("^(\\d\\d?)[-/](\\d\\d?)[-/](\\d\\d\\d?\\d?)\ $"); 90 RegExp mmddyyyy = const RegExp("^(\\d\\d?)[-/](\\d\\d?)[-/](\\d\\d\\d?\\d?)\ $");
91 m = mmddyyyy.firstMatch(value); 91 m = mmddyyyy.firstMatch(value);
92 if (m != null) { 92 if (m != null) {
93 int month = _parseInt(m[1]); 93 int month = _parseInt(m[1]);
94 int day = _parseInt(m[2]); 94 int day = _parseInt(m[2]);
95 int year = _parseInt(m[3]); 95 int year = _parseInt(m[3]);
96 // 0-50 ==> 2000-2050, 51-99 ==> 1951-1999 96 // 0-50 ==> 2000-2050, 51-99 ==> 1951-1999
97 if (year < 50) { 97 if (year < 50) {
98 year += 2000; 98 year += 2000;
99 } else if (year < 100) { 99 } else if (year < 100) {
100 year += 1900; 100 year += 1900;
101 } 101 }
102 Date dateTime = new Date(year, month, day, 0, 0, 0, 0); 102 Date dateTime = new Date(year, month, day, 0, 0, 0, 0);
103 int milliseconds = dateTime.difference(EPOCH).inMilliseconds; 103 int milliseconds = dateTime.difference(EPOCH).inMilliseconds;
104 milliseconds += MILLISECONDS_PER_HOUR; // FIXME - something is one hour be hind 104 // We round the result to get rid of daylight saving differences.
105 double days = milliseconds / MILLISECONDS_PER_DAY; 105 double days = (milliseconds / MILLISECONDS_PER_DAY).round();
106 return days; 106 return days;
107 } 107 }
108 108
109 return -1.0; 109 return -1.0;
110 } 110 }
111 111
112 // Return the number of days between 1899-12-30 and the current time, truncate d to 112 // Return the number of days between 1899-12-30 and the current time, truncate d to
113 // an integer. 113 // an integer.
114 static double today() => now().floor(); 114 static double today() => now().floor();
115 115
116 // Parse an integer, stripping leading zeros to avoid an octal parsing bug. 116 // Parse an integer, stripping leading zeros to avoid an octal parsing bug.
117 static int _parseInt(String s) { 117 static int _parseInt(String s) {
118 while (s.length > 1 && s.charCodeAt(0) == StringUtils.ZERO) { 118 while (s.length > 1 && s.charCodeAt(0) == StringUtils.ZERO) {
119 s = s.substring(1, s.length); 119 s = s.substring(1, s.length);
120 } 120 }
121 return Math.parseInt(s); 121 return Math.parseInt(s);
122 } 122 }
123 } 123 }
OLDNEW
« no previous file with comments | « runtime/vm/os_win.cc ('k') | tests/co19/co19-leg.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698