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

Side by Side Diff: runtime/lib/date_patch.dart

Issue 10834353: Reapply "Sharing of sources for corelib date implementation between VM (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix analyzer Created 8 years, 4 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/lib/date.dart ('k') | runtime/lib/lib_impl_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
1 // Copyright (c) 2011, 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 // Dart core library. 4 // Dart core library.
5 5
6 // VM implementation of DateImplementation. 6 // VM implementation of DateImplementation.
7 class DateImplementation implements Date { 7 patch class DateImplementation {
8 static final int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; 8 /* patch */ DateImplementation(int years,
9 9 [int month = 1,
10 DateImplementation(int years, 10 int day = 1,
11 [int month = 1, 11 int hour = 0,
12 int day = 1, 12 int minute = 0,
13 int hour = 0, 13 int second = 0,
14 int minute = 0, 14 int millisecond = 0,
15 int second = 0, 15 bool isUtc = false])
16 int millisecond = 0,
17 bool isUtc = false])
18 : this.isUtc = isUtc, 16 : this.isUtc = isUtc,
19 this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( 17 this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(
20 years, month, day, hour, minute, second, millisecond, isUtc) { 18 years, month, day, hour, minute, second, millisecond, isUtc) {
21 if (millisecondsSinceEpoch === null) throw new IllegalArgumentException(); 19 if (millisecondsSinceEpoch === null) throw new IllegalArgumentException();
22 if (isUtc === null) throw new IllegalArgumentException(); 20 if (isUtc === null) throw new IllegalArgumentException();
23 } 21 }
24 22
25 DateImplementation.now() 23 /* patch */ DateImplementation.now()
26 : isUtc = false, 24 : isUtc = false,
27 millisecondsSinceEpoch = _getCurrentMs() { 25 millisecondsSinceEpoch = _getCurrentMs() {
28 } 26 }
29 27
30 factory DateImplementation.fromString(String formattedString) { 28 /* patch */ String get timeZoneName() {
31 // Read in (a subset of) ISO 8601.
32 // Examples:
33 // - "2012-02-27 13:27:00"
34 // - "2012-02-27 13:27:00.423z"
35 // - "20120227 13:27:00"
36 // - "20120227T132700"
37 // - "20120227"
38 // - "2012-02-27T14Z"
39 // - "-123450101 00:00:00 Z" // In the year -12345.
40 final RegExp re = const RegExp(
41 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part.
42 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$');
43 Match match = re.firstMatch(formattedString);
44 if (match !== null) {
45 int parseIntOrZero(String matched) {
46 // TODO(floitsch): we should not need to test against the empty string.
47 if (matched === null || matched == "") return 0;
48 return Math.parseInt(matched);
49 }
50
51 double parseDoubleOrZero(String matched) {
52 // TODO(floitsch): we should not need to test against the empty string.
53 if (matched === null || matched == "") return 0.0;
54 return Math.parseDouble(matched);
55 }
56
57 int years = Math.parseInt(match[1]);
58 int month = Math.parseInt(match[2]);
59 int day = Math.parseInt(match[3]);
60 int hour = parseIntOrZero(match[4]);
61 int minute = parseIntOrZero(match[5]);
62 int second = parseIntOrZero(match[6]);
63 bool addOneMillisecond = false;
64 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt();
65 if (millisecond == 1000) {
66 addOneMillisecond = true;
67 millisecond = 999;
68 }
69 // TODO(floitsch): we should not need to test against the empty string.
70 bool isUtc = (match[8] !== null) && (match[8] != "");
71 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(
72 years, month, day, hour, minute, second, millisecond, isUtc);
73 if (millisecondsSinceEpoch === null) {
74 throw new IllegalArgumentException(formattedString);
75 }
76 if (addOneMillisecond) millisecondsSinceEpoch++;
77 return new DateImplementation.fromMillisecondsSinceEpoch(
78 millisecondsSinceEpoch, isUtc);
79 } else {
80 throw new IllegalArgumentException(formattedString);
81 }
82 }
83
84 DateImplementation.fromMillisecondsSinceEpoch(
85 int this.millisecondsSinceEpoch, [bool isUtc = false])
86 : this.isUtc = isUtc {
87 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
88 throw new IllegalArgumentException(millisecondsSinceEpoch);
89 }
90 if (isUtc === null) {
91 throw new IllegalArgumentException(isUtc);
92 }
93 }
94
95 bool operator ==(Object other) {
96 if (other is !DateImplementation) return false;
97 DateImplementation otherDate = other;
98 return millisecondsSinceEpoch == otherDate.millisecondsSinceEpoch;
99 }
100
101 bool operator <(Date other)
102 => millisecondsSinceEpoch < other.millisecondsSinceEpoch;
103
104 bool operator <=(Date other)
105 => millisecondsSinceEpoch <= other.millisecondsSinceEpoch;
106
107 bool operator >(Date other)
108 => millisecondsSinceEpoch > other.millisecondsSinceEpoch;
109
110 bool operator >=(Date other)
111 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch;
112
113 int compareTo(Date other)
114 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch);
115
116 int hashCode() => millisecondsSinceEpoch;
117
118 Date toLocal() {
119 if (isUtc) {
120 return new DateImplementation.fromMillisecondsSinceEpoch(
121 millisecondsSinceEpoch, false);
122 }
123 return this;
124 }
125
126 Date toUtc() {
127 if (isUtc) return this;
128 return new DateImplementation.fromMillisecondsSinceEpoch(
129 millisecondsSinceEpoch, true);
130 }
131
132 String get timeZoneName() {
133 if (isUtc) return "UTC"; 29 if (isUtc) return "UTC";
134 return _timeZoneName(millisecondsSinceEpoch); 30 return _timeZoneName(millisecondsSinceEpoch);
135 } 31 }
136 32
137 Duration get timeZoneOffset() { 33 /* patch */ Duration get timeZoneOffset() {
138 if (isUtc) return new Duration(0); 34 if (isUtc) return new Duration(0);
139 int offsetInSeconds = _timeZoneOffsetInSeconds(millisecondsSinceEpoch); 35 int offsetInSeconds = _timeZoneOffsetInSeconds(millisecondsSinceEpoch);
140 return new Duration(seconds: offsetInSeconds); 36 return new Duration(seconds: offsetInSeconds);
141 } 37 }
142 38
143 int get year() { 39 /* patch */ int get year() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[0] ;
144 return _decomposeIntoYearMonthDay(_localDateInUtcMs)[0];
145 }
146 40
147 int get month() { 41 /* patch */ int get month() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[1 ];
148 return _decomposeIntoYearMonthDay(_localDateInUtcMs)[1];
149 }
150 42
151 int get day() { 43 /* patch */ int get day() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[2];
152 return _decomposeIntoYearMonthDay(_localDateInUtcMs)[2];
153 }
154 44
155 int get hour() { 45 /* patch */ int get hour() {
156 int valueInHours = _flooredDivision(_localDateInUtcMs, 46 int valueInHours = _flooredDivision(_localDateInUtcMs,
157 Duration.MILLISECONDS_PER_HOUR); 47 Duration.MILLISECONDS_PER_HOUR);
158 return valueInHours % Duration.HOURS_PER_DAY; 48 return valueInHours % Duration.HOURS_PER_DAY;
159 } 49 }
160 50
161 int get minute() { 51 /* patch */ int get minute() {
162 int valueInMinutes = _flooredDivision(_localDateInUtcMs, 52 int valueInMinutes = _flooredDivision(_localDateInUtcMs,
163 Duration.MILLISECONDS_PER_MINUTE); 53 Duration.MILLISECONDS_PER_MINUTE);
164 return valueInMinutes % Duration.MINUTES_PER_HOUR; 54 return valueInMinutes % Duration.MINUTES_PER_HOUR;
165 } 55 }
166 56
167 int get second() { 57 /* patch */ int get second() {
168 // Seconds are unaffected by the timezone the user is in. So we can 58 // Seconds are unaffected by the timezone the user is in. So we can
169 // directly use the millisecondsSinceEpoch and not [_localDateInUtcMs]. 59 // directly use the millisecondsSinceEpoch and not [_localDateInUtcMs].
170 int valueInSeconds = 60 int valueInSeconds =
171 _flooredDivision(millisecondsSinceEpoch, 61 _flooredDivision(millisecondsSinceEpoch,
172 Duration.MILLISECONDS_PER_SECOND); 62 Duration.MILLISECONDS_PER_SECOND);
173 return valueInSeconds % Duration.SECONDS_PER_MINUTE; 63 return valueInSeconds % Duration.SECONDS_PER_MINUTE;
174 } 64 }
175 65
176 int get millisecond() { 66 /* patch */ int get millisecond() {
177 // Milliseconds are unaffected by the timezone the user is in. So we can 67 // Milliseconds are unaffected by the timezone the user is in. So we can
178 // directly use the value and not the [_localDateInUtcValue]. 68 // directly use the value and not the [_localDateInUtcValue].
179 return millisecondsSinceEpoch % Duration.MILLISECONDS_PER_SECOND; 69 return millisecondsSinceEpoch % Duration.MILLISECONDS_PER_SECOND;
180 } 70 }
181 71
182 /** Returns the weekday of [this]. In accordance with ISO 8601 a week 72 /** Returns the weekday of [this]. In accordance with ISO 8601 a week
183 * starts with Monday. Monday has the value 1 up to Sunday with 7. */ 73 * starts with Monday. Monday has the value 1 up to Sunday with 7. */
184 int get weekday() { 74 /* patch */ int get weekday() {
185 int daysSince1970 = 75 int daysSince1970 =
186 _flooredDivision(_localDateInUtcMs, Duration.MILLISECONDS_PER_DAY); 76 _flooredDivision(_localDateInUtcMs, Duration.MILLISECONDS_PER_DAY);
187 // 1970-1-1 was a Thursday. 77 // 1970-1-1 was a Thursday.
188 return ((daysSince1970 + Date.THU - Date.MON) % Date.DAYS_IN_WEEK) + 78 return ((daysSince1970 + Date.THU - Date.MON) % Date.DAYS_IN_WEEK) +
189 Date.MON; 79 Date.MON;
190 } 80 }
191 81
192 String toString() {
193 String fourDigits(int n) {
194 int absN = n.abs();
195 String sign = n < 0 ? "-" : "";
196 if (absN >= 1000) return "$n";
197 if (absN >= 100) return "${sign}0$absN";
198 if (absN >= 10) return "${sign}00$absN";
199 return "${sign}000$absN";
200 }
201 String threeDigits(int n) {
202 if (n >= 100) return "${n}";
203 if (n >= 10) return "0${n}";
204 return "00${n}";
205 }
206 String twoDigits(int n) {
207 if (n >= 10) return "${n}";
208 return "0${n}";
209 }
210
211 String y = fourDigits(year);
212 String m = twoDigits(month);
213 String d = twoDigits(day);
214 String h = twoDigits(hour);
215 String min = twoDigits(minute);
216 String sec = twoDigits(second);
217 String ms = threeDigits(millisecond);
218 if (isUtc) {
219 return "$y-$m-$d $h:$min:$sec.${ms}Z";
220 } else {
221 return "$y-$m-$d $h:$min:$sec.$ms";
222 }
223 }
224
225 /** Returns a new [Date] with the [duration] added to [this]. */
226 Date add(Duration duration) {
227 int ms = millisecondsSinceEpoch;
228 return new DateImplementation.fromMillisecondsSinceEpoch(
229 ms + duration.inMilliseconds, isUtc);
230 }
231
232 /** Returns a new [Date] with the [duration] subtracted from [this]. */
233 Date subtract(Duration duration) {
234 int ms = millisecondsSinceEpoch;
235 return new DateImplementation.fromMillisecondsSinceEpoch(
236 ms - duration.inMilliseconds, isUtc);
237 }
238
239 /** Returns a [Duration] with the difference of [this] and [other]. */
240 Duration difference(Date other) {
241 int ms = millisecondsSinceEpoch;
242 int otherMs = other.millisecondsSinceEpoch;
243 return new DurationImplementation(milliseconds: ms - otherMs);
244 }
245 82
246 /** The first list contains the days until each month in non-leap years. The 83 /** The first list contains the days until each month in non-leap years. The
247 * second list contains the days in leap years. */ 84 * second list contains the days in leap years. */
248 static final List<List<int>> _DAYS_UNTIL_MONTH = 85 static final List<List<int>> _DAYS_UNTIL_MONTH =
249 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], 86 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
250 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]]; 87 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]];
251 88
252 // Returns the UTC year, month and day for the corresponding 89 // Returns the UTC year, month and day for the corresponding
253 // [millisecondsSinceEpoch]. 90 // [millisecondsSinceEpoch].
254 // Code is adapted from V8. 91 // Code is adapted from V8.
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 int zoneOffset = 218 int zoneOffset =
382 _timeZoneOffsetInSeconds(millisecondsSinceEpoch - adjustment); 219 _timeZoneOffsetInSeconds(millisecondsSinceEpoch - adjustment);
383 millisecondsSinceEpoch -= zoneOffset * Duration.MILLISECONDS_PER_SECOND; 220 millisecondsSinceEpoch -= zoneOffset * Duration.MILLISECONDS_PER_SECOND;
384 } 221 }
385 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { 222 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
386 return null; 223 return null;
387 } 224 }
388 return millisecondsSinceEpoch; 225 return millisecondsSinceEpoch;
389 } 226 }
390 227
228 static int _weekDay(y) {
229 // 1/1/1970 was a Thursday.
230 return (_dayFromYear(y) + 4) % 7;
231 }
232
391 /** 233 /**
392 * Returns a year in the range 2008-2035 matching 234 * Returns a year in the range 2008-2035 matching
393 * * leap year, and 235 * * leap year, and
394 * * week day of first day. 236 * * week day of first day.
395 * 237 *
396 * Leap seconds are ignored. 238 * Leap seconds are ignored.
397 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. 239 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9.
398 */ 240 */
399 static _equivalentYear(int year) { 241 static _equivalentYear(int year) {
400 // Returns the week day (in range 0 - 6). 242 // Returns the week day (in range 0 - 6).
401 int weekDay(y) {
402 // 1/1/1970 was a Thursday.
403 return (_dayFromYear(y) + 4) % 7;
404 }
405 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. 243 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year.
406 // 1/1/1967 was a Sunday (i.e. weekday 0). 244 // 1/1/1967 was a Sunday (i.e. weekday 0).
407 // Without leap years a subsequent year has a week day + 1 (for example 245 // Without leap years a subsequent year has a week day + 1 (for example
408 // 1/1/1968 was a Monday). With leap-years it jumps over one week day 246 // 1/1/1968 was a Monday). With leap-years it jumps over one week day
409 // (e.g. 1/1/1957 was a Tuesday). 247 // (e.g. 1/1/1957 was a Tuesday).
410 // After 12 years the weekdays have advanced by 12 days + 3 leap days = 248 // After 12 years the weekdays have advanced by 12 days + 3 leap days =
411 // 15 days. 15 % 7 = 1. So after 12 years the week day has always 249 // 15 days. 15 % 7 = 1. So after 12 years the week day has always
412 // (now independently of leap-years) advanced by one. 250 // (now independently of leap-years) advanced by one.
413 // weekDay * 12 gives thus a year starting with the wanted weekDay. 251 // weekDay * 12 gives thus a year starting with the wanted weekDay.
414 int recentYear = (_isLeapYear(year) ? 1956 : 1967) + (weekDay(year) * 12); 252 int recentYear = (_isLeapYear(year) ? 1956 : 1967) + (_weekDay(year) * 12);
415 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the 253 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the
416 // leap years, 7 for the weekdays). 254 // leap years, 7 for the weekdays).
417 // Find the year in the range 2008..2037 that is equivalent mod 28. 255 // Find the year in the range 2008..2037 that is equivalent mod 28.
418 return 2008 + (recentYear - 2008) % 28; 256 return 2008 + (recentYear - 2008) % 28;
419 } 257 }
420 258
421 /** 259 /**
422 * Returns the UTC year for the corresponding [secondsSinceEpoch]. 260 * Returns the UTC year for the corresponding [secondsSinceEpoch].
423 * It is relatively fast for values in the range 0 to year 2098. 261 * It is relatively fast for values in the range 0 to year 2098.
424 * 262 *
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { 306 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) {
469 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); 307 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch);
470 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); 308 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds);
471 } 309 }
472 310
473 static String _timeZoneName(int millisecondsSinceEpoch) { 311 static String _timeZoneName(int millisecondsSinceEpoch) {
474 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); 312 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch);
475 return _timeZoneNameForClampedSeconds(equivalentSeconds); 313 return _timeZoneNameForClampedSeconds(equivalentSeconds);
476 } 314 }
477 315
478 final bool isUtc;
479 final int millisecondsSinceEpoch;
480
481 // Natives 316 // Natives
482 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; 317 static int _getCurrentMs() native "DateNatives_currentTimeMillis";
483 318
484 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) 319 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch)
485 native "DateNatives_timeZoneName"; 320 native "DateNatives_timeZoneName";
486 321
487 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) 322 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch)
488 native "DateNatives_timeZoneOffsetInSeconds"; 323 native "DateNatives_timeZoneOffsetInSeconds";
489 324
490 static int _localTimeZoneAdjustmentInSeconds() 325 static int _localTimeZoneAdjustmentInSeconds()
491 native "DateNatives_localTimeZoneAdjustmentInSeconds"; 326 native "DateNatives_localTimeZoneAdjustmentInSeconds";
492 } 327 }
OLDNEW
« no previous file with comments | « runtime/lib/date.dart ('k') | runtime/lib/lib_impl_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698