|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 | + |
| 5 | +import "package:expect/expect.dart"; |
| 6 | + |
| 7 | +// Dart test program for DateTime, far away dates. |
| 8 | + |
| 9 | +// TODO(37442): Find far-away dates with milliseconds-since-epoch values that |
| 10 | +// are 'web' integers. |
| 11 | + |
| 12 | +bool get supportsMicroseconds => |
| 13 | + new DateTime.fromMicrosecondsSinceEpoch(1).microsecondsSinceEpoch == 1; |
| 14 | + |
| 15 | +void testFarAwayDates() { |
| 16 | + DateTime dt = |
| 17 | + new DateTime.fromMillisecondsSinceEpoch(1000000000000001, isUtc: true); |
| 18 | + Expect.equals(33658, dt.year); |
| 19 | + Expect.equals(9, dt.month); |
| 20 | + Expect.equals(27, dt.day); |
| 21 | + Expect.equals(1, dt.hour); |
| 22 | + Expect.equals(46, dt.minute); |
| 23 | + Expect.equals(40, dt.second); |
| 24 | + Expect.equals(1, dt.millisecond); |
| 25 | + Expect.equals(0, dt.microsecond); |
| 26 | + dt = new DateTime.fromMillisecondsSinceEpoch(-1000000000000001, isUtc: true); |
| 27 | + Expect.equals(-29719, dt.year); |
| 28 | + Expect.equals(4, dt.month); |
| 29 | + Expect.equals(5, dt.day); |
| 30 | + Expect.equals(22, dt.hour); |
| 31 | + Expect.equals(13, dt.minute); |
| 32 | + Expect.equals(19, dt.second); |
| 33 | + Expect.equals(999, dt.millisecond); |
| 34 | + Expect.equals(0, dt.microsecond); |
| 35 | + // Same with local zone. |
| 36 | + dt = new DateTime.fromMillisecondsSinceEpoch(1000000000000001); |
| 37 | + Expect.equals(33658, dt.year); |
| 38 | + Expect.equals(9, dt.month); |
| 39 | + Expect.equals(true, dt.day == 27 || dt.day == 26); |
| 40 | + // Not much we can test for local hour. |
| 41 | + Expect.equals(true, dt.hour >= 0 && dt.hour < 24); |
| 42 | + // Timezones can have offsets down to 15 minute. |
| 43 | + Expect.equals(true, dt.minute % 15 == 46 % 15); |
| 44 | + Expect.equals(40, dt.second); |
| 45 | + Expect.equals(1, dt.millisecond); |
| 46 | + Expect.equals(0, dt.microsecond); |
| 47 | + dt = new DateTime.fromMillisecondsSinceEpoch(-1000000000000001); |
| 48 | + Expect.equals(-29719, dt.year); |
| 49 | + Expect.equals(4, dt.month); |
| 50 | + Expect.equals(true, 5 == dt.day || 6 == dt.day); |
| 51 | + // Not much we can test for local hour. |
| 52 | + Expect.equals(true, dt.hour >= 0 && dt.hour < 24); |
| 53 | + // Timezones can have offsets down to 15 minute. |
| 54 | + Expect.equals(true, dt.minute % 15 == 13); |
| 55 | + Expect.equals(19, dt.second); |
| 56 | + Expect.equals(999, dt.millisecond); |
| 57 | + Expect.equals(0, dt.microsecond); |
| 58 | + |
| 59 | + if (!supportsMicroseconds) return; |
| 60 | + dt = |
| 61 | + new DateTime.fromMicrosecondsSinceEpoch(1000000000000000001, isUtc: true); |
| 62 | + Expect.equals(33658, dt.year); |
| 63 | + Expect.equals(9, dt.month); |
| 64 | + Expect.equals(27, dt.day); |
| 65 | + Expect.equals(1, dt.hour); |
| 66 | + Expect.equals(46, dt.minute); |
| 67 | + Expect.equals(40, dt.second); |
| 68 | + Expect.equals(0, dt.millisecond); |
| 69 | + Expect.equals(1, dt.microsecond); |
| 70 | + dt = new DateTime.fromMicrosecondsSinceEpoch(-1000000000000000001, |
| 71 | + isUtc: true); |
| 72 | + Expect.equals(-29719, dt.year); |
| 73 | + Expect.equals(4, dt.month); |
| 74 | + Expect.equals(5, dt.day); |
| 75 | + Expect.equals(22, dt.hour); |
| 76 | + Expect.equals(13, dt.minute); |
| 77 | + Expect.equals(19, dt.second); |
| 78 | + Expect.equals(999, dt.millisecond); |
| 79 | + Expect.equals(999, dt.microsecond); |
| 80 | + // Same with local zone. |
| 81 | + dt = new DateTime.fromMicrosecondsSinceEpoch(1000000000000000001); |
| 82 | + Expect.equals(33658, dt.year); |
| 83 | + Expect.equals(9, dt.month); |
| 84 | + Expect.equals(true, dt.day == 27 || dt.day == 26); |
| 85 | + // Not much we can test for local hour. |
| 86 | + Expect.equals(true, dt.hour >= 0 && dt.hour < 24); |
| 87 | + // Timezones can have offsets down to 15 minute. |
| 88 | + Expect.equals(true, dt.minute % 15 == 46 % 15); |
| 89 | + Expect.equals(40, dt.second); |
| 90 | + Expect.equals(0, dt.millisecond); |
| 91 | + Expect.equals(1, dt.microsecond); |
| 92 | + dt = new DateTime.fromMicrosecondsSinceEpoch(-1000000000000000001); |
| 93 | + Expect.equals(-29719, dt.year); |
| 94 | + Expect.equals(4, dt.month); |
| 95 | + Expect.equals(true, 5 == dt.day || 6 == dt.day); |
| 96 | + // Not much we can test for local hour. |
| 97 | + Expect.equals(true, dt.hour >= 0 && dt.hour < 24); |
| 98 | + // Timezones can have offsets down to 15 minute. |
| 99 | + Expect.equals(true, dt.minute % 15 == 13); |
| 100 | + Expect.equals(19, dt.second); |
| 101 | + Expect.equals(999, dt.millisecond); |
| 102 | + Expect.equals(999, dt.microsecond); |
| 103 | +} |
| 104 | + |
| 105 | +void main() { |
| 106 | + testFarAwayDates(); |
| 107 | +} |
0 commit comments