[vm/datetime] Fix construction of '1 microsecond before epoch' DateTime.

TEST=corelib/date_time_extremes_test
Fixes https://github.com/dart-lang/sdk/issues/55438

Change-Id: I87f5c01450089a76703ae6a3326fc188f2b90903
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362301
Commit-Queue: Alexander Aprelev <aam@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
This commit is contained in:
Alexander Aprelev 2024-04-11 22:01:29 +00:00 committed by Commit Queue
parent be02caa2d1
commit 8af6f94b61
2 changed files with 8 additions and 2 deletions

View file

@ -44,14 +44,16 @@ class DateTime {
{bool isUtc = false})
: this._withValue(microsecondsSinceEpoch, isUtc: isUtc);
static const _sentinelMs = -_maxMillisecondsSinceEpoch - 1;
@patch
DateTime._internal(int year, int month, int day, int hour, int minute,
int second, int millisecond, int microsecond, bool isUtc)
: this.isUtc = checkNotNullable(isUtc, "isUtc"),
this._value = _brokenDownDateToValue(year, month, day, hour, minute,
second, millisecond, microsecond, isUtc) ??
-1 {
if (_value == -1) throw new ArgumentError();
_sentinelMs {
if (_value == _sentinelMs) throw new ArgumentError();
}
static int _validateMilliseconds(int millisecondsSinceEpoch) =>

View file

@ -94,6 +94,10 @@ void testExtremes() {
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
Expect.throws(() => new DateTime.utc(
dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, -1));
// Regression test for https://dartbug.com/55438
dt = DateTime.utc(1969, 12, 31, 23, 59, 59, 999, 999);
Expect.equals(-1, dt.microsecondsSinceEpoch);
}
void main() {