1use strict;
2use warnings;
3use Test::More tests => 24;
4
5use Time::Piece;
6
7my $epoch = 1373371631;
8my $t = gmtime($epoch); # 2013-07-09T12:07:11
9
10is ($t->truncate,        $t, 'No args, same object');
11is ($t->truncate('foo'), $t, 'No "to" arg, same object');
12eval { $t->truncate('to') };
13like ($@, qr/Invalid value of 'to' parameter/,
14        'No "to" value croaks');
15eval { $t->truncate('to' => 'foo') };
16like ($@, qr/Invalid value of 'to' parameter: foo/,
17        'Unrecognised "to" value croaks');
18
19my $short = $t->truncate(to => 'second');
20my $exp   = $epoch;
21cmp_ok ($short->epoch, '==', $exp, 'Truncate to second');
22
23$short = $t->truncate(to => 'minute');
24$exp   -= 11;
25cmp_ok ($short->epoch, '==', $exp, 'Truncate to minute');
26
27$short = $t->truncate(to => 'hour');
28$exp   -= 420;
29cmp_ok ($short->epoch, '==', $exp, 'Truncate to hour');
30
31$short = $t->truncate(to => 'day');
32$exp   -= 43200;
33cmp_ok ($short->epoch, '==', $exp, 'Truncate to day');
34
35$short = $t->truncate(to => 'month');
36$exp   -= 8 * 86400;
37cmp_ok ($short->epoch, '==', $exp, 'Truncate to month');
38
39$exp = gmtime ($exp)->add_months(-6);
40$short = $t->truncate(to => 'year');
41cmp_ok ($short, '==', $exp, 'Truncate to year');
42
43is ($t->epoch, $epoch, 'Time unchanged');
44
45for my $addmon (0..12) {
46    my $quarter = $short->add_months ($addmon);
47    $exp   = $quarter->add_months (0 - ($addmon % 3));
48    $quarter = $quarter->truncate(to => 'quarter');
49    cmp_ok ($quarter, '==', $exp, "Truncate to quarter (month $addmon)");
50
51}
52