forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_time_extremes_test.dart
99 lines (91 loc) · 4.35 KB
/
date_time_extremes_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 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.
bool get supportsMicroseconds =>
new DateTime.fromMicrosecondsSinceEpoch(1).microsecondsSinceEpoch == 1;
// Identical to _maxMillisecondsSinceEpoch in date_time.dart
const int _MAX_MILLISECONDS = 8640000000000000;
void testExtremes() {
var dt =
new 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 = new 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 = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
Expect.equals(_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
Expect.equals(-_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
_MAX_MILLISECONDS + 1,
isUtc: true));
Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
-_MAX_MILLISECONDS - 1,
isUtc: true));
Expect.throws(
() => new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS + 1));
Expect.throws(
() => new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS - 1));
dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
Expect.throws(
() => new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 1));
dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
Expect.throws(() =>
new DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 1));
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
Expect.throws(
() => new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, -1));
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
Expect.throws(() =>
new DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, -1));
if (!supportsMicroseconds) return;
dt = new DateTime.fromMicrosecondsSinceEpoch(_MAX_MILLISECONDS * 1000);
dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
Expect.equals(_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
dt = new DateTime.fromMicrosecondsSinceEpoch(-_MAX_MILLISECONDS * 1000);
dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
Expect.equals(-_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
Expect.throws(() => new DateTime.fromMicrosecondsSinceEpoch(
_MAX_MILLISECONDS * 1000 + 1,
isUtc: true));
Expect.throws(() => new DateTime.fromMicrosecondsSinceEpoch(
-_MAX_MILLISECONDS * 1000 - 1,
isUtc: true));
Expect.throws(() =>
new DateTime.fromMicrosecondsSinceEpoch(_MAX_MILLISECONDS * 1000 + 1));
Expect.throws(() =>
new DateTime.fromMicrosecondsSinceEpoch(-_MAX_MILLISECONDS * 1000 - 1));
dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
Expect.throws(() =>
new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, 1));
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));
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
Expect.throws(() =>
new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, -1));
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));
}
void main() {
testExtremes();
}