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

Unified Diff: runtime/lib/date_patch.dart

Issue 1493033003: Add microsecond support to DateTime. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comment. Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/lib/date.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/date_patch.dart
diff --git a/runtime/lib/date_patch.dart b/runtime/lib/date_patch.dart
index 6c5c0c242ddc94184576bb3a6ad8f433922220c0..4697901739fb7b71363e9c8f1cd84358c5ec6d07 100644
--- a/runtime/lib/date_patch.dart
+++ b/runtime/lib/date_patch.dart
@@ -7,28 +7,39 @@
patch class DateTime {
// Natives.
// The natives have been moved up here to work around Issue 10401.
- static int _getCurrentMs() native "DateNatives_currentTimeMillis";
+ static int _getCurrentMicros() native "DateTime_currentTimeMicros";
static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch)
- native "DateNatives_timeZoneName";
+ native "DateTime_timeZoneName";
static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch)
- native "DateNatives_timeZoneOffsetInSeconds";
+ native "DateTime_timeZoneOffsetInSeconds";
static int _localTimeZoneAdjustmentInSeconds()
- native "DateNatives_localTimeZoneAdjustmentInSeconds";
-
- static const _MILLISECOND_INDEX = 0;
- static const _SECOND_INDEX = 1;
- static const _MINUTE_INDEX = 2;
- static const _HOUR_INDEX = 3;
- static const _DAY_INDEX = 4;
- static const _WEEKDAY_INDEX = 5;
- static const _MONTH_INDEX = 6;
- static const _YEAR_INDEX = 7;
+ native "DateTime_localTimeZoneAdjustmentInSeconds";
+
+ static const _MICROSECOND_INDEX = 0;
+ static const _MILLISECOND_INDEX = 1;
+ static const _SECOND_INDEX = 2;
+ static const _MINUTE_INDEX = 3;
+ static const _HOUR_INDEX = 4;
+ static const _DAY_INDEX = 5;
+ static const _WEEKDAY_INDEX = 6;
+ static const _MONTH_INDEX = 7;
+ static const _YEAR_INDEX = 8;
List __parts;
+ /* patch */ DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch,
+ {bool isUtc: false})
+ : this._withValue(
+ millisecondsSinceEpoch * Duration.MICROSECONDS_PER_MILLISECOND,
+ isUtc: isUtc);
+
+ /* patch */ DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch,
+ {bool isUtc: false})
+ : this._withValue(microsecondsSinceEpoch, isUtc: isUtc);
+
/* patch */ DateTime._internal(int year,
int month,
int day,
@@ -36,27 +47,29 @@ patch class DateTime {
int minute,
int second,
int millisecond,
+ int microsecond,
bool isUtc)
: this.isUtc = isUtc,
- this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(
- year, month, day, hour, minute, second, millisecond, isUtc) {
- if (millisecondsSinceEpoch == null) throw new ArgumentError();
+ this._value = _brokenDownDateToValue(
+ year, month, day, hour, minute, second, millisecond, microsecond,
+ isUtc) {
+ if (_value == null) throw new ArgumentError();
if (isUtc == null) throw new ArgumentError();
}
/* patch */ DateTime._now()
: isUtc = false,
- millisecondsSinceEpoch = _getCurrentMs() {
+ _value = _getCurrentMicros() {
}
/* patch */ String get timeZoneName {
if (isUtc) return "UTC";
- return _timeZoneName(millisecondsSinceEpoch);
+ return _timeZoneName(microsecondsSinceEpoch);
}
/* patch */ Duration get timeZoneOffset {
if (isUtc) return new Duration();
- int offsetInSeconds = _timeZoneOffsetInSeconds(millisecondsSinceEpoch);
+ int offsetInSeconds = _timeZoneOffsetInSeconds(microsecondsSinceEpoch);
return new Duration(seconds: offsetInSeconds);
}
@@ -66,7 +79,7 @@ patch class DateTime {
const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]];
- static List _computeUpperPart(int localMs) {
+ static List _computeUpperPart(int localMicros) {
const int DAYS_IN_4_YEARS = 4 * 365 + 1;
const int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1;
const int DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1;
@@ -80,8 +93,8 @@ patch class DateTime {
int resultDay = 0;
// Always round down.
- final int daysSince1970 = _flooredDivision(localMs,
- Duration.MILLISECONDS_PER_DAY);
+ final int daysSince1970 = _flooredDivision(localMicros,
+ Duration.MICROSECONDS_PER_DAY);
int days = daysSince1970;
days += DAYS_OFFSET;
resultYear = 400 * (days ~/ DAYS_IN_400_YEARS) - YEARS_OFFSET;
@@ -110,16 +123,20 @@ patch class DateTime {
}
resultDay = days - daysUntilMonth[resultMonth - 1] + 1;
- int resultMillisecond = localMs % Duration.MILLISECONDS_PER_SECOND;
+ int resultMicrosecond = localMicros % Duration.MICROSECONDS_PER_MILLISECOND;
+ int resultMillisecond =
+ _flooredDivision(localMicros, Duration.MICROSECONDS_PER_MILLISECOND) %
+ Duration.MILLISECONDS_PER_SECOND;
int resultSecond =
- _flooredDivision(localMs, Duration.MILLISECONDS_PER_SECOND) %
+ _flooredDivision(localMicros, Duration.MICROSECONDS_PER_SECOND) %
Duration.SECONDS_PER_MINUTE;
- int resultMinute = _flooredDivision(localMs,
- Duration.MILLISECONDS_PER_MINUTE);
+ int resultMinute = _flooredDivision(localMicros,
+ Duration.MICROSECONDS_PER_MINUTE);
resultMinute %= Duration.MINUTES_PER_HOUR;
- int resultHour = _flooredDivision(localMs, Duration.MILLISECONDS_PER_HOUR);
+ int resultHour =
+ _flooredDivision(localMicros, Duration.MICROSECONDS_PER_HOUR);
resultHour %= Duration.HOURS_PER_DAY;
// In accordance with ISO 8601 a week
@@ -129,6 +146,7 @@ patch class DateTime {
DateTime.DAYS_PER_WEEK) + DateTime.MONDAY;
List list = new List(_YEAR_INDEX + 1);
+ list[_MICROSECOND_INDEX] = resultMicrosecond;
list[_MILLISECOND_INDEX] = resultMillisecond;
list[_SECOND_INDEX] = resultSecond;
list[_MINUTE_INDEX] = resultMinute;
@@ -142,11 +160,32 @@ patch class DateTime {
get _parts {
if (__parts == null) {
- __parts = _computeUpperPart(_localDateInUtcMs);
+ __parts = _computeUpperPart(_localDateInUtcMicros);
}
return __parts;
}
+ /* patch */ DateTime add(Duration duration) {
+ return new DateTime._withValue(
+ _value + duration.inMicroseconds, isUtc: isUtc);
+ }
+
+ /* patch */ DateTime subtract(Duration duration) {
+ return new DateTime._withValue(
+ _value - duration.inMicroseconds, isUtc: isUtc);
+ }
+
+ /* patch */ Duration difference(DateTime other) {
+ return new Duration(microseconds: _value - other._value);
+ }
+
+ /* patch */ int get millisecondsSinceEpoch =>
+ _value ~/ Duration.MICROSECONDS_PER_MILLISECOND;
+
+ /* patch */ int get microsecondsSinceEpoch => _value;
+
+ /* patch */ int get microsecond => _parts[_MICROSECOND_INDEX];
+
/* patch */ int get millisecond => _parts[_MILLISECOND_INDEX];
/* patch */ int get second => _parts[_SECOND_INDEX];
@@ -164,26 +203,26 @@ patch class DateTime {
/* patch */ int get year => _parts[_YEAR_INDEX];
/**
- * Returns the amount of milliseconds in UTC that represent the same values
+ * Returns the amount of microseconds in UTC that represent the same values
* as [this].
*
- * Say [:t:] is the result of this function, then
- * * [:this.year == new DateTime.fromMillisecondsSinceEpoch(t, true).year:],
- * * [:this.month == new DateTime.fromMillisecondsSinceEpoch(t, true).month:],
- * * [:this.day == new DateTime.fromMillisecondsSinceEpoch(t, true).day:],
- * * [:this.hour == new DateTime.fromMillisecondsSinceEpoch(t, true).hour:],
+ * Say `t` is the result of this function, then
+ * * `this.year == new DateTime.fromMicrosecondsSinceEpoch(t, true).year`,
+ * * `this.month == new DateTime.fromMicrosecondsSinceEpoch(t, true).month`,
+ * * `this.day == new DateTime.fromMicrosecondsSinceEpoch(t, true).day`,
+ * * `this.hour == new DateTime.fromMicrosecondsSinceEpoch(t, true).hour`,
* * ...
*
* Daylight savings is computed as if the date was computed in [1970..2037].
* If [this] lies outside this range then it is a year with similar
* properties (leap year, weekdays) is used instead.
*/
- int get _localDateInUtcMs {
- int ms = millisecondsSinceEpoch;
- if (isUtc) return ms;
+ int get _localDateInUtcMicros {
+ int micros = _value;
+ if (isUtc) return micros;
int offset =
- _timeZoneOffsetInSeconds(ms) * Duration.MILLISECONDS_PER_SECOND;
- return ms + offset;
+ _timeZoneOffsetInSeconds(micros) * Duration.MICROSECONDS_PER_SECOND;
+ return micros + offset;
}
static int _flooredDivision(int a, int b) {
@@ -204,9 +243,10 @@ patch class DateTime {
return (y % 4 == 0) && ((y % 16 == 0) || (y % 100 != 0));
}
- /* patch */ static int _brokenDownDateToMillisecondsSinceEpoch(
+ /// Converts the given broken down date to microseconds.
+ /* patch */ static int _brokenDownDateToValue(
int year, int month, int day,
- int hour, int minute, int second, int millisecond,
+ int hour, int minute, int second, int millisecond, int microsecond,
bool isUtc) {
// Simplify calculations by working with zero-based month.
--month;
@@ -225,20 +265,21 @@ patch class DateTime {
int days = day - 1;
days += _DAYS_UNTIL_MONTH[_isLeapYear(year) ? 1 : 0][month];
days += _dayFromYear(year);
- int millisecondsSinceEpoch =
- days * Duration.MILLISECONDS_PER_DAY +
- hour * Duration.MILLISECONDS_PER_HOUR +
- minute * Duration.MILLISECONDS_PER_MINUTE +
- second * Duration.MILLISECONDS_PER_SECOND +
- millisecond;
+ int microsecondsSinceEpoch =
+ days * Duration.MICROSECONDS_PER_DAY +
+ hour * Duration.MICROSECONDS_PER_HOUR +
+ minute * Duration.MICROSECONDS_PER_MINUTE +
+ second * Duration.MICROSECONDS_PER_SECOND +
+ millisecond * Duration.MICROSECONDS_PER_MILLISECOND +
+ microsecond;
// Since [_timeZoneOffsetInSeconds] will crash if the input is far out of
// the valid range we do a preliminary test that weeds out values that can
// not become valid even with timezone adjustments.
// The timezone adjustment is always less than a day, so adding a security
// margin of one day should be enough.
- if (millisecondsSinceEpoch.abs() >
- (_MAX_MILLISECONDS_SINCE_EPOCH + Duration.MILLISECONDS_PER_DAY)) {
+ if (microsecondsSinceEpoch.abs() >
+ _MAX_MILLISECONDS_SINCE_EPOCH * 1000 + Duration.MICROSECONDS_PER_DAY) {
return null;
}
@@ -246,15 +287,17 @@ patch class DateTime {
// Note that we need to remove the local timezone adjustement before
// asking for the correct zone offset.
int adjustment = _localTimeZoneAdjustmentInSeconds() *
- Duration.MILLISECONDS_PER_SECOND;
+ Duration.MICROSECONDS_PER_SECOND;
+
int zoneOffset =
- _timeZoneOffsetInSeconds(millisecondsSinceEpoch - adjustment);
- millisecondsSinceEpoch -= zoneOffset * Duration.MILLISECONDS_PER_SECOND;
+ _timeZoneOffsetInSeconds(microsecondsSinceEpoch - adjustment);
+ microsecondsSinceEpoch -= zoneOffset * Duration.MICROSECONDS_PER_SECOND;
}
- if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
+ if (microsecondsSinceEpoch.abs() >
+ _MAX_MILLISECONDS_SINCE_EPOCH * Duration.MICROSECONDS_PER_MILLISECOND) {
return null;
}
- return millisecondsSinceEpoch;
+ return microsecondsSinceEpoch;
}
static int _weekDay(y) {
@@ -304,25 +347,25 @@ patch class DateTime {
// According to V8 this fast case works for dates from 1970 to 2099.
return 1970 + (4 * days + 2) ~/ DAYS_IN_4_YEARS;
}
- int ms = secondsSinceEpoch * Duration.MILLISECONDS_PER_SECOND;
- return _computeUpperPart(ms)[_YEAR_INDEX];
+ int micros = secondsSinceEpoch * Duration.MICROSECONDS_PER_SECOND;
+ return _computeUpperPart(micros)[_YEAR_INDEX];
}
/**
* Returns a date in seconds that is equivalent to the current date. An
- * equivalent date has the same fields ([:month:], [:day:], etc.) as the
- * [this], but the [:year:] is in the range [1970..2037].
+ * equivalent date has the same fields (`month`, `day`, etc.) as the
+ * [this], but the `year` is in the range [1970..2037].
*
* * The time since the beginning of the year is the same.
* * If [this] is in a leap year then the returned seconds are in a leap
* year, too.
* * The week day of [this] is the same as the one for the returned date.
*/
- static int _equivalentSeconds(int millisecondsSinceEpoch) {
+ static int _equivalentSeconds(int microsecondsSinceEpoch) {
const int CUT_OFF_SECONDS = 2100000000;
- int secondsSinceEpoch = _flooredDivision(millisecondsSinceEpoch,
- Duration.MILLISECONDS_PER_SECOND);
+ int secondsSinceEpoch = _flooredDivision(microsecondsSinceEpoch,
+ Duration.MICROSECONDS_PER_SECOND);
if (secondsSinceEpoch < 0 || secondsSinceEpoch >= CUT_OFF_SECONDS) {
int year = _yearsFromSecondsSinceEpoch(secondsSinceEpoch);
@@ -335,13 +378,13 @@ patch class DateTime {
return secondsSinceEpoch;
}
- static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) {
- int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch);
+ static int _timeZoneOffsetInSeconds(int microsecondsSinceEpoch) {
+ int equivalentSeconds = _equivalentSeconds(microsecondsSinceEpoch);
return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds);
}
- static String _timeZoneName(int millisecondsSinceEpoch) {
- int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch);
+ static String _timeZoneName(int microsecondsSinceEpoch) {
+ int equivalentSeconds = _equivalentSeconds(microsecondsSinceEpoch);
return _timeZoneNameForClampedSeconds(equivalentSeconds);
}
}
« no previous file with comments | « runtime/lib/date.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698