mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 11:03:19 +00:00
b42bd24b78
Followup to fb057ea4e0
Now that the web implementation of DateTime supports microseconds, the test special cases for not supporting microseconds can be removed.
Change-Id: I10991b25e42d643ae58850d7190621c9d11877b8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/367680
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
132 lines
5.9 KiB
Dart
132 lines
5.9 KiB
Dart
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
// Dart test program for DateTime, extreme values.
|
|
|
|
// Identical to _maxMillisecondsSinceEpoch in date_time.dart
|
|
const int _MAX_MILLISECONDS = 8640000000000000;
|
|
|
|
void testExtremes() {
|
|
var dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
|
|
Expect.equals(275760, dt.year);
|
|
Expect.equals(9, dt.month);
|
|
Expect.equals(13, dt.day);
|
|
Expect.equals(0, dt.hour);
|
|
Expect.equals(0, dt.minute);
|
|
Expect.equals(0, dt.second);
|
|
Expect.equals(0, dt.millisecond);
|
|
Expect.equals(0, dt.microsecond);
|
|
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
|
|
Expect.equals(-271821, dt.year);
|
|
Expect.equals(4, dt.month);
|
|
Expect.equals(20, dt.day);
|
|
Expect.equals(0, dt.hour);
|
|
Expect.equals(0, dt.minute);
|
|
Expect.equals(0, dt.second);
|
|
Expect.equals(0, dt.millisecond);
|
|
Expect.equals(0, dt.microsecond);
|
|
// Make sure that we can build the extreme dates in local too.
|
|
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
|
|
dt = DateTime(
|
|
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.millisecond);
|
|
Expect.equals(_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
|
|
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
|
|
dt = DateTime(
|
|
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.millisecond);
|
|
Expect.equals(-_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
|
|
Expect.throws(() =>
|
|
DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS + 1, isUtc: true));
|
|
Expect.throws(() =>
|
|
DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS - 1, isUtc: true));
|
|
Expect.throws(
|
|
() => DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS + 1));
|
|
Expect.throws(
|
|
() => DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS - 1));
|
|
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
|
|
Expect.throws(
|
|
() => DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 1));
|
|
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
|
|
Expect.throws(
|
|
() => DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 1));
|
|
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
|
|
Expect.throws(
|
|
() => DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, -1));
|
|
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
|
|
Expect.throws(
|
|
() => DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, -1));
|
|
|
|
/// The nearest value to [base] in the direction [delta]. For native `int`s,
|
|
/// this is just `base + delta`. For web `int`s outside the safe range, the
|
|
/// next value might differ by some power of two.
|
|
int nearest(int base, int delta) {
|
|
for (int factor = 1;; factor *= 2) {
|
|
final next = base + delta * factor;
|
|
print(factor);
|
|
if (next != base) return next;
|
|
}
|
|
}
|
|
|
|
dt = DateTime.fromMicrosecondsSinceEpoch(_MAX_MILLISECONDS * 1000);
|
|
dt = DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
|
|
Expect.equals(_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
|
|
print(-_MAX_MILLISECONDS * 1000);
|
|
dt = DateTime.fromMicrosecondsSinceEpoch(-_MAX_MILLISECONDS * 1000);
|
|
dt = DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
|
|
Expect.equals(-_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
|
|
Expect.throws(() => DateTime.fromMicrosecondsSinceEpoch(
|
|
nearest(_MAX_MILLISECONDS * 1000, 1),
|
|
isUtc: true));
|
|
Expect.throws(() => DateTime.fromMicrosecondsSinceEpoch(
|
|
nearest(-_MAX_MILLISECONDS * 1000, -1),
|
|
isUtc: true));
|
|
Expect.throws(() => DateTime.fromMicrosecondsSinceEpoch(
|
|
nearest(_MAX_MILLISECONDS * 1000, 1)));
|
|
Expect.throws(() => DateTime.fromMicrosecondsSinceEpoch(
|
|
nearest(-_MAX_MILLISECONDS * 1000, -1)));
|
|
// These should all succeed - stepping into the valid range rather than out:
|
|
DateTime.fromMicrosecondsSinceEpoch(nearest(-_MAX_MILLISECONDS * 1000, 1),
|
|
isUtc: true);
|
|
DateTime.fromMicrosecondsSinceEpoch(nearest(_MAX_MILLISECONDS * 1000, -1),
|
|
isUtc: true);
|
|
DateTime.fromMicrosecondsSinceEpoch(nearest(-_MAX_MILLISECONDS * 1000, 1));
|
|
DateTime.fromMicrosecondsSinceEpoch(nearest(_MAX_MILLISECONDS * 1000, -1));
|
|
|
|
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
|
|
Expect.throws(
|
|
() => DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, 1));
|
|
Expect.throws(() => dt.copyWith(microsecond: 1));
|
|
Expect.isTrue(dt.copyWith(microsecond: -1).toString().endsWith('.999999'));
|
|
|
|
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
|
|
Expect.throws(() =>
|
|
DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, 1));
|
|
Expect.throws(() => dt.copyWith(microsecond: 1));
|
|
Expect.isTrue(dt.copyWith(microsecond: -1).toString().endsWith('.999999Z'));
|
|
|
|
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
|
|
Expect.throws(
|
|
() => DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, -1));
|
|
Expect.throws(() => dt.copyWith(microsecond: -1));
|
|
Expect.isTrue(dt.copyWith(microsecond: 1).toString().endsWith('.000001'));
|
|
|
|
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
|
|
Expect.throws(() =>
|
|
DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, -1));
|
|
Expect.throws(() => dt.copyWith(microsecond: -1));
|
|
Expect.isTrue(dt.copyWith(microsecond: 1).toString().endsWith('.000001Z'));
|
|
|
|
// Regression test for https://dartbug.com/55438
|
|
dt = DateTime.utc(1969, 12, 31, 23, 59, 59, 999, 999);
|
|
Expect.equals(-1, dt.microsecondsSinceEpoch);
|
|
// The first fix confused millisecondsSinceEpoch and microsecondsSinceEpoch.
|
|
dt = DateTime.utc(1696, 3, 16, 23, 59, 59, 999, 999);
|
|
Expect.equals(-_MAX_MILLISECONDS - 1, dt.microsecondsSinceEpoch);
|
|
}
|
|
|
|
void main() {
|
|
testExtremes();
|
|
}
|