File: 08truncate.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (51 lines) | stat: -rw-r--r-- 1,492 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use strict;
use warnings;
use Test::More tests => 24;

use Time::Piece;

my $epoch = 1373371631;
my $t = gmtime($epoch); # 2013-07-09T12:07:11

is ($t->truncate,        $t, 'No args, same object');
is ($t->truncate('foo'), $t, 'No "to" arg, same object');
eval { $t->truncate('to') };
like ($@, qr/Invalid value of 'to' parameter/,
        'No "to" value croaks');
eval { $t->truncate('to' => 'foo') };
like ($@, qr/Invalid value of 'to' parameter: foo/,
        'Unrecognised "to" value croaks');

my $short = $t->truncate(to => 'second');
my $exp   = $epoch;
cmp_ok ($short->epoch, '==', $exp, 'Truncate to second');

$short = $t->truncate(to => 'minute');
$exp   -= 11;
cmp_ok ($short->epoch, '==', $exp, 'Truncate to minute');

$short = $t->truncate(to => 'hour');
$exp   -= 420;
cmp_ok ($short->epoch, '==', $exp, 'Truncate to hour');

$short = $t->truncate(to => 'day');
$exp   -= 43200;
cmp_ok ($short->epoch, '==', $exp, 'Truncate to day');

$short = $t->truncate(to => 'month');
$exp   -= 8 * 86400;
cmp_ok ($short->epoch, '==', $exp, 'Truncate to month');

$exp = gmtime ($exp)->add_months(-6);
$short = $t->truncate(to => 'year');
cmp_ok ($short, '==', $exp, 'Truncate to year');

is ($t->epoch, $epoch, 'Time unchanged');

for my $addmon (0..12) {
    my $quarter = $short->add_months ($addmon);
    $exp   = $quarter->add_months (0 - ($addmon % 3));
    $quarter = $quarter->truncate(to => 'quarter');
    cmp_ok ($quarter, '==', $exp, "Truncate to quarter (month $addmon)");

}