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

Side by Side Diff: tests/corelib/date_time_test.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 unified diff | Download patch
« no previous file with comments | « tests/corelib/date_time_parse_test.dart ('k') | 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 4
5 import "package:expect/expect.dart"; 5 import "package:expect/expect.dart";
6 6
7 // Dart test program for DateTime. 7 // Dart test program for DateTime.
8 8
9 bool get supportsMicroseconds =>
10 new DateTime.fromMicrosecondsSinceEpoch(1).microsecondsSinceEpoch == 1;
11
9 // Identical to _MAX_MILLISECONDS_SINCE_EPOCH in date_time.dart 12 // Identical to _MAX_MILLISECONDS_SINCE_EPOCH in date_time.dart
10 const int _MAX_MILLISECONDS = 8640000000000000; 13 const int _MAX_MILLISECONDS = 8640000000000000;
11 14
12 // Tests if the time moves eventually forward. 15 // Tests if the time moves eventually forward.
13 void testNow() { 16 void testNow() {
14 var t1 = new DateTime.now(); 17 var t1 = new DateTime.now();
15 bool timeMovedForward = false; 18 bool timeMovedForward = false;
16 const int N = 1000000; 19 const int N = 1000000;
17 outer: while (true) { 20 outer: while (true) {
18 for (int i = N; i > 0; i--) { 21 for (int i = N; i > 0; i--) {
19 var t2 = new DateTime.now(); 22 var t2 = new DateTime.now();
20 if (t1.millisecondsSinceEpoch < t2.millisecondsSinceEpoch) { 23 if (t1.millisecondsSinceEpoch < t2.millisecondsSinceEpoch) {
21 break outer; 24 break outer;
22 } 25 }
23 } 26 }
24 print("testNow: No Date.now() progress in $N loops. Time: $t1"); 27 print("testNow: No Date.now() progress in $N loops. Time: $t1");
25 } 28 }
26 Expect.isFalse(t1.isUtc); 29 Expect.isFalse(t1.isUtc);
27 } 30 }
28 31
29 void testValue() { 32 void testMillisecondsSinceEpoch() {
30 var dt1 = new DateTime.now(); 33 var dt1 = new DateTime.now();
31 var millisecondsSinceEpoch = dt1.millisecondsSinceEpoch; 34 var millisecondsSinceEpoch = dt1.millisecondsSinceEpoch;
32 var dt2 = new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch); 35 var dt2 = new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch);
33 Expect.equals(millisecondsSinceEpoch, dt2.millisecondsSinceEpoch); 36 Expect.equals(millisecondsSinceEpoch, dt2.millisecondsSinceEpoch);
34 } 37 }
35 38
39 void testMicrosecondsSinceEpoch() {
40 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1);
41 var microsecondsSinceEpoch = dt1.millisecondsSinceEpoch;
42 var dt2 = new DateTime.fromMicrosecondsSinceEpoch(microsecondsSinceEpoch);
43 Expect.equals(microsecondsSinceEpoch, dt2.microsecondsSinceEpoch);
44
45 if (!supportsMicroseconds) return;
46 dt1 = new DateTime.now();
47 microsecondsSinceEpoch = dt1.millisecondsSinceEpoch;
48 dt2 = new DateTime.fromMicrosecondsSinceEpoch(microsecondsSinceEpoch);
49 Expect.equals(microsecondsSinceEpoch, dt2.microsecondsSinceEpoch);
50 }
51
36 void testFarAwayDates() { 52 void testFarAwayDates() {
37 DateTime dt = 53 DateTime dt =
38 new DateTime.fromMillisecondsSinceEpoch(1000000000000001, isUtc: true); 54 new DateTime.fromMillisecondsSinceEpoch(1000000000000001, isUtc: true);
39 Expect.equals(33658, dt.year); 55 Expect.equals(33658, dt.year);
40 Expect.equals(9, dt.month); 56 Expect.equals(9, dt.month);
41 Expect.equals(27, dt.day); 57 Expect.equals(27, dt.day);
42 Expect.equals(1, dt.hour); 58 Expect.equals(1, dt.hour);
43 Expect.equals(46, dt.minute); 59 Expect.equals(46, dt.minute);
44 Expect.equals(40, dt.second); 60 Expect.equals(40, dt.second);
45 Expect.equals(1, dt.millisecond); 61 Expect.equals(1, dt.millisecond);
62 Expect.equals(0, dt.microsecond);
46 dt = new DateTime.fromMillisecondsSinceEpoch(-1000000000000001, isUtc: true); 63 dt = new DateTime.fromMillisecondsSinceEpoch(-1000000000000001, isUtc: true);
47 Expect.equals(-29719, dt.year); 64 Expect.equals(-29719, dt.year);
48 Expect.equals(4, dt.month); 65 Expect.equals(4, dt.month);
49 Expect.equals(5, dt.day); 66 Expect.equals(5, dt.day);
50 Expect.equals(22, dt.hour); 67 Expect.equals(22, dt.hour);
51 Expect.equals(13, dt.minute); 68 Expect.equals(13, dt.minute);
52 Expect.equals(19, dt.second); 69 Expect.equals(19, dt.second);
53 Expect.equals(999, dt.millisecond); 70 Expect.equals(999, dt.millisecond);
71 Expect.equals(0, dt.microsecond);
54 // Same with local zone. 72 // Same with local zone.
55 dt = new DateTime.fromMillisecondsSinceEpoch(1000000000000001); 73 dt = new DateTime.fromMillisecondsSinceEpoch(1000000000000001);
56 Expect.equals(33658, dt.year); 74 Expect.equals(33658, dt.year);
57 Expect.equals(9, dt.month); 75 Expect.equals(9, dt.month);
58 Expect.equals(true, dt.day == 27 || dt.day == 26); 76 Expect.equals(true, dt.day == 27 || dt.day == 26);
59 // Not much we can test for local hour. 77 // Not much we can test for local hour.
60 Expect.equals(true, dt.hour >= 0 && dt.hour < 24); 78 Expect.equals(true, dt.hour >= 0 && dt.hour < 24);
61 // Timezones can have offsets down to 15 minute. 79 // Timezones can have offsets down to 15 minute.
62 Expect.equals(true, dt.minute % 15 == 46 % 15); 80 Expect.equals(true, dt.minute % 15 == 46 % 15);
63 Expect.equals(40, dt.second); 81 Expect.equals(40, dt.second);
64 Expect.equals(1, dt.millisecond); 82 Expect.equals(1, dt.millisecond);
83 Expect.equals(0, dt.microsecond);
65 dt = new DateTime.fromMillisecondsSinceEpoch(-1000000000000001); 84 dt = new DateTime.fromMillisecondsSinceEpoch(-1000000000000001);
66 Expect.equals(-29719, dt.year); 85 Expect.equals(-29719, dt.year);
67 Expect.equals(4, dt.month); 86 Expect.equals(4, dt.month);
68 Expect.equals(true, 5 == dt.day || 6 == dt.day); 87 Expect.equals(true, 5 == dt.day || 6 == dt.day);
69 // Not much we can test for local hour. 88 // Not much we can test for local hour.
70 Expect.equals(true, dt.hour >= 0 && dt.hour < 24); 89 Expect.equals(true, dt.hour >= 0 && dt.hour < 24);
71 // Timezones can have offsets down to 15 minute. 90 // Timezones can have offsets down to 15 minute.
72 Expect.equals(true, dt.minute % 15 == 13); 91 Expect.equals(true, dt.minute % 15 == 13);
73 Expect.equals(19, dt.second); 92 Expect.equals(19, dt.second);
74 Expect.equals(999, dt.millisecond); 93 Expect.equals(999, dt.millisecond);
94 Expect.equals(0, dt.microsecond);
95
96 if (!supportsMicroseconds) return;
97 dt =
98 new DateTime.fromMicrosecondsSinceEpoch(1000000000000000001, isUtc: true);
99 Expect.equals(33658, dt.year);
100 Expect.equals(9, dt.month);
101 Expect.equals(27, dt.day);
102 Expect.equals(1, dt.hour);
103 Expect.equals(46, dt.minute);
104 Expect.equals(40, dt.second);
105 Expect.equals(0, dt.millisecond);
106 Expect.equals(1, dt.microsecond);
107 dt = new DateTime.fromMicrosecondsSinceEpoch(
108 -1000000000000000001, isUtc: true);
109 Expect.equals(-29719, dt.year);
110 Expect.equals(4, dt.month);
111 Expect.equals(5, dt.day);
112 Expect.equals(22, dt.hour);
113 Expect.equals(13, dt.minute);
114 Expect.equals(19, dt.second);
115 Expect.equals(999, dt.millisecond);
116 Expect.equals(999, dt.microsecond);
117 // Same with local zone.
118 dt = new DateTime.fromMicrosecondsSinceEpoch(1000000000000000001);
119 Expect.equals(33658, dt.year);
120 Expect.equals(9, dt.month);
121 Expect.equals(true, dt.day == 27 || dt.day == 26);
122 // Not much we can test for local hour.
123 Expect.equals(true, dt.hour >= 0 && dt.hour < 24);
124 // Timezones can have offsets down to 15 minute.
125 Expect.equals(true, dt.minute % 15 == 46 % 15);
126 Expect.equals(40, dt.second);
127 Expect.equals(0, dt.millisecond);
128 Expect.equals(1, dt.microsecond);
129 dt = new DateTime.fromMicrosecondsSinceEpoch(-1000000000000000001);
130 Expect.equals(-29719, dt.year);
131 Expect.equals(4, dt.month);
132 Expect.equals(true, 5 == dt.day || 6 == dt.day);
133 // Not much we can test for local hour.
134 Expect.equals(true, dt.hour >= 0 && dt.hour < 24);
135 // Timezones can have offsets down to 15 minute.
136 Expect.equals(true, dt.minute % 15 == 13);
137 Expect.equals(19, dt.second);
138 Expect.equals(999, dt.millisecond);
139 Expect.equals(999, dt.microsecond);
75 } 140 }
76 141
77 void testEquivalentYears() { 142 void testEquivalentYears() {
78 // All hardcoded values come from V8. This means that the values are not 143 // All hardcoded values come from V8. This means that the values are not
79 // necessarily correct (see limitations of DateTime object in 144 // necessarily correct (see limitations of DateTime object in
80 // EcmaScript 15.9.1 and in particular 15.9.1.8/9). 145 // EcmaScript 15.9.1 and in particular 15.9.1.8/9).
81 DateTime dt = new DateTime.fromMillisecondsSinceEpoch(-31485600000, isUtc: tru e); 146 DateTime dt = new DateTime.fromMillisecondsSinceEpoch(-31485600000, isUtc: tru e);
82 Expect.equals(1969, dt.year); 147 Expect.equals(1969, dt.year);
83 Expect.equals(1, dt.month); 148 Expect.equals(1, dt.month);
84 Expect.equals(1, dt.day); 149 Expect.equals(1, dt.day);
85 Expect.equals(14, dt.hour); 150 Expect.equals(14, dt.hour);
86 Expect.equals(0, dt.minute); 151 Expect.equals(0, dt.minute);
87 Expect.equals(0, dt.second); 152 Expect.equals(0, dt.second);
88 Expect.equals(0, dt.millisecond); 153 Expect.equals(0, dt.millisecond);
154 Expect.equals(0, dt.microsecond);
89 dt = new DateTime.fromMillisecondsSinceEpoch(-63108000000, isUtc: true); 155 dt = new DateTime.fromMillisecondsSinceEpoch(-63108000000, isUtc: true);
90 Expect.equals(1968, dt.year); 156 Expect.equals(1968, dt.year);
91 Expect.equals(1, dt.month); 157 Expect.equals(1, dt.month);
92 Expect.equals(1, dt.day); 158 Expect.equals(1, dt.day);
93 Expect.equals(14, dt.hour); 159 Expect.equals(14, dt.hour);
94 Expect.equals(0, dt.minute); 160 Expect.equals(0, dt.minute);
95 Expect.equals(0, dt.second); 161 Expect.equals(0, dt.second);
96 Expect.equals(0, dt.millisecond); 162 Expect.equals(0, dt.millisecond);
163 Expect.equals(0, dt.microsecond);
97 dt = new DateTime.fromMillisecondsSinceEpoch(-94644000000, isUtc: true); 164 dt = new DateTime.fromMillisecondsSinceEpoch(-94644000000, isUtc: true);
98 Expect.equals(1967, dt.year); 165 Expect.equals(1967, dt.year);
99 Expect.equals(1, dt.month); 166 Expect.equals(1, dt.month);
100 Expect.equals(1, dt.day); 167 Expect.equals(1, dt.day);
101 Expect.equals(14, dt.hour); 168 Expect.equals(14, dt.hour);
102 Expect.equals(0, dt.minute); 169 Expect.equals(0, dt.minute);
103 Expect.equals(0, dt.second); 170 Expect.equals(0, dt.second);
104 Expect.equals(0, dt.millisecond); 171 Expect.equals(0, dt.millisecond);
172 Expect.equals(0, dt.microsecond);
105 dt = new DateTime.fromMillisecondsSinceEpoch(-126180000000, isUtc: true); 173 dt = new DateTime.fromMillisecondsSinceEpoch(-126180000000, isUtc: true);
106 Expect.equals(1966, dt.year); 174 Expect.equals(1966, dt.year);
107 Expect.equals(1, dt.month); 175 Expect.equals(1, dt.month);
108 Expect.equals(1, dt.day); 176 Expect.equals(1, dt.day);
109 Expect.equals(14, dt.hour); 177 Expect.equals(14, dt.hour);
110 Expect.equals(0, dt.minute); 178 Expect.equals(0, dt.minute);
111 Expect.equals(0, dt.second); 179 Expect.equals(0, dt.second);
112 Expect.equals(0, dt.millisecond); 180 Expect.equals(0, dt.millisecond);
181 Expect.equals(0, dt.microsecond);
113 dt = new DateTime.fromMillisecondsSinceEpoch(-157716000000, isUtc: true); 182 dt = new DateTime.fromMillisecondsSinceEpoch(-157716000000, isUtc: true);
114 Expect.equals(1965, dt.year); 183 Expect.equals(1965, dt.year);
115 Expect.equals(1, dt.month); 184 Expect.equals(1, dt.month);
116 Expect.equals(1, dt.day); 185 Expect.equals(1, dt.day);
117 Expect.equals(14, dt.hour); 186 Expect.equals(14, dt.hour);
118 Expect.equals(0, dt.minute); 187 Expect.equals(0, dt.minute);
119 Expect.equals(0, dt.second); 188 Expect.equals(0, dt.second);
120 Expect.equals(0, dt.millisecond); 189 Expect.equals(0, dt.millisecond);
190 Expect.equals(0, dt.microsecond);
121 dt = new DateTime.fromMillisecondsSinceEpoch(-2177402400000, isUtc: true); 191 dt = new DateTime.fromMillisecondsSinceEpoch(-2177402400000, isUtc: true);
122 Expect.equals(1901, dt.year); 192 Expect.equals(1901, dt.year);
123 Expect.equals(1, dt.month); 193 Expect.equals(1, dt.month);
124 Expect.equals(1, dt.day); 194 Expect.equals(1, dt.day);
125 Expect.equals(14, dt.hour); 195 Expect.equals(14, dt.hour);
126 Expect.equals(0, dt.minute); 196 Expect.equals(0, dt.minute);
127 Expect.equals(0, dt.second); 197 Expect.equals(0, dt.second);
128 Expect.equals(0, dt.millisecond); 198 Expect.equals(0, dt.millisecond);
199 Expect.equals(0, dt.microsecond);
129 dt = new DateTime.fromMillisecondsSinceEpoch(-5333076000000, isUtc: true); 200 dt = new DateTime.fromMillisecondsSinceEpoch(-5333076000000, isUtc: true);
130 Expect.equals(1801, dt.year); 201 Expect.equals(1801, dt.year);
131 Expect.equals(1, dt.month); 202 Expect.equals(1, dt.month);
132 Expect.equals(1, dt.day); 203 Expect.equals(1, dt.day);
133 Expect.equals(14, dt.hour); 204 Expect.equals(14, dt.hour);
134 Expect.equals(0, dt.minute); 205 Expect.equals(0, dt.minute);
135 Expect.equals(0, dt.second); 206 Expect.equals(0, dt.second);
136 Expect.equals(0, dt.millisecond); 207 Expect.equals(0, dt.millisecond);
208 Expect.equals(0, dt.microsecond);
137 dt = new DateTime.fromMillisecondsSinceEpoch(-8520285600000, isUtc: true); 209 dt = new DateTime.fromMillisecondsSinceEpoch(-8520285600000, isUtc: true);
138 Expect.equals(1700, dt.year); 210 Expect.equals(1700, dt.year);
139 Expect.equals(1, dt.month); 211 Expect.equals(1, dt.month);
140 Expect.equals(1, dt.day); 212 Expect.equals(1, dt.day);
141 Expect.equals(14, dt.hour); 213 Expect.equals(14, dt.hour);
142 Expect.equals(0, dt.minute); 214 Expect.equals(0, dt.minute);
143 Expect.equals(0, dt.second); 215 Expect.equals(0, dt.second);
144 Expect.equals(0, dt.millisecond); 216 Expect.equals(0, dt.millisecond);
217 Expect.equals(0, dt.microsecond);
145 dt = new DateTime.fromMillisecondsSinceEpoch(-14831719200000, isUtc: true); 218 dt = new DateTime.fromMillisecondsSinceEpoch(-14831719200000, isUtc: true);
146 Expect.equals(1500, dt.year); 219 Expect.equals(1500, dt.year);
147 Expect.equals(1, dt.month); 220 Expect.equals(1, dt.month);
148 Expect.equals(1, dt.day); 221 Expect.equals(1, dt.day);
149 Expect.equals(14, dt.hour); 222 Expect.equals(14, dt.hour);
150 Expect.equals(0, dt.minute); 223 Expect.equals(0, dt.minute);
151 Expect.equals(0, dt.second); 224 Expect.equals(0, dt.second);
152 Expect.equals(0, dt.millisecond); 225 Expect.equals(0, dt.millisecond);
226 Expect.equals(0, dt.microsecond);
153 dt = new DateTime.fromMillisecondsSinceEpoch(-59011408800000, isUtc: true); 227 dt = new DateTime.fromMillisecondsSinceEpoch(-59011408800000, isUtc: true);
154 Expect.equals(100, dt.year); 228 Expect.equals(100, dt.year);
155 Expect.equals(1, dt.month); 229 Expect.equals(1, dt.month);
156 Expect.equals(1, dt.day); 230 Expect.equals(1, dt.day);
157 Expect.equals(14, dt.hour); 231 Expect.equals(14, dt.hour);
158 Expect.equals(0, dt.minute); 232 Expect.equals(0, dt.minute);
159 Expect.equals(0, dt.second); 233 Expect.equals(0, dt.second);
160 Expect.equals(0, dt.millisecond); 234 Expect.equals(0, dt.millisecond);
235 Expect.equals(0, dt.microsecond);
161 dt = new DateTime.fromMillisecondsSinceEpoch(-62011408800000, isUtc: true); 236 dt = new DateTime.fromMillisecondsSinceEpoch(-62011408800000, isUtc: true);
162 Expect.equals(4, dt.year); 237 Expect.equals(4, dt.year);
163 Expect.equals(12, dt.month); 238 Expect.equals(12, dt.month);
164 Expect.equals(8, dt.day); 239 Expect.equals(8, dt.day);
165 Expect.equals(8, dt.hour); 240 Expect.equals(8, dt.hour);
166 Expect.equals(40, dt.minute); 241 Expect.equals(40, dt.minute);
167 Expect.equals(0, dt.second); 242 Expect.equals(0, dt.second);
168 Expect.equals(0, dt.millisecond); 243 Expect.equals(0, dt.millisecond);
244 Expect.equals(0, dt.microsecond);
169 dt = new DateTime.fromMillisecondsSinceEpoch(-64011408800000, isUtc: true); 245 dt = new DateTime.fromMillisecondsSinceEpoch(-64011408800000, isUtc: true);
170 Expect.equals(-59, dt.year); 246 Expect.equals(-59, dt.year);
171 Expect.equals(7, dt.month); 247 Expect.equals(7, dt.month);
172 Expect.equals(24, dt.day); 248 Expect.equals(24, dt.day);
173 Expect.equals(5, dt.hour); 249 Expect.equals(5, dt.hour);
174 Expect.equals(6, dt.minute); 250 Expect.equals(6, dt.minute);
175 Expect.equals(40, dt.second); 251 Expect.equals(40, dt.second);
176 Expect.equals(0, dt.millisecond); 252 Expect.equals(0, dt.millisecond);
253 Expect.equals(0, dt.microsecond);
177 final int SECONDS_YEAR_2035 = 2051222400; 254 final int SECONDS_YEAR_2035 = 2051222400;
178 dt = new DateTime.fromMillisecondsSinceEpoch(SECONDS_YEAR_2035 * 1000 + 1, 255 dt = new DateTime.fromMillisecondsSinceEpoch(SECONDS_YEAR_2035 * 1000 + 1,
179 isUtc: true); 256 isUtc: true);
180 Expect.equals(2035, dt.year); 257 Expect.equals(2035, dt.year);
181 Expect.equals(1, dt.month); 258 Expect.equals(1, dt.month);
182 Expect.equals(1, dt.day); 259 Expect.equals(1, dt.day);
183 Expect.equals(0, dt.hour); 260 Expect.equals(0, dt.hour);
184 Expect.equals(0, dt.minute); 261 Expect.equals(0, dt.minute);
185 Expect.equals(0, dt.second); 262 Expect.equals(0, dt.second);
186 Expect.equals(1, dt.millisecond); 263 Expect.equals(1, dt.millisecond);
264 Expect.equals(0, dt.microsecond);
187 dt = new DateTime.fromMillisecondsSinceEpoch(SECONDS_YEAR_2035 * 1000 - 1, 265 dt = new DateTime.fromMillisecondsSinceEpoch(SECONDS_YEAR_2035 * 1000 - 1,
188 isUtc: true); 266 isUtc: true);
189 Expect.equals(2034, dt.year); 267 Expect.equals(2034, dt.year);
190 Expect.equals(12, dt.month); 268 Expect.equals(12, dt.month);
191 Expect.equals(31, dt.day); 269 Expect.equals(31, dt.day);
192 Expect.equals(23, dt.hour); 270 Expect.equals(23, dt.hour);
193 Expect.equals(59, dt.minute); 271 Expect.equals(59, dt.minute);
194 Expect.equals(59, dt.second); 272 Expect.equals(59, dt.second);
195 Expect.equals(999, dt.millisecond); 273 Expect.equals(999, dt.millisecond);
274 Expect.equals(0, dt.microsecond);
275
196 dt = new DateTime.utc(2035, 1, 1, 0, 0, 0, 1); 276 dt = new DateTime.utc(2035, 1, 1, 0, 0, 0, 1);
197 Expect.equals(SECONDS_YEAR_2035 * 1000 + 1, dt.millisecondsSinceEpoch); 277 Expect.equals(SECONDS_YEAR_2035 * 1000 + 1, dt.millisecondsSinceEpoch);
198 dt = new DateTime.utc(2034, 12, 31, 23, 59, 59, 999); 278 dt = new DateTime.utc(2034, 12, 31, 23, 59, 59, 999);
199 Expect.equals(SECONDS_YEAR_2035 * 1000 - 1, dt.millisecondsSinceEpoch); 279 Expect.equals(SECONDS_YEAR_2035 * 1000 - 1, dt.millisecondsSinceEpoch);
200 dt = new DateTime.fromMillisecondsSinceEpoch(SECONDS_YEAR_2035 * 1000 + 1); 280 dt = new DateTime.fromMillisecondsSinceEpoch(SECONDS_YEAR_2035 * 1000 + 1);
201 Expect.equals(true, (2035 == dt.year && 1 == dt.month && 1 == dt.day) || 281 Expect.equals(true, (2035 == dt.year && 1 == dt.month && 1 == dt.day) ||
202 (2034 == dt.year && 12 == dt.month && 31 == dt.day)); 282 (2034 == dt.year && 12 == dt.month && 31 == dt.day));
203 Expect.equals(0, dt.second); 283 Expect.equals(0, dt.second);
204 Expect.equals(1, dt.millisecond); 284 Expect.equals(1, dt.millisecond);
205 DateTime dt2 = new DateTime( 285 DateTime dt2 = new DateTime(
206 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, 286 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
207 dt.millisecond); 287 dt.millisecond);
208 Expect.equals(dt.millisecondsSinceEpoch, dt2.millisecondsSinceEpoch); 288 Expect.equals(dt.millisecondsSinceEpoch, dt2.millisecondsSinceEpoch);
209 dt = new DateTime.fromMillisecondsSinceEpoch(SECONDS_YEAR_2035 * 1000 - 1); 289 dt = new DateTime.fromMillisecondsSinceEpoch(SECONDS_YEAR_2035 * 1000 - 1);
210 Expect.equals(true, (2035 == dt.year && 1 == dt.month && 1 == dt.day) || 290 Expect.equals(true, (2035 == dt.year && 1 == dt.month && 1 == dt.day) ||
211 (2034 == dt.year && 12 == dt.month && 31 == dt.day)); 291 (2034 == dt.year && 12 == dt.month && 31 == dt.day));
212 Expect.equals(59, dt.second); 292 Expect.equals(59, dt.second);
213 Expect.equals(999, dt.millisecond); 293 Expect.equals(999, dt.millisecond);
294 Expect.equals(0, dt.microsecond);
295
214 dt2 = new DateTime( 296 dt2 = new DateTime(
215 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, 297 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
216 dt.millisecond); 298 dt.millisecond);
217 Expect.equals(dt.millisecondsSinceEpoch, dt2.millisecondsSinceEpoch); 299 Expect.equals(dt.millisecondsSinceEpoch, dt2.millisecondsSinceEpoch);
218 dt = new DateTime.fromMillisecondsSinceEpoch(2100000000 * 1000, isUtc: true); 300 dt = new DateTime.fromMillisecondsSinceEpoch(2100000000 * 1000, isUtc: true);
219 Expect.equals(2036, dt.year); 301 Expect.equals(2036, dt.year);
220 Expect.equals(7, dt.month); 302 Expect.equals(7, dt.month);
221 Expect.equals(18, dt.day); 303 Expect.equals(18, dt.day);
222 Expect.equals(13, dt.hour); 304 Expect.equals(13, dt.hour);
223 Expect.equals(20, dt.minute); 305 Expect.equals(20, dt.minute);
224 Expect.equals(0, dt.second); 306 Expect.equals(0, dt.second);
225 Expect.equals(0, dt.millisecond); 307 Expect.equals(0, dt.millisecond);
308 Expect.equals(0, dt.microsecond);
309
226 // Internally this will use the maximum value for the native calls. 310 // Internally this will use the maximum value for the native calls.
227 dt = new DateTime(2036, 7, 18, 13, 20); 311 dt = new DateTime(2036, 7, 18, 13, 20);
228 Expect.equals(2036, dt.year); 312 Expect.equals(2036, dt.year);
229 Expect.equals(7, dt.month); 313 Expect.equals(7, dt.month);
230 Expect.equals(18, dt.day); 314 Expect.equals(18, dt.day);
231 Expect.equals(13, dt.hour); 315 Expect.equals(13, dt.hour);
232 Expect.equals(20, dt.minute); 316 Expect.equals(20, dt.minute);
233 Expect.equals(0, dt.second); 317 Expect.equals(0, dt.second);
234 Expect.equals(0, dt.millisecond); 318 Expect.equals(0, dt.millisecond);
319 Expect.equals(0, dt.microsecond);
235 Expect.equals("2036-07-18 13:20:00.000", dt.toString()); 320 Expect.equals("2036-07-18 13:20:00.000", dt.toString());
321
322 if (!supportsMicroseconds) return;
323
324 dt = new DateTime.utc(2035, 1, 1, 0, 0, 0, 0, 1);
325 Expect.equals(SECONDS_YEAR_2035 * 1000000 + 1, dt.microsecondsSinceEpoch);
326 dt = new DateTime.utc(2034, 12, 31, 23, 59, 59, 999, 999);
327 Expect.equals(SECONDS_YEAR_2035 * 1000000 - 1, dt.microsecondsSinceEpoch);
328 dt = new DateTime.fromMicrosecondsSinceEpoch(SECONDS_YEAR_2035 * 1000000 + 1);
329 Expect.equals(true, (2035 == dt.year && 1 == dt.month && 1 == dt.day) ||
330 (2034 == dt.year && 12 == dt.month && 31 == dt.day));
331 Expect.equals(0, dt.second);
332 Expect.equals(0, dt.millisecond);
333 Expect.equals(1, dt.microsecond);
334 dt2 = new DateTime(
335 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
336 dt.millisecond, dt.microsecond);
337 Expect.equals(dt.microsecondsSinceEpoch, dt2.microsecondsSinceEpoch);
338 dt = new DateTime.fromMicrosecondsSinceEpoch(SECONDS_YEAR_2035 * 1000000 - 1);
339 Expect.equals(true, (2035 == dt.year && 1 == dt.month && 1 == dt.day) ||
340 (2034 == dt.year && 12 == dt.month && 31 == dt.day));
341 Expect.equals(59, dt.second);
342 Expect.equals(999, dt.millisecond);
343 Expect.equals(999, dt.microsecond);
344
345 dt2 = new DateTime(
346 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
347 dt.millisecond, dt.microsecond);
348 Expect.equals(dt.millisecondsSinceEpoch, dt2.millisecondsSinceEpoch);
349 dt = new DateTime.fromMicrosecondsSinceEpoch(2100000000 * 1000000,
350 isUtc: true);
351 Expect.equals(2036, dt.year);
352 Expect.equals(7, dt.month);
353 Expect.equals(18, dt.day);
354 Expect.equals(13, dt.hour);
355 Expect.equals(20, dt.minute);
356 Expect.equals(0, dt.second);
357 Expect.equals(0, dt.millisecond);
358 Expect.equals(0, dt.microsecond);
236 } 359 }
237 360
238 void testExtremes() { 361 void testExtremes() {
239 var dt = 362 var dt =
240 new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true); 363 new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
241 Expect.equals(275760, dt.year); 364 Expect.equals(275760, dt.year);
242 Expect.equals(9, dt.month); 365 Expect.equals(9, dt.month);
243 Expect.equals(13, dt.day); 366 Expect.equals(13, dt.day);
244 Expect.equals(0, dt.hour); 367 Expect.equals(0, dt.hour);
245 Expect.equals(0, dt.minute); 368 Expect.equals(0, dt.minute);
246 Expect.equals(0, dt.second); 369 Expect.equals(0, dt.second);
247 Expect.equals(0, dt.millisecond); 370 Expect.equals(0, dt.millisecond);
371 Expect.equals(0, dt.microsecond);
248 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true); 372 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
249 Expect.equals(-271821, dt.year); 373 Expect.equals(-271821, dt.year);
250 Expect.equals(4, dt.month); 374 Expect.equals(4, dt.month);
251 Expect.equals(20, dt.day); 375 Expect.equals(20, dt.day);
252 Expect.equals(0, dt.hour); 376 Expect.equals(0, dt.hour);
253 Expect.equals(0, dt.minute); 377 Expect.equals(0, dt.minute);
254 Expect.equals(0, dt.second); 378 Expect.equals(0, dt.second);
255 Expect.equals(0, dt.millisecond); 379 Expect.equals(0, dt.millisecond);
380 Expect.equals(0, dt.microsecond);
256 // Make sure that we can build the extreme dates in local too. 381 // Make sure that we can build the extreme dates in local too.
257 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS); 382 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
258 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute); 383 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
259 Expect.equals(_MAX_MILLISECONDS, dt.millisecondsSinceEpoch); 384 Expect.equals(_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
260 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS); 385 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
261 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute); 386 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
262 Expect.equals(-_MAX_MILLISECONDS, dt.millisecondsSinceEpoch); 387 Expect.equals(-_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
263 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch( 388 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
264 _MAX_MILLISECONDS + 1, isUtc: true)); 389 _MAX_MILLISECONDS + 1, isUtc: true));
265 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch( 390 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
266 -_MAX_MILLISECONDS - 1, isUtc: true)); 391 -_MAX_MILLISECONDS - 1, isUtc: true));
267 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch( 392 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
268 _MAX_MILLISECONDS + 1)); 393 _MAX_MILLISECONDS + 1));
269 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch( 394 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
270 -_MAX_MILLISECONDS - 1)); 395 -_MAX_MILLISECONDS - 1));
271 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS); 396 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
272 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day, 397 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day,
273 dt.hour, dt.minute, 0, 1)); 398 dt.hour, dt.minute, 0, 1));
274 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true); 399 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
275 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day, 400 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day,
276 dt.hour, dt.minute, 0, 1)); 401 dt.hour, dt.minute, 0, 1));
277 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS); 402 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
278 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day, 403 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day,
279 dt.hour, dt.minute, 0, -1)); 404 dt.hour, dt.minute, 0, -1));
280 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true); 405 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
281 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day, 406 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day,
282 dt.hour, dt.minute, 0, -1)); 407 dt.hour, dt.minute, 0, -1));
408
409 if (!supportsMicroseconds) return;
410
411 dt = new DateTime.fromMicrosecondsSinceEpoch(_MAX_MILLISECONDS * 1000);
412 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
413 Expect.equals(_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
414 dt = new DateTime.fromMicrosecondsSinceEpoch(-_MAX_MILLISECONDS * 1000);
415 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
416 Expect.equals(-_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
417 Expect.throws(() => new DateTime.fromMicrosecondsSinceEpoch(
418 _MAX_MILLISECONDS * 1000 + 1, isUtc: true));
419 Expect.throws(() => new DateTime.fromMicrosecondsSinceEpoch(
420 -_MAX_MILLISECONDS * 1000 - 1, isUtc: true));
421 Expect.throws(() => new DateTime.fromMicrosecondsSinceEpoch(
422 _MAX_MILLISECONDS * 1000 + 1));
423 Expect.throws(() => new DateTime.fromMicrosecondsSinceEpoch(
424 -_MAX_MILLISECONDS * 1000 - 1));
425 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
426 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day,
427 dt.hour, dt.minute, 0, 0, 1));
428 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
429 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day,
430 dt.hour, dt.minute, 0, 0, 1));
431 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
432 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day,
433 dt.hour, dt.minute, 0, 0, -1));
434 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
435 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day,
436 dt.hour, dt.minute, 0, 0, -1));
283 } 437 }
284 438
285 void testUTCGetters() { 439 void testUTCGetters() {
286 var dt = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true); 440 var dt = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true);
287 Expect.equals(2011, dt.year); 441 Expect.equals(2011, dt.year);
288 Expect.equals(5, dt.month); 442 Expect.equals(5, dt.month);
289 Expect.equals(11, dt.day); 443 Expect.equals(11, dt.day);
290 Expect.equals(18, dt.hour); 444 Expect.equals(18, dt.hour);
291 Expect.equals(58, dt.minute); 445 Expect.equals(58, dt.minute);
292 Expect.equals(35, dt.second); 446 Expect.equals(35, dt.second);
293 Expect.equals(0, dt.millisecond); 447 Expect.equals(0, dt.millisecond);
448 Expect.equals(0, dt.microsecond);
294 Expect.equals(true, dt.isUtc); 449 Expect.equals(true, dt.isUtc);
295 Expect.equals(1305140315000, dt.millisecondsSinceEpoch); 450 Expect.equals(1305140315000, dt.millisecondsSinceEpoch);
296 dt = new DateTime.fromMillisecondsSinceEpoch(-9999999, isUtc: true); 451 dt = new DateTime.fromMillisecondsSinceEpoch(-9999999, isUtc: true);
297 Expect.equals(1969, dt.year); 452 Expect.equals(1969, dt.year);
298 Expect.equals(12, dt.month); 453 Expect.equals(12, dt.month);
299 Expect.equals(31, dt.day); 454 Expect.equals(31, dt.day);
300 Expect.equals(21, dt.hour); 455 Expect.equals(21, dt.hour);
301 Expect.equals(13, dt.minute); 456 Expect.equals(13, dt.minute);
302 Expect.equals(20, dt.second); 457 Expect.equals(20, dt.second);
303 Expect.equals(1, dt.millisecond); 458 Expect.equals(1, dt.millisecond);
459 Expect.equals(0, dt.microsecond);
460
461 if (!supportsMicroseconds) return;
462
463 dt = new DateTime.fromMicrosecondsSinceEpoch(-9999999999, isUtc: true);
464 Expect.equals(1969, dt.year);
465 Expect.equals(12, dt.month);
466 Expect.equals(31, dt.day);
467 Expect.equals(21, dt.hour);
468 Expect.equals(13, dt.minute);
469 Expect.equals(20, dt.second);
470 Expect.equals(0, dt.millisecond);
471 Expect.equals(1, dt.microsecond);
304 } 472 }
305 473
306 void testLocalGetters() { 474 void testLocalGetters() {
307 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000); 475 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000);
308 var dt2 = new DateTime.utc(dt1.year, dt1.month, dt1.day, 476 var dt2 = new DateTime.utc(dt1.year, dt1.month, dt1.day,
309 dt1.hour, dt1.minute, dt1.second, dt1.millisecond); 477 dt1.hour, dt1.minute, dt1.second, dt1.millisecond,
478 dt1.microsecond);
310 Duration zoneOffset = dt1.difference(dt2); 479 Duration zoneOffset = dt1.difference(dt2);
311 Expect.equals(true, zoneOffset.inDays == 0); 480 Expect.equals(true, zoneOffset.inDays == 0);
312 Expect.equals(true, zoneOffset.inHours.abs() <= 12); 481 Expect.equals(true, zoneOffset.inHours.abs() <= 12);
313 Expect.equals(dt1.year, dt2.year); 482 Expect.equals(dt1.year, dt2.year);
314 Expect.equals(dt1.month, dt2.month); 483 Expect.equals(dt1.month, dt2.month);
315 Expect.equals(true, (dt1.day - dt2.day).abs() <= 1); 484 Expect.equals(true, (dt1.day - dt2.day).abs() <= 1);
316 Expect.equals(true, dt1.hour < 24); 485 Expect.equals(true, dt1.hour < 24);
317 // There are timezones with 0.5 or 0.25 hour offsets. 486 // There are timezones with 0.5 or 0.25 hour offsets.
318 Expect.equals(true, 487 Expect.equals(true,
319 (dt1.minute == dt2.minute) || 488 (dt1.minute == dt2.minute) ||
320 ((dt1.minute - dt2.minute).abs() == 30) || 489 ((dt1.minute - dt2.minute).abs() == 30) ||
321 ((dt1.minute - dt2.minute).abs() == 15)); 490 ((dt1.minute - dt2.minute).abs() == 15));
322 Expect.equals(dt1.second, dt2.second); 491 Expect.equals(dt1.second, dt2.second);
323 Expect.equals(dt1.millisecond, dt2.millisecond); 492 Expect.equals(dt1.millisecond, dt2.millisecond);
493 Expect.equals(dt1.microsecond, dt2.microsecond);
324 } 494 }
325 495
326 void testConstructors() { 496 void testConstructors() {
327 var dt0 = new DateTime.utc(2011, 5, 11, 18, 58, 35, 0); 497 var dt0 = new DateTime.utc(2011, 5, 11, 18, 58, 35, 0, 0);
328 var dt0b = new DateTime.utc(2011, 5, 11, 18, 58, 35, 0).toLocal(); 498 var dt0b = new DateTime.utc(2011, 5, 11, 18, 58, 35, 0, 0).toLocal();
329 Expect.equals(1305140315000, dt0.millisecondsSinceEpoch); 499 Expect.equals(1305140315000, dt0.millisecondsSinceEpoch);
330 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000); 500 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000);
331 Expect.equals(dt1.millisecondsSinceEpoch, dt0.millisecondsSinceEpoch); 501 Expect.equals(dt1.millisecondsSinceEpoch, dt0.millisecondsSinceEpoch);
502 Expect.equals(dt1.microsecondsSinceEpoch, dt0.microsecondsSinceEpoch);
332 Expect.equals(false, dt1 == dt0); 503 Expect.equals(false, dt1 == dt0);
333 Expect.equals(true, dt1 == dt0b); 504 Expect.equals(true, dt1 == dt0b);
334 var dt3 = new DateTime(dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute, 505 var dt3 = new DateTime(dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute,
335 dt1.second, dt1.millisecond); 506 dt1.second, dt1.millisecond, dt1.microsecond);
336 Expect.equals(dt1.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch); 507 Expect.equals(dt1.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch);
508 Expect.equals(dt1.microsecondsSinceEpoch, dt3.microsecondsSinceEpoch);
337 Expect.equals(false, dt3 == dt0); 509 Expect.equals(false, dt3 == dt0);
338 Expect.equals(true, dt1 == dt3); 510 Expect.equals(true, dt1 == dt3);
339 dt3 = new DateTime( 511 dt3 = new DateTime(
340 dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute, 512 dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute,
341 dt1.second, dt1.millisecond); 513 dt1.second, dt1.millisecond, dt1.microsecond);
342 Expect.equals(dt1.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch); 514 Expect.equals(dt1.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch);
515 Expect.equals(dt1.microsecondsSinceEpoch, dt3.microsecondsSinceEpoch);
343 Expect.equals(true, dt1 == dt3); 516 Expect.equals(true, dt1 == dt3);
344 var dt2 = dt1.toLocal(); 517 var dt2 = dt1.toLocal();
345 dt3 = new DateTime(2011, 5, dt1.day, dt1.hour, dt1.minute, 35, 0); 518 dt3 = new DateTime(2011, 5, dt1.day, dt1.hour, dt1.minute, 35, 0, 0);
346 Expect.equals(dt2.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch); 519 Expect.equals(dt2.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch);
520 Expect.equals(dt2.microsecondsSinceEpoch, dt3.microsecondsSinceEpoch);
347 Expect.equals(true, dt2 == dt3); 521 Expect.equals(true, dt2 == dt3);
348 dt1 = new DateTime.fromMillisecondsSinceEpoch(-9999999, isUtc: true); 522 dt1 = new DateTime.fromMillisecondsSinceEpoch(-9999999, isUtc: true);
349 dt3 = new DateTime.utc(dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute, 523 dt3 = new DateTime.utc(dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute,
350 dt1.second, dt1.millisecond); 524 dt1.second, dt1.millisecond);
351 Expect.equals(dt1.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch); 525 Expect.equals(dt1.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch);
526 Expect.equals(dt1.microsecondsSinceEpoch, dt3.microsecondsSinceEpoch);
352 dt3 = new DateTime.utc(99, 1, 2, 10, 11, 12, 0); 527 dt3 = new DateTime.utc(99, 1, 2, 10, 11, 12, 0);
353 Expect.equals(99, dt3.year); 528 Expect.equals(99, dt3.year);
354 Expect.equals(1, dt3.month); 529 Expect.equals(1, dt3.month);
355 Expect.equals(2, dt3.day); 530 Expect.equals(2, dt3.day);
356 Expect.equals(10, dt3.hour); 531 Expect.equals(10, dt3.hour);
357 Expect.equals(11, dt3.minute); 532 Expect.equals(11, dt3.minute);
358 Expect.equals(12, dt3.second); 533 Expect.equals(12, dt3.second);
359 Expect.equals(0, dt3.millisecond); 534 Expect.equals(0, dt3.millisecond);
535 Expect.equals(0, dt3.microsecond);
360 Expect.equals(true, dt3.isUtc); 536 Expect.equals(true, dt3.isUtc);
361 var dt4 = new DateTime(99, 1, 2); 537 var dt4 = new DateTime(99, 1, 2);
362 Expect.equals(99, dt4.year); 538 Expect.equals(99, dt4.year);
363 Expect.equals(1, dt4.month); 539 Expect.equals(1, dt4.month);
364 Expect.equals(2, dt4.day); 540 Expect.equals(2, dt4.day);
365 Expect.equals(0, dt4.hour); 541 Expect.equals(0, dt4.hour);
366 Expect.equals(0, dt4.minute); 542 Expect.equals(0, dt4.minute);
367 Expect.equals(0, dt4.second); 543 Expect.equals(0, dt4.second);
368 Expect.equals(0, dt4.millisecond); 544 Expect.equals(0, dt4.millisecond);
545 Expect.equals(0, dt4.microsecond);
369 Expect.isFalse(dt4.isUtc); 546 Expect.isFalse(dt4.isUtc);
370 var dt5 = new DateTime.utc(99, 1, 2); 547 var dt5 = new DateTime.utc(99, 1, 2);
371 Expect.equals(99, dt5.year); 548 Expect.equals(99, dt5.year);
372 Expect.equals(1, dt5.month); 549 Expect.equals(1, dt5.month);
373 Expect.equals(2, dt5.day); 550 Expect.equals(2, dt5.day);
374 Expect.equals(0, dt5.hour); 551 Expect.equals(0, dt5.hour);
375 Expect.equals(0, dt5.minute); 552 Expect.equals(0, dt5.minute);
376 Expect.equals(0, dt5.second); 553 Expect.equals(0, dt5.second);
377 Expect.equals(0, dt5.millisecond); 554 Expect.equals(0, dt5.millisecond);
555 Expect.equals(0, dt5.microsecond);
378 Expect.isTrue(dt5.isUtc); 556 Expect.isTrue(dt5.isUtc);
379 var dt6 = new DateTime(2012, 2, 27, 13, 27, 0); 557 var dt6 = new DateTime(2012, 2, 27, 13, 27, 0);
380 Expect.equals(2012, dt6.year); 558 Expect.equals(2012, dt6.year);
381 Expect.equals(2, dt6.month); 559 Expect.equals(2, dt6.month);
382 Expect.equals(27, dt6.day); 560 Expect.equals(27, dt6.day);
383 Expect.equals(13, dt6.hour); 561 Expect.equals(13, dt6.hour);
384 Expect.equals(27, dt6.minute); 562 Expect.equals(27, dt6.minute);
385 Expect.equals(0, dt6.second); 563 Expect.equals(0, dt6.second);
386 Expect.equals(0, dt6.millisecond); 564 Expect.equals(0, dt6.millisecond);
565 Expect.equals(0, dt6.microsecond);
387 Expect.isFalse(dt6.isUtc); 566 Expect.isFalse(dt6.isUtc);
388 var dt7 = new DateTime.utc(2012, 2, 27, 13, 27, 0); 567 var dt7 = new DateTime.utc(2012, 2, 27, 13, 27, 0);
389 Expect.equals(2012, dt7.year); 568 Expect.equals(2012, dt7.year);
390 Expect.equals(2, dt7.month); 569 Expect.equals(2, dt7.month);
391 Expect.equals(27, dt7.day); 570 Expect.equals(27, dt7.day);
392 Expect.equals(13, dt7.hour); 571 Expect.equals(13, dt7.hour);
393 Expect.equals(27, dt7.minute); 572 Expect.equals(27, dt7.minute);
394 Expect.equals(0, dt7.second); 573 Expect.equals(0, dt7.second);
395 Expect.equals(0, dt7.millisecond); 574 Expect.equals(0, dt7.millisecond);
575 Expect.equals(0, dt7.microsecond);
396 Expect.isTrue(dt7.isUtc); 576 Expect.isTrue(dt7.isUtc);
397 } 577 }
398 578
399 void testChangeTimeZone() { 579 void testChangeTimeZone() {
400 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000); 580 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000);
401 var dt2 = dt1.toUtc(); 581 var dt2 = dt1.toUtc();
402 Expect.equals(dt1.millisecondsSinceEpoch, dt2.millisecondsSinceEpoch); 582 Expect.equals(dt1.millisecondsSinceEpoch, dt2.millisecondsSinceEpoch);
403 var dt3 = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true); 583 var dt3 = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true);
404 Expect.equals(dt1.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch); 584 Expect.equals(dt1.millisecondsSinceEpoch, dt3.millisecondsSinceEpoch);
405 Expect.equals(dt2.year, dt3.year); 585 Expect.equals(dt2.year, dt3.year);
406 Expect.equals(dt2.month, dt3.month); 586 Expect.equals(dt2.month, dt3.month);
407 Expect.equals(dt2.day, dt3.day); 587 Expect.equals(dt2.day, dt3.day);
408 Expect.equals(dt2.hour, dt3.hour); 588 Expect.equals(dt2.hour, dt3.hour);
409 Expect.equals(dt2.minute, dt3.minute); 589 Expect.equals(dt2.minute, dt3.minute);
410 Expect.equals(dt2.second, dt3.second); 590 Expect.equals(dt2.second, dt3.second);
411 Expect.equals(dt2.millisecond, dt3.millisecond); 591 Expect.equals(dt2.millisecond, dt3.millisecond);
592 Expect.equals(dt2.microsecond, dt3.microsecond);
412 var dt4 = dt3.toLocal(); 593 var dt4 = dt3.toLocal();
413 Expect.equals(dt1.year, dt4.year); 594 Expect.equals(dt1.year, dt4.year);
414 Expect.equals(dt1.month, dt4.month); 595 Expect.equals(dt1.month, dt4.month);
415 Expect.equals(dt1.day, dt4.day); 596 Expect.equals(dt1.day, dt4.day);
416 Expect.equals(dt1.hour, dt4.hour); 597 Expect.equals(dt1.hour, dt4.hour);
417 Expect.equals(dt1.minute, dt4.minute); 598 Expect.equals(dt1.minute, dt4.minute);
418 Expect.equals(dt1.second, dt4.second); 599 Expect.equals(dt1.second, dt4.second);
419 Expect.equals(dt1.millisecond, dt4.millisecond); 600 Expect.equals(dt1.millisecond, dt4.millisecond);
601 Expect.equals(dt1.microsecond, dt4.microsecond);
420 } 602 }
421 603
422 void testSubAdd() { 604 void testSubAdd() {
423 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true); 605 var dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true);
424 var dt2 = dt1.add(new Duration(milliseconds: 606 var dt2 = dt1.add(new Duration(milliseconds:
425 3 * Duration.MILLISECONDS_PER_SECOND + 5)); 607 3 * Duration.MILLISECONDS_PER_SECOND + 5));
426 Expect.equals(dt1.year, dt2.year); 608 Expect.equals(dt1.year, dt2.year);
427 Expect.equals(dt1.month, dt2.month); 609 Expect.equals(dt1.month, dt2.month);
428 Expect.equals(dt1.day, dt2.day); 610 Expect.equals(dt1.day, dt2.day);
429 Expect.equals(dt1.hour, dt2.hour); 611 Expect.equals(dt1.hour, dt2.hour);
430 Expect.equals(dt1.minute, dt2.minute); 612 Expect.equals(dt1.minute, dt2.minute);
431 Expect.equals(dt1.second + 3, dt2.second); 613 Expect.equals(dt1.second + 3, dt2.second);
432 Expect.equals(dt1.millisecond + 5, dt2.millisecond); 614 Expect.equals(dt1.millisecond + 5, dt2.millisecond);
615 Expect.equals(dt1.microsecond, dt2.microsecond);
433 var dt3 = dt2.subtract(new Duration(milliseconds: 616 var dt3 = dt2.subtract(new Duration(milliseconds:
434 3 * Duration.MILLISECONDS_PER_SECOND + 5)); 617 3 * Duration.MILLISECONDS_PER_SECOND + 5));
435 Expect.equals(true, dt1 == dt3); 618 Expect.equals(true, dt1 == dt3);
436 Expect.equals(false, dt1 == dt2); 619 Expect.equals(false, dt1 == dt2);
620
621 if (!supportsMicroseconds) return;
622
623 dt1 = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true);
624 dt2 = dt1.add(new Duration(microseconds:
625 3 * Duration.MICROSECONDS_PER_SECOND + 5));
626 Expect.equals(dt1.year, dt2.year);
627 Expect.equals(dt1.month, dt2.month);
628 Expect.equals(dt1.day, dt2.day);
629 Expect.equals(dt1.hour, dt2.hour);
630 Expect.equals(dt1.minute, dt2.minute);
631 Expect.equals(dt1.second + 3, dt2.second);
632 Expect.equals(dt1.millisecond, dt2.millisecond);
633 Expect.equals(dt1.microsecond + 5, dt2.microsecond);
634 dt3 = dt2.subtract(new Duration(microseconds:
635 3 * Duration.MICROSECONDS_PER_SECOND + 5));
636 Expect.equals(true, dt1 == dt3);
637 Expect.equals(false, dt1 == dt2);
437 } 638 }
438 639
439 void testUnderflowAndOverflow() { 640 void testUnderflowAndOverflow() {
440 final dtBase = new DateTime(2012, 6, 20, 12, 30, 30, 500); 641 int microsecond = supportsMicroseconds ? 499 : 0;
642 final dtBase = new DateTime(2012, 6, 20, 12, 30, 30, 500, microsecond);
441 643
442 // Millisecond 644 // Millisecond
443 print(" >>> Millisecond+"); 645 print(" >>> Millisecond+");
444 var dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, 646 var dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
445 dtBase.minute, dtBase.second, 1000); 647 dtBase.minute, dtBase.second, 1000, dtBase.microsecond);
446 Expect.equals(dtBase.year, dt.year); 648 Expect.equals(dtBase.year, dt.year);
447 Expect.equals(dtBase.month, dt.month); 649 Expect.equals(dtBase.month, dt.month);
448 Expect.equals(dtBase.day, dt.day); 650 Expect.equals(dtBase.day, dt.day);
449 Expect.equals(dtBase.hour, dt.hour); 651 Expect.equals(dtBase.hour, dt.hour);
450 Expect.equals(dtBase.minute, dt.minute); 652 Expect.equals(dtBase.minute, dt.minute);
451 Expect.equals(dtBase.second + 1, dt.second); 653 Expect.equals(dtBase.second + 1, dt.second);
452 Expect.equals(0, dt.millisecond); 654 Expect.equals(0, dt.millisecond);
655 Expect.equals(dtBase.microsecond, dt.microsecond);
453 656
454 print(" >>> Millisecond-"); 657 print(" >>> Millisecond-");
455 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, 658 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
456 dtBase.minute, dtBase.second, -1000); 659 dtBase.minute, dtBase.second, -1000, dtBase.microsecond);
457 Expect.equals(dtBase.year, dt.year); 660 Expect.equals(dtBase.year, dt.year);
458 Expect.equals(dtBase.month, dt.month); 661 Expect.equals(dtBase.month, dt.month);
459 Expect.equals(dtBase.day, dt.day); 662 Expect.equals(dtBase.day, dt.day);
460 Expect.equals(dtBase.hour, dt.hour); 663 Expect.equals(dtBase.hour, dt.hour);
461 Expect.equals(dtBase.minute, dt.minute); 664 Expect.equals(dtBase.minute, dt.minute);
462 Expect.equals(dtBase.second - 1, dt.second); 665 Expect.equals(dtBase.second - 1, dt.second);
463 Expect.equals(0, dt.millisecond); 666 Expect.equals(0, dt.millisecond);
667 Expect.equals(dtBase.microsecond, dt.microsecond);
464 668
465 // Second 669 // Second
466 print(" >>> Second+"); 670 print(" >>> Second+");
467 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, 671 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
468 dtBase.minute, 60, dtBase.millisecond); 672 dtBase.minute, 60, dtBase.millisecond, dtBase.microsecond);
469 Expect.equals(dtBase.year, dt.year); 673 Expect.equals(dtBase.year, dt.year);
470 Expect.equals(dtBase.month, dt.month); 674 Expect.equals(dtBase.month, dt.month);
471 Expect.equals(dtBase.day, dt.day); 675 Expect.equals(dtBase.day, dt.day);
472 Expect.equals(dtBase.hour, dt.hour); 676 Expect.equals(dtBase.hour, dt.hour);
473 Expect.equals(dtBase.minute + 1, dt.minute); 677 Expect.equals(dtBase.minute + 1, dt.minute);
474 Expect.equals(0, dt.second); 678 Expect.equals(0, dt.second);
475 Expect.equals(dtBase.millisecond, dt.millisecond); 679 Expect.equals(dtBase.millisecond, dt.millisecond);
680 Expect.equals(dtBase.microsecond, dt.microsecond);
476 681
477 print(" >>> Second-"); 682 print(" >>> Second-");
478 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, 683 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
479 dtBase.minute, -60, dtBase.millisecond); 684 dtBase.minute, -60, dtBase.millisecond, dtBase.microsecond);
480 Expect.equals(dtBase.year, dt.year); 685 Expect.equals(dtBase.year, dt.year);
481 Expect.equals(dtBase.month, dt.month); 686 Expect.equals(dtBase.month, dt.month);
482 Expect.equals(dtBase.day, dt.day); 687 Expect.equals(dtBase.day, dt.day);
483 Expect.equals(dtBase.hour, dt.hour); 688 Expect.equals(dtBase.hour, dt.hour);
484 Expect.equals(dtBase.minute - 1, dt.minute); 689 Expect.equals(dtBase.minute - 1, dt.minute);
485 Expect.equals(0, dt.second); 690 Expect.equals(0, dt.second);
486 Expect.equals(dtBase.millisecond, dt.millisecond); 691 Expect.equals(dtBase.millisecond, dt.millisecond);
692 Expect.equals(dtBase.microsecond, dt.microsecond);
487 693
488 // Minute 694 // Minute
489 print(" >>> Minute+"); 695 print(" >>> Minute+");
490 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, 60, 696 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, 60,
491 dtBase.second, dtBase.millisecond); 697 dtBase.second, dtBase.millisecond, dtBase.microsecond);
492 Expect.equals(dtBase.year, dt.year); 698 Expect.equals(dtBase.year, dt.year);
493 Expect.equals(dtBase.month, dt.month); 699 Expect.equals(dtBase.month, dt.month);
494 Expect.equals(dtBase.day, dt.day); 700 Expect.equals(dtBase.day, dt.day);
495 Expect.equals(dtBase.hour + 1, dt.hour); 701 Expect.equals(dtBase.hour + 1, dt.hour);
496 Expect.equals(0, dt.minute); 702 Expect.equals(0, dt.minute);
497 Expect.equals(dtBase.second, dt.second); 703 Expect.equals(dtBase.second, dt.second);
498 Expect.equals(dtBase.millisecond, dt.millisecond); 704 Expect.equals(dtBase.millisecond, dt.millisecond);
705 Expect.equals(dtBase.microsecond, dt.microsecond);
499 706
500 print(" >>> Minute-"); 707 print(" >>> Minute-");
501 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, -60, 708 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, -60,
502 dtBase.second, dtBase.millisecond); 709 dtBase.second, dtBase.millisecond, dtBase.microsecond);
503 Expect.equals(dtBase.year, dt.year); 710 Expect.equals(dtBase.year, dt.year);
504 Expect.equals(dtBase.month, dt.month); 711 Expect.equals(dtBase.month, dt.month);
505 Expect.equals(dtBase.day, dt.day); 712 Expect.equals(dtBase.day, dt.day);
506 Expect.equals(dtBase.hour - 1, dt.hour); 713 Expect.equals(dtBase.hour - 1, dt.hour);
507 Expect.equals(0, dt.minute); 714 Expect.equals(0, dt.minute);
508 Expect.equals(dtBase.second, dt.second); 715 Expect.equals(dtBase.second, dt.second);
509 Expect.equals(dtBase.millisecond, dt.millisecond); 716 Expect.equals(dtBase.millisecond, dt.millisecond);
717 Expect.equals(dtBase.microsecond, dt.microsecond);
510 718
511 // Hour 719 // Hour
512 print(" >>> Hour+"); 720 print(" >>> Hour+");
513 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, 24, dtBase.minute, 721 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, 24, dtBase.minute,
514 dtBase.second, dtBase.millisecond); 722 dtBase.second, dtBase.millisecond, dtBase.microsecond);
515 Expect.equals(dtBase.year, dt.year); 723 Expect.equals(dtBase.year, dt.year);
516 Expect.equals(dtBase.month, dt.month); 724 Expect.equals(dtBase.month, dt.month);
517 Expect.equals(dtBase.day + 1, dt.day); 725 Expect.equals(dtBase.day + 1, dt.day);
518 Expect.equals(0, dt.hour); 726 Expect.equals(0, dt.hour);
519 Expect.equals(dtBase.minute, dt.minute); 727 Expect.equals(dtBase.minute, dt.minute);
520 Expect.equals(dtBase.second, dt.second); 728 Expect.equals(dtBase.second, dt.second);
521 Expect.equals(dtBase.millisecond, dt.millisecond); 729 Expect.equals(dtBase.millisecond, dt.millisecond);
730 Expect.equals(dtBase.microsecond, dt.microsecond);
522 731
523 print(" >>> Hour-"); 732 print(" >>> Hour-");
524 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, -24, dtBase.minute, 733 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, -24, dtBase.minute,
525 dtBase.second, dtBase.millisecond); 734 dtBase.second, dtBase.millisecond, dtBase.microsecond);
526 Expect.equals(dtBase.year, dt.year); 735 Expect.equals(dtBase.year, dt.year);
527 Expect.equals(dtBase.month, dt.month); 736 Expect.equals(dtBase.month, dt.month);
528 Expect.equals(dtBase.day - 1, dt.day); 737 Expect.equals(dtBase.day - 1, dt.day);
529 Expect.equals(0, dt.hour); 738 Expect.equals(0, dt.hour);
530 Expect.equals(dtBase.minute, dt.minute); 739 Expect.equals(dtBase.minute, dt.minute);
531 Expect.equals(dtBase.second, dt.second); 740 Expect.equals(dtBase.second, dt.second);
532 Expect.equals(dtBase.millisecond, dt.millisecond); 741 Expect.equals(dtBase.millisecond, dt.millisecond);
742 Expect.equals(dtBase.microsecond, dt.microsecond);
533 743
534 // Day 744 // Day
535 print(" >>> Day+"); 745 print(" >>> Day+");
536 dt = new DateTime(dtBase.year, dtBase.month, 31, dtBase.hour, dtBase.minute, 746 dt = new DateTime(dtBase.year, dtBase.month, 31, dtBase.hour, dtBase.minute,
537 dtBase.second, dtBase.millisecond); 747 dtBase.second, dtBase.millisecond, dtBase.microsecond);
538 Expect.equals(dtBase.year, dt.year); 748 Expect.equals(dtBase.year, dt.year);
539 Expect.equals(dtBase.month + 1, dt.month); 749 Expect.equals(dtBase.month + 1, dt.month);
540 Expect.equals(1, dt.day); 750 Expect.equals(1, dt.day);
541 Expect.equals(dtBase.hour, dt.hour); 751 Expect.equals(dtBase.hour, dt.hour);
542 Expect.equals(dtBase.minute, dt.minute); 752 Expect.equals(dtBase.minute, dt.minute);
543 Expect.equals(dtBase.second, dt.second); 753 Expect.equals(dtBase.second, dt.second);
544 Expect.equals(dtBase.millisecond, dt.millisecond); 754 Expect.equals(dtBase.millisecond, dt.millisecond);
755 Expect.equals(dtBase.microsecond, dt.microsecond);
545 756
546 print(" >>> Day-"); 757 print(" >>> Day-");
547 dt = new DateTime(dtBase.year, dtBase.month, -30, dtBase.hour, dtBase.minute, 758 dt = new DateTime(dtBase.year, dtBase.month, -30, dtBase.hour, dtBase.minute,
548 dtBase.second, dtBase.millisecond); 759 dtBase.second, dtBase.millisecond, dtBase.microsecond);
549 Expect.equals(dtBase.year, dt.year); 760 Expect.equals(dtBase.year, dt.year);
550 Expect.equals(dtBase.month - 1, dt.month); 761 Expect.equals(dtBase.month - 1, dt.month);
551 Expect.equals(1, dt.day); 762 Expect.equals(1, dt.day);
552 Expect.equals(dtBase.hour, dt.hour); 763 Expect.equals(dtBase.hour, dt.hour);
553 Expect.equals(dtBase.minute, dt.minute); 764 Expect.equals(dtBase.minute, dt.minute);
554 Expect.equals(dtBase.second, dt.second); 765 Expect.equals(dtBase.second, dt.second);
555 Expect.equals(dtBase.millisecond, dt.millisecond); 766 Expect.equals(dtBase.millisecond, dt.millisecond);
767 Expect.equals(dtBase.microsecond, dt.microsecond);
556 768
557 // Month 769 // Month
558 print(" >>> Month+"); 770 print(" >>> Month+");
559 dt = new DateTime(dtBase.year, 13, dtBase.day, dtBase.hour, dtBase.minute, 771 dt = new DateTime(dtBase.year, 13, dtBase.day, dtBase.hour, dtBase.minute,
560 dtBase.second, dtBase.millisecond); 772 dtBase.second, dtBase.millisecond, dtBase.microsecond);
561 Expect.equals(dtBase.year + 1, dt.year); 773 Expect.equals(dtBase.year + 1, dt.year);
562 Expect.equals(1, dt.month); 774 Expect.equals(1, dt.month);
563 Expect.equals(dtBase.day, dt.day); 775 Expect.equals(dtBase.day, dt.day);
564 Expect.equals(dtBase.hour, dt.hour); 776 Expect.equals(dtBase.hour, dt.hour);
565 Expect.equals(dtBase.minute, dt.minute); 777 Expect.equals(dtBase.minute, dt.minute);
566 Expect.equals(dtBase.second, dt.second); 778 Expect.equals(dtBase.second, dt.second);
567 Expect.equals(dtBase.millisecond, dt.millisecond); 779 Expect.equals(dtBase.millisecond, dt.millisecond);
780 Expect.equals(dtBase.microsecond, dt.microsecond);
568 781
569 print(" >>> Month-"); 782 print(" >>> Month-");
570 dt = new DateTime(dtBase.year, -11, dtBase.day, dtBase.hour, dtBase.minute, 783 dt = new DateTime(dtBase.year, -11, dtBase.day, dtBase.hour, dtBase.minute,
571 dtBase.second, dtBase.millisecond); 784 dtBase.second, dtBase.millisecond, dtBase.microsecond);
572 Expect.equals(dtBase.year - 1, dt.year); 785 Expect.equals(dtBase.year - 1, dt.year);
573 Expect.equals(1, dt.month); 786 Expect.equals(1, dt.month);
574 Expect.equals(dtBase.day, dt.day); 787 Expect.equals(dtBase.day, dt.day);
575 Expect.equals(dtBase.hour, dt.hour); 788 Expect.equals(dtBase.hour, dt.hour);
576 Expect.equals(dtBase.minute, dt.minute); 789 Expect.equals(dtBase.minute, dt.minute);
577 Expect.equals(dtBase.second, dt.second); 790 Expect.equals(dtBase.second, dt.second);
578 Expect.equals(dtBase.millisecond, dt.millisecond); 791 Expect.equals(dtBase.millisecond, dt.millisecond);
792 Expect.equals(dtBase.microsecond, dt.microsecond);
579 793
580 // Flowing all the way up the chain. 794 // Flowing all the way up the chain.
581 print(" >>> Flow+"); 795 print(" >>> Flow+");
582 var dtBase1 = new DateTime(2012, 12, 31, 23, 59, 59, 999); 796 var dtBase1 = new DateTime(2012, 12, 31, 23, 59, 59, 999, 000);
583 var dtTick = new DateTime(dtBase1.year, dtBase1.month, dtBase1.day, 797 var dtTick = new DateTime(dtBase1.year, dtBase1.month, dtBase1.day,
584 dtBase1.hour, dtBase1.minute, dtBase1.second, 798 dtBase1.hour, dtBase1.minute, dtBase1.second,
585 dtBase1.millisecond + 1); 799 dtBase1.millisecond + 1, dtBase1.microsecond);
586 Expect.equals(dtBase1.year + 1, dtTick.year); 800 Expect.equals(dtBase1.year + 1, dtTick.year);
587 Expect.equals(1, dtTick.month); 801 Expect.equals(1, dtTick.month);
588 Expect.equals(1, dtTick.day); 802 Expect.equals(1, dtTick.day);
589 Expect.equals(0, dtTick.hour); 803 Expect.equals(0, dtTick.hour);
590 Expect.equals(0, dtTick.minute); 804 Expect.equals(0, dtTick.minute);
591 Expect.equals(0, dtTick.second); 805 Expect.equals(0, dtTick.second);
592 Expect.equals(0, dtTick.millisecond); 806 Expect.equals(0, dtTick.millisecond);
807 Expect.equals(0, dtTick.microsecond);
593 808
594 print(" >>> Flow-"); 809 print(" >>> Flow-");
595 dtBase1 = new DateTime(2012, 1, 1, 0, 0, 0, 0); 810 dtBase1 = new DateTime(2012, 1, 1, 0, 0, 0, 0);
596 dtTick = new DateTime(dtBase1.year, dtBase1.month, dtBase1.day, dtBase1.hour, 811 dtTick = new DateTime(dtBase1.year, dtBase1.month, dtBase1.day, dtBase1.hour,
597 dtBase1.minute, dtBase1.second, dtBase1.millisecond - 1); 812 dtBase1.minute, dtBase1.second, dtBase1.millisecond - 1,
813 dtBase1.microsecond);
598 Expect.equals(dtBase1.year - 1, dtTick.year); 814 Expect.equals(dtBase1.year - 1, dtTick.year);
599 Expect.equals(12, dtTick.month); 815 Expect.equals(12, dtTick.month);
600 Expect.equals(31, dtTick.day); 816 Expect.equals(31, dtTick.day);
601 Expect.equals(23, dtTick.hour); 817 Expect.equals(23, dtTick.hour);
602 Expect.equals(59, dtTick.minute); 818 Expect.equals(59, dtTick.minute);
603 Expect.equals(59, dtTick.second); 819 Expect.equals(59, dtTick.second);
604 Expect.equals(999, dtTick.millisecond); 820 Expect.equals(999, dtTick.millisecond);
821 Expect.equals(0, dtTick.microsecond);
605 822
606 print(" >>> extra underflow"); 823 print(" >>> extra underflow");
607 dtTick = new DateTime(dtBase1.year, dtBase1.month, dtBase1.day, -17520, 824 dtTick = new DateTime(dtBase1.year, dtBase1.month, dtBase1.day, -17520,
608 dtBase1.minute, dtBase1.second, dtBase1.millisecond); 825 dtBase1.minute, dtBase1.second, dtBase1.millisecond,
826 dtBase1.microsecond);
609 Expect.equals(dtBase1.year - 2, dtTick.year); 827 Expect.equals(dtBase1.year - 2, dtTick.year);
610 Expect.equals(dtBase1.month, dtTick.month); 828 Expect.equals(dtBase1.month, dtTick.month);
611 Expect.equals(dtBase1.day, dtTick.day); 829 Expect.equals(dtBase1.day, dtTick.day);
612 Expect.equals(dtBase1.hour, dtTick.hour); 830 Expect.equals(dtBase1.hour, dtTick.hour);
613 Expect.equals(dtBase1.minute, dtTick.minute); 831 Expect.equals(dtBase1.minute, dtTick.minute);
614 Expect.equals(dtBase1.second, dtTick.second); 832 Expect.equals(dtBase1.second, dtTick.second);
615 Expect.equals(dtBase1.millisecond, dtTick.millisecond); 833 Expect.equals(dtBase1.millisecond, dtTick.millisecond);
834 Expect.equals(dtBase1.microsecond, dtTick.microsecond);
835
836 if (!supportsMicroseconds) return;
837
838 // Microsecond
839 print(" >>> Microsecond+");
840 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
841 dtBase.minute, dtBase.second, dtBase.millisecond, 1000);
842 Expect.equals(dtBase.year, dt.year);
843 Expect.equals(dtBase.month, dt.month);
844 Expect.equals(dtBase.day, dt.day);
845 Expect.equals(dtBase.hour, dt.hour);
846 Expect.equals(dtBase.minute, dt.minute);
847 Expect.equals(dtBase.second, dt.second);
848 Expect.equals(dtBase.millisecond + 1, dt.millisecond);
849 Expect.equals(0, dt.microsecond);
850
851 print(" >>> Microsecond-");
852 dt = new DateTime(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
853 dtBase.minute, dtBase.second, dtBase.millisecond, -1000);
854 Expect.equals(dtBase.year, dt.year);
855 Expect.equals(dtBase.month, dt.month);
856 Expect.equals(dtBase.day, dt.day);
857 Expect.equals(dtBase.hour, dt.hour);
858 Expect.equals(dtBase.minute, dt.minute);
859 Expect.equals(dtBase.second, dt.second);
860 Expect.equals(dtBase.millisecond - 1, dt.millisecond);
861 Expect.equals(0, dt.microsecond);
862
863 // Flowing all the way up the chain.
864 print(" >>> Flow+ 2");
865 dtBase1 = new DateTime(2012, 12, 31, 23, 59, 59, 999, 999);
866 dtTick = new DateTime(dtBase1.year, dtBase1.month, dtBase1.day,
867 dtBase1.hour, dtBase1.minute, dtBase1.second,
868 dtBase1.millisecond, dtBase1.microsecond + 1);
869 Expect.equals(dtBase1.year + 1, dtTick.year);
870 Expect.equals(1, dtTick.month);
871 Expect.equals(1, dtTick.day);
872 Expect.equals(0, dtTick.hour);
873 Expect.equals(0, dtTick.minute);
874 Expect.equals(0, dtTick.second);
875 Expect.equals(0, dtTick.millisecond);
876 Expect.equals(0, dtTick.microsecond);
877
878 print(" >>> Flow- 2");
879 dtBase1 = new DateTime(2012, 1, 1, 0, 0, 0, 0, 0);
880 dtTick = new DateTime(dtBase1.year, dtBase1.month, dtBase1.day, dtBase1.hour,
881 dtBase1.minute, dtBase1.second, dtBase1.millisecond,
882 dtBase1.microsecond - 1);
883 Expect.equals(dtBase1.year - 1, dtTick.year);
884 Expect.equals(12, dtTick.month);
885 Expect.equals(31, dtTick.day);
886 Expect.equals(23, dtTick.hour);
887 Expect.equals(59, dtTick.minute);
888 Expect.equals(59, dtTick.second);
889 Expect.equals(999, dtTick.millisecond);
890 Expect.equals(999, dtTick.microsecond);
616 } 891 }
617 892
618 void testDateStrings() { 893 void testDateStrings() {
619 // TODO(floitsch): Clean up the DateTime API that deals with strings. 894 // TODO(floitsch): Clean up the DateTime API that deals with strings.
620 var dt1 = DateTime.parse("2011-05-11 18:58:35Z"); 895 var dt1 = DateTime.parse("2011-05-11 18:58:35Z");
621 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 896 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
622 Expect.isTrue(dt1.isUtc); 897 Expect.isTrue(dt1.isUtc);
623 dt1 = DateTime.parse("20110511 18:58:35z"); 898 dt1 = DateTime.parse("20110511 18:58:35z");
624 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 899 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
625 Expect.isTrue(dt1.isUtc); 900 Expect.isTrue(dt1.isUtc);
626 dt1 = DateTime.parse("+20110511 18:58:35z"); 901 dt1 = DateTime.parse("+20110511 18:58:35z");
627 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 902 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
628 Expect.isTrue(dt1.isUtc); 903 Expect.isTrue(dt1.isUtc);
629 var str = dt1.toString(); 904 var str = dt1.toString();
630 var dt2 = DateTime.parse(str); 905 var dt2 = DateTime.parse(str);
631 Expect.equals(true, dt1 == dt2); 906 Expect.equals(true, dt1 == dt2);
632 var dt3 = dt1.toUtc(); 907 var dt3 = dt1.toUtc();
633 str = dt3.toString(); 908 str = dt3.toString();
634 Expect.equals("2011-05-11 18:58:35.000Z", str); 909 Expect.equals("2011-05-11 18:58:35.000Z", str);
635 var dt4 = DateTime.parse("-1234-01-01 00:00:00Z"); 910 var dt4 = DateTime.parse("-1234-01-01 00:00:00Z");
636 Expect.equals(-1234, dt4.year); 911 Expect.equals(-1234, dt4.year);
637 Expect.equals(1, dt4.month); 912 Expect.equals(1, dt4.month);
638 Expect.equals(1, dt4.day); 913 Expect.equals(1, dt4.day);
639 Expect.equals(0, dt4.hour); 914 Expect.equals(0, dt4.hour);
640 Expect.equals(0, dt4.minute); 915 Expect.equals(0, dt4.minute);
641 Expect.equals(0, dt4.second); 916 Expect.equals(0, dt4.second);
642 Expect.equals(0, dt4.millisecond); 917 Expect.equals(0, dt4.millisecond);
918 Expect.equals(0, dt4.microsecond);
643 Expect.isTrue(dt4.isUtc); 919 Expect.isTrue(dt4.isUtc);
644 var dt5 = DateTime.parse("0099-01-02"); 920 var dt5 = DateTime.parse("0099-01-02");
645 Expect.equals(99, dt5.year); 921 Expect.equals(99, dt5.year);
646 Expect.equals(1, dt5.month); 922 Expect.equals(1, dt5.month);
647 Expect.equals(2, dt5.day); 923 Expect.equals(2, dt5.day);
648 Expect.equals(0, dt5.hour); 924 Expect.equals(0, dt5.hour);
649 Expect.equals(0, dt5.minute); 925 Expect.equals(0, dt5.minute);
650 Expect.equals(0, dt5.second); 926 Expect.equals(0, dt5.second);
651 Expect.equals(0, dt5.millisecond); 927 Expect.equals(0, dt5.millisecond);
928 Expect.equals(0, dt5.microsecond);
652 Expect.isFalse(dt5.isUtc); 929 Expect.isFalse(dt5.isUtc);
653 var dt6 = DateTime.parse("2012-01-01 00:00:10.012"); 930 var dt6 = DateTime.parse("2012-01-01 00:00:10.012");
654 Expect.equals(12, dt6.millisecond); 931 Expect.equals(12, dt6.millisecond);
932 Expect.equals(0, dt6.microsecond);
655 dt6 = DateTime.parse("2012-01-01 00:00:10.003"); 933 dt6 = DateTime.parse("2012-01-01 00:00:10.003");
656 Expect.equals(3, dt6.millisecond); 934 Expect.equals(3, dt6.millisecond);
935 Expect.equals(0, dt6.microsecond);
657 dt6 = DateTime.parse("2012-01-01 00:00:10.5"); 936 dt6 = DateTime.parse("2012-01-01 00:00:10.5");
658 Expect.equals(500, dt6.millisecond); 937 Expect.equals(500, dt6.millisecond);
938 Expect.equals(0, dt6.microsecond);
659 dt6 = DateTime.parse("2012-01-01 00:00:10.003Z"); 939 dt6 = DateTime.parse("2012-01-01 00:00:10.003Z");
660 Expect.equals(3, dt6.millisecond); 940 Expect.equals(3, dt6.millisecond);
941 Expect.equals(0, dt6.microsecond);
661 dt6 = DateTime.parse("2012-01-01 00:00:10.5z"); 942 dt6 = DateTime.parse("2012-01-01 00:00:10.5z");
662 Expect.equals(500, dt6.millisecond); 943 Expect.equals(500, dt6.millisecond);
944 Expect.equals(0, dt6.microsecond);
663 var dt7 = DateTime.parse("2011-05-11T18:58:35Z"); 945 var dt7 = DateTime.parse("2011-05-11T18:58:35Z");
664 Expect.equals(1305140315000, dt7.millisecondsSinceEpoch); 946 Expect.equals(1305140315000, dt7.millisecondsSinceEpoch);
665 var dt8 = DateTime.parse("-1234-01-01T00:00:00Z"); 947 var dt8 = DateTime.parse("-1234-01-01T00:00:00Z");
666 Expect.equals(-1234, dt8.year); 948 Expect.equals(-1234, dt8.year);
667 Expect.equals(1, dt8.month); 949 Expect.equals(1, dt8.month);
668 Expect.equals(1, dt8.day); 950 Expect.equals(1, dt8.day);
669 Expect.equals(0, dt8.hour); 951 Expect.equals(0, dt8.hour);
670 Expect.equals(0, dt8.minute); 952 Expect.equals(0, dt8.minute);
671 Expect.equals(0, dt8.second); 953 Expect.equals(0, dt8.second);
672 Expect.equals(0, dt8.millisecond); 954 Expect.equals(0, dt8.millisecond);
955 Expect.equals(0, dt8.microsecond);
673 Expect.isTrue(dt8.isUtc); 956 Expect.isTrue(dt8.isUtc);
674 var dt9 = DateTime.parse("-1234-01-01T00:00:00"); 957 var dt9 = DateTime.parse("-1234-01-01T00:00:00");
675 Expect.equals(-1234, dt9.year); 958 Expect.equals(-1234, dt9.year);
676 Expect.equals(1, dt9.month); 959 Expect.equals(1, dt9.month);
677 Expect.equals(1, dt9.day); 960 Expect.equals(1, dt9.day);
678 Expect.equals(0, dt9.hour); 961 Expect.equals(0, dt9.hour);
679 Expect.equals(0, dt9.minute); 962 Expect.equals(0, dt9.minute);
680 Expect.equals(0, dt9.second); 963 Expect.equals(0, dt9.second);
681 Expect.equals(0, dt9.millisecond); 964 Expect.equals(0, dt9.millisecond);
965 Expect.equals(0, dt9.microsecond);
682 Expect.isFalse(dt9.isUtc); 966 Expect.isFalse(dt9.isUtc);
683 var dt10 = DateTime.parse("-12340101"); 967 var dt10 = DateTime.parse("-12340101");
684 Expect.equals(-1234, dt10.year); 968 Expect.equals(-1234, dt10.year);
685 Expect.equals(1, dt10.month); 969 Expect.equals(1, dt10.month);
686 Expect.equals(1, dt10.day); 970 Expect.equals(1, dt10.day);
687 Expect.equals(0, dt10.hour); 971 Expect.equals(0, dt10.hour);
688 Expect.equals(0, dt10.minute); 972 Expect.equals(0, dt10.minute);
689 Expect.equals(0, dt10.second); 973 Expect.equals(0, dt10.second);
690 Expect.equals(0, dt10.millisecond); 974 Expect.equals(0, dt10.millisecond);
975 Expect.equals(0, dt10.microsecond);
691 Expect.isFalse(dt10.isUtc); 976 Expect.isFalse(dt10.isUtc);
692 dt1 = DateTime.parse("2012-02-27 13:27:00"); 977 dt1 = DateTime.parse("2012-02-27 13:27:00");
693 Expect.equals(2012, dt1.year); 978 Expect.equals(2012, dt1.year);
694 Expect.equals(2, dt1.month); 979 Expect.equals(2, dt1.month);
695 Expect.equals(27, dt1.day); 980 Expect.equals(27, dt1.day);
696 Expect.equals(13, dt1.hour); 981 Expect.equals(13, dt1.hour);
697 Expect.equals(27, dt1.minute); 982 Expect.equals(27, dt1.minute);
698 Expect.equals(0, dt1.second); 983 Expect.equals(0, dt1.second);
699 Expect.equals(0, dt1.millisecond); 984 Expect.equals(0, dt1.millisecond);
985 Expect.equals(0, dt1.microsecond);
700 Expect.equals(false, dt1.isUtc); 986 Expect.equals(false, dt1.isUtc);
701 dt1 = DateTime.parse("2012-02-27 13:27:00.423z"); 987 dt1 = DateTime.parse("2012-02-27 13:27:00.423z");
702 Expect.equals(2012, dt1.year); 988 Expect.equals(2012, dt1.year);
703 Expect.equals(2, dt1.month); 989 Expect.equals(2, dt1.month);
704 Expect.equals(27, dt1.day); 990 Expect.equals(27, dt1.day);
705 Expect.equals(13, dt1.hour); 991 Expect.equals(13, dt1.hour);
706 Expect.equals(27, dt1.minute); 992 Expect.equals(27, dt1.minute);
707 Expect.equals(0, dt1.second); 993 Expect.equals(0, dt1.second);
708 Expect.equals(423, dt1.millisecond); 994 Expect.equals(423, dt1.millisecond);
995 Expect.equals(0, dt1.microsecond);
709 Expect.equals(true, dt1.isUtc); 996 Expect.equals(true, dt1.isUtc);
710 dt1 = DateTime.parse("20120227 13:27:00"); 997 dt1 = DateTime.parse("20120227 13:27:00");
711 Expect.equals(2012, dt1.year); 998 Expect.equals(2012, dt1.year);
712 Expect.equals(2, dt1.month); 999 Expect.equals(2, dt1.month);
713 Expect.equals(27, dt1.day); 1000 Expect.equals(27, dt1.day);
714 Expect.equals(13, dt1.hour); 1001 Expect.equals(13, dt1.hour);
715 Expect.equals(27, dt1.minute); 1002 Expect.equals(27, dt1.minute);
716 Expect.equals(0, dt1.second); 1003 Expect.equals(0, dt1.second);
717 Expect.equals(0, dt1.millisecond); 1004 Expect.equals(0, dt1.millisecond);
1005 Expect.equals(0, dt1.microsecond);
718 Expect.equals(false, dt1.isUtc); 1006 Expect.equals(false, dt1.isUtc);
719 dt1 = DateTime.parse("20120227T132700"); 1007 dt1 = DateTime.parse("20120227T132700");
720 Expect.equals(2012, dt1.year); 1008 Expect.equals(2012, dt1.year);
721 Expect.equals(2, dt1.month); 1009 Expect.equals(2, dt1.month);
722 Expect.equals(27, dt1.day); 1010 Expect.equals(27, dt1.day);
723 Expect.equals(13, dt1.hour); 1011 Expect.equals(13, dt1.hour);
724 Expect.equals(27, dt1.minute); 1012 Expect.equals(27, dt1.minute);
725 Expect.equals(0, dt1.second); 1013 Expect.equals(0, dt1.second);
726 Expect.equals(0, dt1.millisecond); 1014 Expect.equals(0, dt1.millisecond);
1015 Expect.equals(0, dt1.microsecond);
727 Expect.equals(false, dt1.isUtc); 1016 Expect.equals(false, dt1.isUtc);
728 dt1 = DateTime.parse("20120227"); 1017 dt1 = DateTime.parse("20120227");
729 Expect.equals(2012, dt1.year); 1018 Expect.equals(2012, dt1.year);
730 Expect.equals(2, dt1.month); 1019 Expect.equals(2, dt1.month);
731 Expect.equals(27, dt1.day); 1020 Expect.equals(27, dt1.day);
732 Expect.equals(0, dt1.hour); 1021 Expect.equals(0, dt1.hour);
733 Expect.equals(0, dt1.minute); 1022 Expect.equals(0, dt1.minute);
734 Expect.equals(0, dt1.second); 1023 Expect.equals(0, dt1.second);
735 Expect.equals(0, dt1.millisecond); 1024 Expect.equals(0, dt1.millisecond);
1025 Expect.equals(0, dt1.microsecond);
736 Expect.equals(false, dt1.isUtc); 1026 Expect.equals(false, dt1.isUtc);
737 dt1 = DateTime.parse("2012-02-27T14Z"); 1027 dt1 = DateTime.parse("2012-02-27T14Z");
738 Expect.equals(2012, dt1.year); 1028 Expect.equals(2012, dt1.year);
739 Expect.equals(2, dt1.month); 1029 Expect.equals(2, dt1.month);
740 Expect.equals(27, dt1.day); 1030 Expect.equals(27, dt1.day);
741 Expect.equals(14, dt1.hour); 1031 Expect.equals(14, dt1.hour);
742 Expect.equals(0, dt1.minute); 1032 Expect.equals(0, dt1.minute);
743 Expect.equals(0, dt1.second); 1033 Expect.equals(0, dt1.second);
744 Expect.equals(0, dt1.millisecond); 1034 Expect.equals(0, dt1.millisecond);
1035 Expect.equals(0, dt1.microsecond);
745 Expect.equals(true, dt1.isUtc); 1036 Expect.equals(true, dt1.isUtc);
746 dt1 = DateTime.parse("-123450101 00:00:00 Z"); 1037 dt1 = DateTime.parse("-123450101 00:00:00 Z");
747 Expect.equals(-12345, dt1.year); 1038 Expect.equals(-12345, dt1.year);
748 Expect.equals(1, dt1.month); 1039 Expect.equals(1, dt1.month);
749 Expect.equals(1, dt1.day); 1040 Expect.equals(1, dt1.day);
750 Expect.equals(0, dt1.hour); 1041 Expect.equals(0, dt1.hour);
751 Expect.equals(0, dt1.minute); 1042 Expect.equals(0, dt1.minute);
752 Expect.equals(0, dt1.second); 1043 Expect.equals(0, dt1.second);
753 Expect.equals(0, dt1.millisecond); 1044 Expect.equals(0, dt1.millisecond);
1045 Expect.equals(0, dt1.microsecond);
754 Expect.equals(true, dt1.isUtc); 1046 Expect.equals(true, dt1.isUtc);
755 // We only support millisecond. If the user supplies more data (the "51"
756 // here), we round.
757 // If (eventually) we support more than just millisecond this test could
758 // fail. Please update the test in this case.
759 dt1 = DateTime.parse("1999-01-02 23:59:59.99951"); 1047 dt1 = DateTime.parse("1999-01-02 23:59:59.99951");
760 Expect.equals(1999, dt1.year); 1048 if (supportsMicroseconds) {
761 Expect.equals(1, dt1.month); 1049 Expect.equals(1999, dt1.year);
762 Expect.equals(3, dt1.day); 1050 Expect.equals(1, dt1.month);
763 Expect.equals(0, dt1.hour); 1051 Expect.equals(2, dt1.day);
764 Expect.equals(0, dt1.minute); 1052 Expect.equals(23, dt1.hour);
765 Expect.equals(0, dt1.second); 1053 Expect.equals(59, dt1.minute);
766 Expect.equals(0, dt1.millisecond); 1054 Expect.equals(59, dt1.second);
1055 Expect.equals(999, dt1.millisecond);
1056 Expect.equals(510, dt1.microsecond);
1057 } else {
1058 // We only support millisecond. If the user supplies more data (the "51"
1059 // here), we round.
1060 Expect.equals(1999, dt1.year);
1061 Expect.equals(1, dt1.month);
1062 Expect.equals(3, dt1.day);
1063 Expect.equals(0, dt1.hour);
1064 Expect.equals(0, dt1.minute);
1065 Expect.equals(0, dt1.second);
1066 Expect.equals(0, dt1.millisecond);
1067 Expect.equals(0, dt1.microsecond);
1068 }
767 Expect.equals(false, dt1.isUtc); 1069 Expect.equals(false, dt1.isUtc);
768 dt1 = DateTime.parse("1999-01-02 23:58:59.99951Z"); 1070 dt1 = DateTime.parse("1999-01-02 23:58:59.99951Z");
769 Expect.equals(1999, dt1.year); 1071 if (supportsMicroseconds) {
770 Expect.equals(1, dt1.month); 1072 Expect.equals(1999, dt1.year);
771 Expect.equals(2, dt1.day); 1073 Expect.equals(1, dt1.month);
772 Expect.equals(23, dt1.hour); 1074 Expect.equals(2, dt1.day);
773 Expect.equals(59, dt1.minute); 1075 Expect.equals(23, dt1.hour);
774 Expect.equals(0, dt1.second); 1076 Expect.equals(58, dt1.minute);
775 Expect.equals(0, dt1.millisecond); 1077 Expect.equals(59, dt1.second);
1078 Expect.equals(999, dt1.millisecond);
1079 Expect.equals(510, dt1.microsecond);
1080 } else {
1081 Expect.equals(1999, dt1.year);
1082 Expect.equals(1, dt1.month);
1083 Expect.equals(2, dt1.day);
1084 Expect.equals(23, dt1.hour);
1085 Expect.equals(59, dt1.minute);
1086 Expect.equals(0, dt1.second);
1087 Expect.equals(0, dt1.millisecond);
1088 Expect.equals(0, dt1.microsecond);
1089 }
776 Expect.equals(true, dt1.isUtc); 1090 Expect.equals(true, dt1.isUtc);
777 dt1 = DateTime.parse("0009-09-09 09:09:09.009Z"); 1091 dt1 = DateTime.parse("0009-09-09 09:09:09.009Z");
778 Expect.equals(9, dt1.year); 1092 Expect.equals(9, dt1.year);
779 Expect.equals(9, dt1.month); 1093 Expect.equals(9, dt1.month);
780 Expect.equals(9, dt1.day); 1094 Expect.equals(9, dt1.day);
781 Expect.equals(9, dt1.hour); 1095 Expect.equals(9, dt1.hour);
782 Expect.equals(9, dt1.minute); 1096 Expect.equals(9, dt1.minute);
783 Expect.equals(9, dt1.second); 1097 Expect.equals(9, dt1.second);
784 Expect.equals(9, dt1.millisecond); 1098 Expect.equals(9, dt1.millisecond);
785 Expect.equals(true, dt1.isUtc); 1099 Expect.equals(true, dt1.isUtc);
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 Expect.throws(() => DateTime.parse("bad"), (e) => e is FormatException); 1173 Expect.throws(() => DateTime.parse("bad"), (e) => e is FormatException);
860 var bad_year = 1970 + (_MAX_MILLISECONDS ~/ (1000*60*60*24*365.2425)) + 1; 1174 var bad_year = 1970 + (_MAX_MILLISECONDS ~/ (1000*60*60*24*365.2425)) + 1;
861 Expect.throws(() => DateTime.parse(bad_year.toString() + "-01-01"), 1175 Expect.throws(() => DateTime.parse(bad_year.toString() + "-01-01"),
862 (e) => e is FormatException); 1176 (e) => e is FormatException);
863 // The last valid time; should not throw. 1177 // The last valid time; should not throw.
864 dt1 = DateTime.parse("275760-09-13T00:00:00.000Z"); 1178 dt1 = DateTime.parse("275760-09-13T00:00:00.000Z");
865 Expect.throws(() => DateTime.parse("275760-09-14T00:00:00.000Z"), 1179 Expect.throws(() => DateTime.parse("275760-09-14T00:00:00.000Z"),
866 (e) => e is FormatException); 1180 (e) => e is FormatException);
867 Expect.throws(() => DateTime.parse("275760-09-13T00:00:00.001Z"), 1181 Expect.throws(() => DateTime.parse("275760-09-13T00:00:00.001Z"),
868 (e) => e is FormatException); 1182 (e) => e is FormatException);
1183 if (supportsMicroseconds) {
1184 Expect.throws(() => DateTime.parse("275760-09-13T00:00:00.000001Z"),
1185 (e) => e is FormatException);
1186 } else {
1187 dt1 = DateTime.parse("275760-09-13T00:00:00.000001Z");
1188 }
869 1189
870 // first valid time; should not throw. 1190 // first valid time; should not throw.
871 dt1 = DateTime.parse("-271821-04-20T00:00:00.000Z"); 1191 dt1 = DateTime.parse("-271821-04-20T00:00:00.000Z");
872 Expect.throws(() => DateTime.parse("-271821-04-19T23:59:59.999Z"), 1192 Expect.throws(() => DateTime.parse("-271821-04-19T23:59:59.999Z"),
873 (e) => e is FormatException); 1193 (e) => e is FormatException);
1194
1195 if (supportsMicroseconds) {
1196 Expect.throws(() => DateTime.parse("-271821-04-19T23:59:59.999999Z"),
1197 (e) => e is FormatException);
1198 }
874 } 1199 }
875 1200
876 void testWeekday() { 1201 void testWeekday() {
877 // 2011-10-06 is Summertime. 1202 // 2011-10-06 is Summertime.
878 var d = new DateTime(2011, 10, 6, 0, 45, 37, 0); 1203 var d = new DateTime(2011, 10, 6, 0, 45, 37, 0);
879 Expect.equals(DateTime.THURSDAY, d.weekday); 1204 Expect.equals(DateTime.THURSDAY, d.weekday);
880 d = new DateTime.utc(2011, 10, 6, 0, 45, 37, 0); 1205 d = new DateTime.utc(2011, 10, 6, 0, 45, 37, 0);
881 Expect.equals(DateTime.THURSDAY, d.weekday); 1206 Expect.equals(DateTime.THURSDAY, d.weekday);
882 d = new DateTime(2011, 10, 5, 23, 45, 37, 0); 1207 d = new DateTime(2011, 10, 5, 23, 45, 37, 0);
883 Expect.equals(DateTime.WEDNESDAY, d.weekday); 1208 Expect.equals(DateTime.WEDNESDAY, d.weekday);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 Expect.equals(utcTime1, utcTime2); 1250 Expect.equals(utcTime1, utcTime2);
926 Expect.equals(source1, utcTime1.toString()); 1251 Expect.equals(source1, utcTime1.toString());
927 Expect.equals(source2, utcTime1.toIso8601String()); 1252 Expect.equals(source2, utcTime1.toIso8601String());
928 } 1253 }
929 } 1254 }
930 test("2000-01-01", "12:00:00.000"); 1255 test("2000-01-01", "12:00:00.000");
931 test("-2000-01-01", "12:00:00.000"); 1256 test("-2000-01-01", "12:00:00.000");
932 test("1970-01-01", "00:00:00.000"); 1257 test("1970-01-01", "00:00:00.000");
933 test("1969-12-31", "23:59:59.999"); 1258 test("1969-12-31", "23:59:59.999");
934 test("1969-09-09", "00:09:09.009"); 1259 test("1969-09-09", "00:09:09.009");
1260
1261 if (supportsMicroseconds) {
1262 test("2000-01-01", "12:00:00.000001");
1263 test("-2000-01-01", "12:00:00.000001");
1264 test("1970-01-01", "00:00:00.000001");
1265 test("1969-12-31", "23:59:59.999999");
1266 test("1969-09-09", "00:09:09.009999");
1267 }
935 } 1268 }
936 1269
937 void testIsoString() { 1270 void testIsoString() {
938 var d = new DateTime(9999, 1, 1, 23, 59, 59, 999); 1271 var d = new DateTime(9999, 1, 1, 23, 59, 59, 999);
939 Expect.equals("9999-01-01T23:59:59.999", d.toIso8601String()); 1272 Expect.equals("9999-01-01T23:59:59.999", d.toIso8601String());
940 d = new DateTime(-9999, 1, 1, 23, 59, 59, 999); 1273 d = new DateTime(-9999, 1, 1, 23, 59, 59, 999);
941 Expect.equals("-9999-01-01T23:59:59.999", d.toIso8601String()); 1274 Expect.equals("-9999-01-01T23:59:59.999", d.toIso8601String());
942 d = new DateTime.utc(9999, 1, 1, 23, 59, 59, 999); 1275 d = new DateTime.utc(9999, 1, 1, 23, 59, 59, 999);
943 Expect.equals("9999-01-01T23:59:59.999Z", d.toIso8601String()); 1276 Expect.equals("9999-01-01T23:59:59.999Z", d.toIso8601String());
944 d = new DateTime.utc(-9999, 1, 1, 23, 59, 59, 999); 1277 d = new DateTime.utc(-9999, 1, 1, 23, 59, 59, 999);
945 Expect.equals("-9999-01-01T23:59:59.999Z", d.toIso8601String()); 1278 Expect.equals("-9999-01-01T23:59:59.999Z", d.toIso8601String());
946 1279
947 d = new DateTime(10000, 1, 1, 23, 59, 59, 999); 1280 d = new DateTime(10000, 1, 1, 23, 59, 59, 999);
948 Expect.equals("+010000-01-01T23:59:59.999", d.toIso8601String()); 1281 Expect.equals("+010000-01-01T23:59:59.999", d.toIso8601String());
949 d = new DateTime(-10000, 1, 1, 23, 59, 59, 999); 1282 d = new DateTime(-10000, 1, 1, 23, 59, 59, 999);
950 Expect.equals("-010000-01-01T23:59:59.999", d.toIso8601String()); 1283 Expect.equals("-010000-01-01T23:59:59.999", d.toIso8601String());
951 d = new DateTime.utc(10000, 1, 1, 23, 59, 59, 999); 1284 d = new DateTime.utc(10000, 1, 1, 23, 59, 59, 999);
952 Expect.equals("+010000-01-01T23:59:59.999Z", d.toIso8601String()); 1285 Expect.equals("+010000-01-01T23:59:59.999Z", d.toIso8601String());
953 d = new DateTime.utc(-10000, 1, 1, 23, 59, 59, 999); 1286 d = new DateTime.utc(-10000, 1, 1, 23, 59, 59, 999);
954 Expect.equals("-010000-01-01T23:59:59.999Z", d.toIso8601String()); 1287 Expect.equals("-010000-01-01T23:59:59.999Z", d.toIso8601String());
1288
1289 if (!supportsMicroseconds) return;
1290
1291 d = new DateTime(9999, 1, 1, 23, 59, 59, 999, 999);
1292 Expect.equals("9999-01-01T23:59:59.999999", d.toIso8601String());
1293 d = new DateTime(-9999, 1, 1, 23, 59, 59, 999, 999);
1294 Expect.equals("-9999-01-01T23:59:59.999999", d.toIso8601String());
1295 d = new DateTime.utc(9999, 1, 1, 23, 59, 59, 999, 999);
1296 Expect.equals("9999-01-01T23:59:59.999999Z", d.toIso8601String());
1297 d = new DateTime.utc(-9999, 1, 1, 23, 59, 59, 999, 999);
1298 Expect.equals("-9999-01-01T23:59:59.999999Z", d.toIso8601String());
1299
1300 d = new DateTime(10000, 1, 1, 23, 59, 59, 999, 999);
1301 Expect.equals("+010000-01-01T23:59:59.999999", d.toIso8601String());
1302 d = new DateTime(-10000, 1, 1, 23, 59, 59, 999, 999);
1303 Expect.equals("-010000-01-01T23:59:59.999999", d.toIso8601String());
1304 d = new DateTime.utc(10000, 1, 1, 23, 59, 59, 999, 999);
1305 Expect.equals("+010000-01-01T23:59:59.999999Z", d.toIso8601String());
1306 d = new DateTime.utc(-10000, 1, 1, 23, 59, 59, 999, 999);
1307 Expect.equals("-010000-01-01T23:59:59.999999Z", d.toIso8601String());
1308
1309 d = new DateTime(9999, 1, 1, 23, 49, 59, 989, 979);
1310 Expect.equals("9999-01-01T23:49:59.989979", d.toIso8601String());
1311 d = new DateTime(-9999, 1, 1, 23, 49, 59, 989, 979);
1312 Expect.equals("-9999-01-01T23:49:59.989979", d.toIso8601String());
1313 d = new DateTime.utc(9999, 1, 1, 23, 49, 59, 989, 979);
1314 Expect.equals("9999-01-01T23:49:59.989979Z", d.toIso8601String());
1315 d = new DateTime.utc(-9999, 1, 1, 23, 49, 59, 989, 979);
1316 Expect.equals("-9999-01-01T23:49:59.989979Z", d.toIso8601String());
1317
1318 d = new DateTime(10000, 1, 1, 23, 49, 59, 989, 979);
1319 Expect.equals("+010000-01-01T23:49:59.989979", d.toIso8601String());
1320 d = new DateTime(-10000, 1, 1, 23, 49, 59, 989, 979);
1321 Expect.equals("-010000-01-01T23:49:59.989979", d.toIso8601String());
1322 d = new DateTime.utc(10000, 1, 1, 23, 49, 59, 989, 979);
1323 Expect.equals("+010000-01-01T23:49:59.989979Z", d.toIso8601String());
1324 d = new DateTime.utc(-10000, 1, 1, 23, 49, 59, 989, 979);
1325 Expect.equals("-010000-01-01T23:49:59.989979Z", d.toIso8601String());
955 } 1326 }
956 1327
957 void main() { 1328 void main() {
958 testNow(); 1329 testNow();
959 testValue(); 1330 testMillisecondsSinceEpoch();
1331 testMicrosecondsSinceEpoch();
960 testConstructors(); 1332 testConstructors();
961 testUTCGetters(); 1333 testUTCGetters();
962 testLocalGetters(); 1334 testLocalGetters();
963 testChangeTimeZone(); 1335 testChangeTimeZone();
964 testSubAdd(); 1336 testSubAdd();
965 testUnderflowAndOverflow(); 1337 testUnderflowAndOverflow();
966 testDateStrings(); 1338 testDateStrings();
967 testEquivalentYears(); 1339 testEquivalentYears();
968 testExtremes(); 1340 testExtremes();
969 testFarAwayDates(); 1341 testFarAwayDates();
970 testWeekday(); 1342 testWeekday();
971 testToStrings(); 1343 testToStrings();
972 testIsoString(); 1344 testIsoString();
973 } 1345 }
OLDNEW
« no previous file with comments | « tests/corelib/date_time_parse_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698