File: 2_fmt_normalise.t

package info (click to toggle)
libdatetime-format-duration-perl 1.04-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 392 kB
  • sloc: perl: 671; makefile: 2
file content (174 lines) | stat: -rwxr-xr-x 6,041 bytes parent folder | download
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl 1.t'

use warnings;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Duration;

#########################

use Test::More tests=>52;


my $x = 0; # Test Counter


$strf = DateTime::Format::Duration->new(
    base => DateTime->new( year=> 2004, time_zone=>"Australia/Melbourne" ),
    pattern => '%P%F %r',
);
$strf->{diagnostic} = join('',@ARGV) =~ /\!/;

test(
    ['0000-00-00 00:00:01',     '0000-00-00 00:00:01'],
    ['0000-00-00 00:00:59',     '0000-00-00 00:00:59'],
    ['0000-00-00 00:00:60',     '0000-00-00 00:01:00'],
    ['0000-00-00 00:00:61',     '0000-00-00 00:01:01'],
    ['0000-00-00 00:00:119',    '0000-00-00 00:01:59'],
    ['0000-00-00 00:00:120',    '0000-00-00 00:02:00'],
    ['0000-00-00 00:00:121',    '0000-00-00 00:02:01'],

    ['0000-00-00 00:01:00',     '0000-00-00 00:01:00'],
    ['0000-00-00 00:59:00',     '0000-00-00 00:59:00'],
    ['0000-00-00 00:60:00',     '0000-00-00 01:00:00'],
    ['0000-00-00 00:61:00',     '0000-00-00 01:01:00'],
    ['0000-00-00 00:01:59',     '0000-00-00 00:01:59'],
    ['0000-00-00 00:01:60',     '0000-00-00 00:02:00'],
    ['0000-00-00 00:01:61',     '0000-00-00 00:02:01'],
    ['0000-00-00 00:59:60',     '0000-00-00 01:00:00'],
    ['0000-00-00 00:60:60',     '0000-00-00 01:01:00'],

    ['0000-00-00 01:00:00',     '0000-00-00 01:00:00'],
    ['0000-00-00 23:00:00',     '0000-00-00 23:00:00'],
    ['0000-00-00 24:00:00',     '0000-00-01 00:00:00'],
    ['0000-00-00 25:00:00',     '0000-00-01 01:00:00'],
    ['0000-00-00 01:59:00',     '0000-00-00 01:59:00'],
    ['0000-00-00 01:60:00',     '0000-00-00 02:00:00'],
    ['0000-00-00 01:61:00',     '0000-00-00 02:01:00'],
    ['0000-00-00 23:60:00',     '0000-00-01 00:00:00'],
    ['0000-00-00 24:60:00',     '0000-00-01 01:00:00'],

    ['0000-00-00 00:00:86400',  '0000-00-01 00:00:00'], # Overflows
    ['0000-00-00 00:1440:00',   '0000-00-01 00:00:00'],
    ['0000-00-00 240:00:00',    '0000-00-10 00:00:00',],
    ['0000-00-45 00:00:00',     '0000-01-14 00:00:00',],
    ['0000-240-00 00:00:00',    '0020-00-00 00:00:00',],
    ['0000-00-00 00:00:-86400', '-0000-00-01 00:00:00'], # Underflows
    ['0000-00-00 00:-1440:00',  '-0000-00-01 00:00:00'],
    ['0000-00-00 -240:00:00',   '-0000-00-10 00:00:00',],
    ['0000-00--45 00:00:00',    '-0000-01-14 00:00:00',],
    ['0000--240-00 00:00:00',   '-0020-00-00 00:00:00',],

    ['0000-00-00 00:00:-01',    '-0000-00-00 00:00:01'],
    ['0000-00-00 00:00:-59',    '-0000-00-00 00:00:59'],
    ['0000-00-00 00:00:-60',    '-0000-00-00 00:01:00'],
    ['0000-00-00 00:00:-61',    '-0000-00-00 00:01:01'],

    ['0000-00-00 -01:01:-01',   '-0000-00-00 00:59:01'], # Mixed positivity

);


# DST Changeovers are easier to see if we use better bases:
$strf->set_base(DateTime->new( year=> 2004, month=>3, day=>27, time_zone=>"Australia/Melbourne" ));
test(
    ['0000-00-00 48:00:00',     '0000-00-01 23:00:00', 'DST ends, Day is 25 hours long'],
    ['0000-00-02 -48:00:00',    '0000-00-00 01:00:00'],
    ['0000-00-02 00:00:00',     '0000-00-02 00:00:00'], # days are days, they don't change
    ['0000-00-00 24:00:00',     '0000-00-01 00:00:00'], # hours not crossing the DST don't change

);

$strf->set_base(DateTime->new( year=> 2004, month=>10, day=>30, time_zone=>"Australia/Melbourne" ));
test(
    ['0000-00-00 48:00:00',     '0000-00-02 01:00:00', 'DST starts, Day is 23 hours long'],
    ['0000-00-02 -48:00:00',    '-0000-00-00 01:00:00'],
    ['0000-00-02 00:00:00',     '0000-00-02 00:00:00'], # days are days, they don't change
    ['0000-00-00 24:00:00',     '0000-00-01 00:00:00'], # hours not crossing the DST don't change
);


# Leap Seconds are easier to see if we use better bases:
$strf->set_base(DateTime->new( year=> 1998, month=>12, day=>31, hour=>23, minute=>58, time_zone=>"UTC" ));
test(
    ['0000-00-00 00:01:120',    '0000-00-00 00:02:59', 'Leap Second adds an extra second to one of the minutes.'],
    ['0000-00-00 00:01:60',     '0000-00-00 00:01:60'],
    ['0000-00-00 00:03:00',     '0000-00-00 00:03:00'],
    ['0000-00-00 00:00:60',     '0000-00-00 00:01:00'],
);





# ------------------ TESTING ROUTINES -------------------------

sub test {
    my @tests = @_;

    foreach my $test (@tests) {
        my $w = ($x++ < 9) ? 22 : 21;
        next unless in_range($x);

        diag($test->[2]) if $test->[2];
        is(
            $strf->format_duration_from_deltas(
                $strf->parse_duration_as_deltas(
                    $test->[0]
                )
            ),
            $test->[1],
            sprintf("Test %2d: %${w}s should %s %s", $x, $test->[0], ($test->[0] eq $test->[1]) ? 'stay  ':'become', $test->[1]) # . (($test->[2]) ? sprintf(" (%s)",$test->[2]) : '')
        ) or diag( "If you send an error report, please include the output of:\n $^X $0 $x!" );
    }
}

sub in_range {
    # see if this test is in our list of tests:
    return 1 unless $ARGV[0];

    my $test = shift;

    $argv = join(',', @ARGV);
    $argv=~s/,\.\.,/../g;
    $argv=~s/,,/,/g;

    $argv=~s/\!//;

    return 1 if $argv=~/\b$test\b/;

    foreach my $part( split(/,\s*/, $argv) ) {
        my ($start, $end) = $part =~ /(\d+)\s*\.\.\s*(\d+)/;
        ($start, $end) = ($end, $start) if $end < $start;

        next if $start > $test;
        return 1 if $end > $test

    }

    return 0;
}

sub Dump {
    eval{
        require Data::Dumper
    };
    return "<Couldn't load Data::Dumper>" if $@;
    return Data::Dumper::Dumper(@_)
}



#    Oct 30 (24 hrs)    #    Oct 31 (25 hrs)     #     Nov 1 (24 hrs)    #
#-----------|-----------#------------|-----------#-----------|-----------#

# 48 Hours == 1 day, 24 hours:
#-----------------------------------------------#
#---- 1 day ------------#------ 24 hours -------#

# 2 Days, -48 Hours == 1 hour:
#------------------- 2 days --------------------#
 #------------------ 48 hours ------------------#
# 1 hour