| OLD | NEW |
| 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 // Identical to _MAX_MILLISECONDS_SINCE_EPOCH in date_time.dart |
| 10 const int _MAX_MILLISECONDS = 8640000000000000; |
| 11 |
| 9 // Tests if the time moves eventually forward. | 12 // Tests if the time moves eventually forward. |
| 10 void testNow() { | 13 void testNow() { |
| 11 var t1 = new DateTime.now(); | 14 var t1 = new DateTime.now(); |
| 12 bool timeMovedForward = false; | 15 bool timeMovedForward = false; |
| 13 for (int i = 0; i < 1000000; i++) { | 16 for (int i = 0; i < 1000000; i++) { |
| 14 var t2 = new DateTime.now(); | 17 var t2 = new DateTime.now(); |
| 15 if (t1.millisecondsSinceEpoch < t2.millisecondsSinceEpoch) { | 18 if (t1.millisecondsSinceEpoch < t2.millisecondsSinceEpoch) { |
| 16 timeMovedForward = true; | 19 timeMovedForward = true; |
| 17 break; | 20 break; |
| 18 } | 21 } |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 Expect.equals(18, dt.day); | 228 Expect.equals(18, dt.day); |
| 226 Expect.equals(13, dt.hour); | 229 Expect.equals(13, dt.hour); |
| 227 Expect.equals(20, dt.minute); | 230 Expect.equals(20, dt.minute); |
| 228 Expect.equals(0, dt.second); | 231 Expect.equals(0, dt.second); |
| 229 Expect.equals(0, dt.millisecond); | 232 Expect.equals(0, dt.millisecond); |
| 230 Expect.equals("2036-07-18 13:20:00.000", dt.toString()); | 233 Expect.equals("2036-07-18 13:20:00.000", dt.toString()); |
| 231 } | 234 } |
| 232 | 235 |
| 233 void testExtremes() { | 236 void testExtremes() { |
| 234 var dt = | 237 var dt = |
| 235 new DateTime.fromMillisecondsSinceEpoch(8640000000000000, isUtc: true); | 238 new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true); |
| 236 Expect.equals(275760, dt.year); | 239 Expect.equals(275760, dt.year); |
| 237 Expect.equals(9, dt.month); | 240 Expect.equals(9, dt.month); |
| 238 Expect.equals(13, dt.day); | 241 Expect.equals(13, dt.day); |
| 239 Expect.equals(0, dt.hour); | 242 Expect.equals(0, dt.hour); |
| 240 Expect.equals(0, dt.minute); | 243 Expect.equals(0, dt.minute); |
| 241 Expect.equals(0, dt.second); | 244 Expect.equals(0, dt.second); |
| 242 Expect.equals(0, dt.millisecond); | 245 Expect.equals(0, dt.millisecond); |
| 243 dt = new DateTime.fromMillisecondsSinceEpoch(-8640000000000000, isUtc: true); | 246 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true); |
| 244 Expect.equals(-271821, dt.year); | 247 Expect.equals(-271821, dt.year); |
| 245 Expect.equals(4, dt.month); | 248 Expect.equals(4, dt.month); |
| 246 Expect.equals(20, dt.day); | 249 Expect.equals(20, dt.day); |
| 247 Expect.equals(0, dt.hour); | 250 Expect.equals(0, dt.hour); |
| 248 Expect.equals(0, dt.minute); | 251 Expect.equals(0, dt.minute); |
| 249 Expect.equals(0, dt.second); | 252 Expect.equals(0, dt.second); |
| 250 Expect.equals(0, dt.millisecond); | 253 Expect.equals(0, dt.millisecond); |
| 251 // Make sure that we can build the extreme dates in local too. | 254 // Make sure that we can build the extreme dates in local too. |
| 252 dt = new DateTime.fromMillisecondsSinceEpoch(8640000000000000); | 255 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS); |
| 253 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute); | 256 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute); |
| 254 Expect.equals(8640000000000000, dt.millisecondsSinceEpoch); | 257 Expect.equals(_MAX_MILLISECONDS, dt.millisecondsSinceEpoch); |
| 255 dt = new DateTime.fromMillisecondsSinceEpoch(-8640000000000000); | 258 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS); |
| 256 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute); | 259 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute); |
| 257 Expect.equals(-8640000000000000, dt.millisecondsSinceEpoch); | 260 Expect.equals(-_MAX_MILLISECONDS, dt.millisecondsSinceEpoch); |
| 258 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(8640000000000001, | 261 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch( |
| 259 isUtc: true)); | 262 _MAX_MILLISECONDS + 1, isUtc: true)); |
| 260 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(-8640000000000001, | 263 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch( |
| 261 isUtc: true)); | 264 -_MAX_MILLISECONDS - 1, isUtc: true)); |
| 262 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(8640000000000001))
; | 265 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch( |
| 263 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(-8640000000000001)
); | 266 _MAX_MILLISECONDS + 1)); |
| 264 dt = new DateTime.fromMillisecondsSinceEpoch(8640000000000000); | 267 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch( |
| 268 -_MAX_MILLISECONDS - 1)); |
| 269 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS); |
| 265 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day, | 270 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day, |
| 266 dt.hour, dt.minute, 0, 1)); | 271 dt.hour, dt.minute, 0, 1)); |
| 267 dt = new DateTime.fromMillisecondsSinceEpoch(8640000000000000, isUtc: true); | 272 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true); |
| 268 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day, | 273 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day, |
| 269 dt.hour, dt.minute, 0, 1)); | 274 dt.hour, dt.minute, 0, 1)); |
| 270 dt = new DateTime.fromMillisecondsSinceEpoch(-8640000000000000); | 275 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS); |
| 271 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day, | 276 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day, |
| 272 dt.hour, dt.minute, 0, -1)); | 277 dt.hour, dt.minute, 0, -1)); |
| 273 dt = new DateTime.fromMillisecondsSinceEpoch(-8640000000000000, isUtc: true); | 278 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true); |
| 274 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day, | 279 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day, |
| 275 dt.hour, dt.minute, 0, -1)); | 280 dt.hour, dt.minute, 0, -1)); |
| 276 } | 281 } |
| 277 | 282 |
| 278 void testUTCGetters() { | 283 void testUTCGetters() { |
| 279 var dt = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true); | 284 var dt = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true); |
| 280 Expect.equals(2011, dt.year); | 285 Expect.equals(2011, dt.year); |
| 281 Expect.equals(5, dt.month); | 286 Expect.equals(5, dt.month); |
| 282 Expect.equals(11, dt.day); | 287 Expect.equals(11, dt.day); |
| 283 Expect.equals(18, dt.hour); | 288 Expect.equals(18, dt.hour); |
| (...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 Expect.equals(true, dt1.isUtc); | 846 Expect.equals(true, dt1.isUtc); |
| 842 dt1 = DateTime.parse("0009-09-09 09:09:09.009-2134"); | 847 dt1 = DateTime.parse("0009-09-09 09:09:09.009-2134"); |
| 843 Expect.equals(9, dt1.year); | 848 Expect.equals(9, dt1.year); |
| 844 Expect.equals(9, dt1.month); | 849 Expect.equals(9, dt1.month); |
| 845 Expect.equals(10, dt1.day); | 850 Expect.equals(10, dt1.day); |
| 846 Expect.equals(6, dt1.hour); | 851 Expect.equals(6, dt1.hour); |
| 847 Expect.equals(43, dt1.minute); | 852 Expect.equals(43, dt1.minute); |
| 848 Expect.equals(9, dt1.second); | 853 Expect.equals(9, dt1.second); |
| 849 Expect.equals(9, dt1.millisecond); | 854 Expect.equals(9, dt1.millisecond); |
| 850 Expect.equals(true, dt1.isUtc); | 855 Expect.equals(true, dt1.isUtc); |
| 856 |
| 857 Expect.throws(() => DateTime.parse("bad"), (e) => e is FormatException); |
| 858 var bad_year = 1970 + (_MAX_MILLISECONDS ~/ (1000*60*60*24*365.2425)) + 1; |
| 859 Expect.throws(() => DateTime.parse(bad_year.toString() + "-01-01"), |
| 860 (e) => e is FormatException); |
| 861 // The last valid time; should not throw. |
| 862 dt1 = DateTime.parse("275760-09-13T00:00:00.000Z"); |
| 863 Expect.throws(() => DateTime.parse("275760-09-14T00:00:00.000Z"), |
| 864 (e) => e is FormatException); |
| 865 Expect.throws(() => DateTime.parse("275760-09-13T00:00:00.001Z"), |
| 866 (e) => e is FormatException); |
| 867 |
| 868 // first valid time; should not throw. |
| 869 dt1 = DateTime.parse("-271821-04-20T00:00:00.000Z"); |
| 870 Expect.throws(() => DateTime.parse("-271821-04-19T23:59:59.999Z"), |
| 871 (e) => e is FormatException); |
| 851 } | 872 } |
| 852 | 873 |
| 853 void testWeekday() { | 874 void testWeekday() { |
| 854 // 2011-10-06 is Summertime. | 875 // 2011-10-06 is Summertime. |
| 855 var d = new DateTime(2011, 10, 6, 0, 45, 37, 0); | 876 var d = new DateTime(2011, 10, 6, 0, 45, 37, 0); |
| 856 Expect.equals(DateTime.THURSDAY, d.weekday); | 877 Expect.equals(DateTime.THURSDAY, d.weekday); |
| 857 d = new DateTime.utc(2011, 10, 6, 0, 45, 37, 0); | 878 d = new DateTime.utc(2011, 10, 6, 0, 45, 37, 0); |
| 858 Expect.equals(DateTime.THURSDAY, d.weekday); | 879 Expect.equals(DateTime.THURSDAY, d.weekday); |
| 859 d = new DateTime(2011, 10, 5, 23, 45, 37, 0); | 880 d = new DateTime(2011, 10, 5, 23, 45, 37, 0); |
| 860 Expect.equals(DateTime.WEDNESDAY, d.weekday); | 881 Expect.equals(DateTime.WEDNESDAY, d.weekday); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 920 testChangeTimeZone(); | 941 testChangeTimeZone(); |
| 921 testSubAdd(); | 942 testSubAdd(); |
| 922 testUnderflowAndOverflow(); | 943 testUnderflowAndOverflow(); |
| 923 testDateStrings(); | 944 testDateStrings(); |
| 924 testEquivalentYears(); | 945 testEquivalentYears(); |
| 925 testExtremes(); | 946 testExtremes(); |
| 926 testFarAwayDates(); | 947 testFarAwayDates(); |
| 927 testWeekday(); | 948 testWeekday(); |
| 928 testToStrings(); | 949 testToStrings(); |
| 929 } | 950 } |
| OLD | NEW |