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

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

Issue 830143003: Optimize DateTime constructor slightly. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 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 | « no previous file | no next file » | 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 // Dart core library. 4 // Dart core library.
5 5
6 // VM implementation of DateTime. 6 // VM implementation of DateTime.
7 patch class DateTime { 7 patch class DateTime {
8 // Natives. 8 // Natives.
9 // The natives have been moved up here to work around Issue 10401. 9 // The natives have been moved up here to work around Issue 10401.
10 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; 10 static int _getCurrentMs() native "DateNatives_currentTimeMillis";
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 return list; 140 return list;
141 } 141 }
142 142
143 get _parts { 143 get _parts {
144 if (__parts == null) { 144 if (__parts == null) {
145 __parts = _computeUpperPart(_localDateInUtcMs); 145 __parts = _computeUpperPart(_localDateInUtcMs);
146 } 146 }
147 return __parts; 147 return __parts;
148 } 148 }
149 149
150 /* patch */ int get millisecond => _parts[_MILLISECOND_INDEX]; 150 /* patch */ int get millisecond => __[_MILLISECOND_INDEX];
Søren Gjesse 2015/01/07 10:48:18 Where does __ come from?
Lasse Reichstein Nielsen 2015/01/07 12:01:19 Ack, bad edit.
Søren Gjesse 2015/01/07 12:03:29 Thought so :-)
151 151
152 /* patch */ int get second => _parts[_SECOND_INDEX]; 152 /* patch */ int get second => _parts[_SECOND_INDEX];
153 153
154 /* patch */ int get minute => _parts[_MINUTE_INDEX]; 154 /* patch */ int get minute => _parts[_MINUTE_INDEX];
155 155
156 /* patch */ int get hour => _parts[_HOUR_INDEX]; 156 /* patch */ int get hour => _parts[_HOUR_INDEX];
157 157
158 /* patch */ int get day => _parts[_DAY_INDEX]; 158 /* patch */ int get day => _parts[_DAY_INDEX];
159 159
160 /* patch */ int get weekday => _parts[_WEEKDAY_INDEX]; 160 /* patch */ int get weekday => _parts[_WEEKDAY_INDEX];
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 // Returns the days since 1970 for the start of the given [year]. 193 // Returns the days since 1970 for the start of the given [year].
194 // [year] may be before epoch. 194 // [year] may be before epoch.
195 static int _dayFromYear(int year) { 195 static int _dayFromYear(int year) {
196 return 365 * (year - 1970) 196 return 365 * (year - 1970)
197 + _flooredDivision(year - 1969, 4) 197 + _flooredDivision(year - 1969, 4)
198 - _flooredDivision(year - 1901, 100) 198 - _flooredDivision(year - 1901, 100)
199 + _flooredDivision(year - 1601, 400); 199 + _flooredDivision(year - 1601, 400);
200 } 200 }
201 201
202 static bool _isLeapYear(y) { 202 static bool _isLeapYear(y) {
203 return (y.remainder(4) == 0) && 203 // (y % 16 == 0) matches multiples of 400, and is faster than % 400.
204 ((y.remainder(100) != 0) || (y.remainder(400) == 0)); 204 return (y % 4 == 0) && ((y % 16 == 0) || (y % 100 != 0));
205 } 205 }
206 206
207 /* patch */ static int _brokenDownDateToMillisecondsSinceEpoch( 207 /* patch */ static int _brokenDownDateToMillisecondsSinceEpoch(
208 int year, int month, int day, 208 int year, int month, int day,
209 int hour, int minute, int second, int millisecond, 209 int hour, int minute, int second, int millisecond,
210 bool isUtc) { 210 bool isUtc) {
211 // Simplify calculations by working with zero-based month. 211 // Simplify calculations by working with zero-based month.
212 --month; 212 --month;
213 // Deal with under and overflow. 213 // Deal with under and overflow.
214 year += (month / 12).floor(); 214 if (month >= 12) {
215 month = month % 12; 215 year += month ~/ 12;
216 month = month % 12;
217 } else if (month < 0) {
218 int realMonth = month % 12;
219 year += (month - realMonth) ~/ 12;
220 month = realMonth;
221 }
216 222
217 // First compute the seconds in UTC, independent of the [isUtc] flag. If 223 // First compute the seconds in UTC, independent of the [isUtc] flag. If
218 // necessary we will add the time-zone offset later on. 224 // necessary we will add the time-zone offset later on.
219 int days = day - 1; 225 int days = day - 1;
220 days += _DAYS_UNTIL_MONTH[_isLeapYear(year) ? 1 : 0][month]; 226 days += _DAYS_UNTIL_MONTH[_isLeapYear(year) ? 1 : 0][month];
221 days += _dayFromYear(year); 227 days += _dayFromYear(year);
222 int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY + 228 int millisecondsSinceEpoch =
223 hour * Duration.MILLISECONDS_PER_HOUR + 229 days * Duration.MILLISECONDS_PER_DAY +
224 minute * Duration.MILLISECONDS_PER_MINUTE+ 230 hour * Duration.MILLISECONDS_PER_HOUR +
231 minute * Duration.MILLISECONDS_PER_MINUTE +
225 second * Duration.MILLISECONDS_PER_SECOND + 232 second * Duration.MILLISECONDS_PER_SECOND +
226 millisecond; 233 millisecond;
227 234
228 // Since [_timeZoneOffsetInSeconds] will crash if the input is far out of 235 // Since [_timeZoneOffsetInSeconds] will crash if the input is far out of
229 // the valid range we do a preliminary test that weeds out values that can 236 // the valid range we do a preliminary test that weeds out values that can
230 // not become valid even with timezone adjustments. 237 // not become valid even with timezone adjustments.
231 // The timezone adjustment is always less than a day, so adding a security 238 // The timezone adjustment is always less than a day, so adding a security
232 // margin of one day should be enough. 239 // margin of one day should be enough.
233 if (millisecondsSinceEpoch.abs() > 240 if (millisecondsSinceEpoch.abs() >
234 (_MAX_MILLISECONDS_SINCE_EPOCH + Duration.MILLISECONDS_PER_DAY)) { 241 (_MAX_MILLISECONDS_SINCE_EPOCH + Duration.MILLISECONDS_PER_DAY)) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { 338 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) {
332 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); 339 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch);
333 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); 340 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds);
334 } 341 }
335 342
336 static String _timeZoneName(int millisecondsSinceEpoch) { 343 static String _timeZoneName(int millisecondsSinceEpoch) {
337 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); 344 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch);
338 return _timeZoneNameForClampedSeconds(equivalentSeconds); 345 return _timeZoneNameForClampedSeconds(equivalentSeconds);
339 } 346 }
340 } 347 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698