Skip to content
Newer
Older
100644 136 lines (105 sloc) 5.63 KB
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
1 use v6;
2 use Test;
3
8d122f3 Round-trip Instant.from-posix instead of .to-posix
Juerd Waalboer authored
4 plan 55;
9b43296 Added a test for Instant.perl.
Kodi Arfer authored
5
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
6 =begin pod
7
af258c3 Add round-tripping tests for Instant.DateTime
Juerd Waalboer authored
8 We test some of the properties of Instants and Durations here
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
9 rather than in S02/instants-and-duration.t.
10
11 =end pod
12
62da93e [t/spec] More Temporal tests.
Kodi authored
13 sub dtp($year, $month, $day, $hour, $minute, $second) {
14 DateTime.new(:$year, :$month, :$day, :$hour, :$minute, :$second)
15 }
16
17 sub dtpi($year, $month, $day, $hour, $minute, $second) {
18 DateTime.new(:$year, :$month, :$day, :$hour, :$minute, :$second).Instant
19 }
20
6bbd321 @tadzik [DateTime-Instant-Duration.t] Avoid some more doubled named parameters
tadzik authored
21 sub dti(*%args) { DateTime.new(|{year => 1984, %args}).Instant }
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
22
62da93e [t/spec] More Temporal tests.
Kodi authored
23 sub dsi($s) { DateTime.new($s).Instant }
24
9fb628f @moritz correct previous commit
moritz authored
25 sub diff(%early?, *%late) { + do dti(|%late) - dti(|%early) }
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
26
27 sub days($n) { $n * 24 * 60 * 60 }
28
29
30 # L<S32::Temporal/Accessors/'the method Instant'>
31
bb0a8fa @lizmat Use isa-ok instead of isa_ok
lizmat authored
32 isa-ok dti, Instant, 'DateTime.Instant returns an Instant';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
33 is dti, dti, 'Equal DateTimes yield equal Instants';
34 is diff, 0, 'The difference of equal Instants is 0';
35
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
36 ok dsi('2005-12-31T23:59:60') < dsi('2006-01-01T00:00:00'), 'DateTime.Instant counts leap seconds';
62da93e [t/spec] More Temporal tests.
Kodi authored
37 # These seconds have equal POSIX times.
38
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
39 is diff(second => 5), 5, 'Instant subtraction (seconds)';
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
40 is diff(second => 2/7), 2/7, 'Instant subtraction (non-integral seconds)';
a6eda93 [t/spec] Instant and Duration tweaks.
Kodi authored
41 is diff(second => 3.14159), 3.14159, 'Instant subtraction (needing high precision)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
42 is diff(minute => 15), 15 * 60, 'Instant subtraction (minutes)';
43 is diff(:hour(3), :minute(15), :second(33)),
44 3*60*60 + 15*60 + 33, 'Instant subtraction (HMS)';
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
45 is diff(day => 4), days(3), 'Instant subtraction (days)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
46 is diff(month => 2), days(31), 'Instant subtraction (a month)';
47 is diff(month => 3), days(31 + 29), 'Instant subtraction (Jan and Feb, leap year)';
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
48 is diff({year => 1985}, year => 1985, month => 3), days(31 + 28), 'Instant subtraction (Jan and Feb, common year)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
49 is diff(:year(1985), :month(3), :day(14)),
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
50 days(366 + 31 + 28 + 13), 'Instant subtraction (YMD)';
51 is +(DateTime.new('1985-03-14T13:28:22').Instant - dti),
52 days(366 + 31 + 28 + 13) + 13*60*60 + 28*60 + 22, 'Instant subtraction (YMDHMS)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
53
54 {
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
55 my $a = dtp(2004, 12, 31, 23, 57, 8.5);
56 my $b = dtp(2005, 1, 1, 2, 22, 13.4);
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
57 my $expected-diff = 60 - 8.5 + 2*60 + 2*60*60 + 22*60 + 13.4;
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
58 is +($b.Instant() - $a.Instant), $expected-diff, 'Instant subtraction (ugly case)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
59
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
60 $a .= clone(timezone => 35*60 - 5);
61 $b .= clone(timezone => 3*60*60);
62 is +($a.Instant() - $b.Instant), 0.1, 'Instant subtraction (time zones)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
63
64 diff({:year(1997), :month(6), :day(30)},
65 :year(1997), :month(7), :day(1)),
66 days(1) + 1, 'Instant subtraction (June 30 leap second)';
67 $a .= clone(year => 2005, timezone => 0);
68 $b .= clone(year => 2006, timezone => 0);
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
69 is +($b.Instant() - $a.Instant), $expected-diff + 1, 'Instant subtraction (December 31 leap second)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
70
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
71 $a = DateTime.new('2006-01-01T12:33:58+1234');
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
72 # In UTC, $a is 2005-12-31T23:59:58.
73 $b = DateTime.new('2006-01-01T12:44:03+1244');
74 # In UTC, $b is 2006-01-01T00:00:03.
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
75 is +($b.Instant() - $a.Instant), 6, 'Instant subtraction (leap second and time zones)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
76
77 $a .= clone(year => 1973);
78 $b .= clone(year => 2008);
0c1f6d4 [t/spec] Fixed some Instant and Duration tests and fudged some for Ra…
Kodi authored
79 is +($b.Instant() - $a.Instant), 1_104_451_227, 'Instant subtraction (thirty-year span)';
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
80 # I got this figure by adding 22 (the number of leap seconds
81 # between the two moments) to the difference of POSIX
82 # times.
83 }
84
85 # L<S32::Temporal/C<DateTime>/DateTime.new(now)>
86
62da93e [t/spec] More Temporal tests.
Kodi authored
87 is ~DateTime.new(dsi('2004-03-05T12:43:22')), '2004-03-05T12:43:22Z', 'Round-tripping DateTime.Instant (2004-03-05T12:43:22Z)';
88 is ~DateTime.new(dsi('2005-12-31T23:59:59')), '2005-12-31T23:59:59Z', 'Round-tripping DateTime.Instant (2005-12-31T23:59:59Z)';
89 is ~DateTime.new(dsi('2005-12-31T23:59:60')), '2005-12-31T23:59:60Z', 'Round-tripping DateTime.Instant (2005-12-31T23:59:60Z)';
90 is ~DateTime.new(dsi('2006-01-01T00:00:00')), '2006-01-01T00:00:00Z', 'Round-tripping DateTime.Instant (2006-01-01T00:00:00Z)';
91
92 is DateTime.new(dtpi 2005, 12, 31, 23, 59, 59.5).second, 59.5, 'Round-tripping DateTime.Instant (2005-12-31T23:59:59.5Z)';
93 is DateTime.new(dtpi 2005, 12, 31, 23, 59, 60.5).second, 60.5, 'Round-tripping DateTime.Instant (2005-12-31T23:59:60.5Z)';
94 is DateTime.new(dtpi 2006, 1, 1, 0, 0, 0.5).second, 0.5, 'Round-tripping DateTime.Instant (2006-01-01T00:00:00.5Z)';
95
9ee86bc [t/spec] More high-precision Instant tests.
Kodi authored
96 is DateTime.new(dtpi 2005, 12, 31, 23, 59, 59.2).second, 59.2, 'Round-tripping DateTime.Instant (2005-12-31T23:59:59.2Z)';
97 is DateTime.new(dtpi 2005, 12, 31, 23, 59, 60.2).second, 60.2, 'Round-tripping DateTime.Instant (2005-12-31T23:59:60.2Z)';
98 is DateTime.new(dtpi 2006, 1, 1, 0, 0, 0.2).second, 0.2, 'Round-tripping DateTime.Instant (2006-01-01T00:00:00.2Z)';
99
100
62da93e [t/spec] More Temporal tests.
Kodi authored
101 {
102 my $last-t = time;
103 my $t;
104 loop { # Loop until we reach the beginning of the next second.
105 $t = time;
106 last if $t > $last-t;
107 $last-t = $t;
108 }
109 my $i = now; # $t and $i are supposed to be within the
110 # same UTC second, but if we're unlucky they
111 # might not be.
d9dae9f @lizmat We have subsecond granularity now, fix test for it
lizmat authored
112 is DateTime.new($i).Str.substr(^19),DateTime.new($t).Str.substr(^19),
113 'DateTime.new(now)';
62da93e [t/spec] More Temporal tests.
Kodi authored
114 }
115
116 {
117 my $dt = DateTime.new(dsi('1999-12-31T23:59:59'),
118 timezone => -(5*60*60 + 55*60),
119 formatter => { .day ~ '/' ~ .month ~ '/' ~ .year ~ ' ' ~
120 .second ~ 's' ~ .minute ~ 'm' ~ .hour ~ 'h' });
121 is ~$dt, '31/12/1999 59s4m18h', 'DateTime.new(Instant) with time zone and formatter';
122 }
123
9b43296 Added a test for Instant.perl.
Kodi Arfer authored
124 {
125 my $i = dtpi 1988, 11, 22, 18, 42, 15.9;
8d122f3 Round-trip Instant.from-posix instead of .to-posix
Juerd Waalboer authored
126 isa-ok $i.perl.EVAL, Instant, 'Instant.perl evals to Instant';
9b43296 Added a test for Instant.perl.
Kodi Arfer authored
127 }
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
128
af258c3 Add round-tripping tests for Instant.DateTime
Juerd Waalboer authored
129 for 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1449755609 {
130 my $i = Instant.from-posix($_);
8d122f3 Round-trip Instant.from-posix instead of .to-posix
Juerd Waalboer authored
131 is $i.to-posix[0], $_, "Round-tripping Instant.[from|to]-posix ($_)";
af258c3 Add round-tripping tests for Instant.DateTime
Juerd Waalboer authored
132 is $i.DateTime.Instant, $i, "Round-tripping Instant.DateTime ($_)";
133 }
134
61aae6f [t/spec] More tests for Instants and Durations.
Kodi authored
135 # vim: ft=perl6
Something went wrong with that request. Please try again.