File: 37local-add.t

package info (click to toggle)
libdatetime-perl 2%3A1.50-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,504 kB
  • sloc: perl: 2,964; makefile: 3
file content (226 lines) | stat: -rw-r--r-- 5,502 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use strict;
use warnings;

use Test::Fatal;
use Test::More;

use DateTime;

# These tests should be the final word on dt addition involving a
# DST-changing time zone

# time addition is "wait X amount of time, then what does the clock
# say?"  this means it acts on the UTC components.
{
    my $dt = DateTime->new(
        year      => 2003, month => 4, day => 6,
        time_zone => 'America/Chicago',
    );

    $dt->add( hours => 1 );
    is(
        $dt->datetime, '2003-04-06T01:00:00',
        'add one hour to midnight, get 1 am'
    );

    is(
        exception { $dt->add( hours => 1 ) },
        undef,
        'no error adding 1 hour just before DST leap forward'
    );
    is(
        $dt->datetime, '2003-04-06T03:00:00',
        'add one hour to 1 am, get 3 am'
    );

    $dt->subtract( hours => 1 );
    is(
        $dt->datetime, '2003-04-06T01:00:00',
        'subtract one hour from 3 am, get 1 am'
    );

    $dt->subtract( hours => 1 );
    is(
        $dt->datetime, '2003-04-06T00:00:00',
        'subtract one hour from 1 am, get midnight'
    );
}

{
    my $dt = DateTime->new(
        year      => 2003, month => 10, day => 26,
        time_zone => 'America/Chicago',
    );

    $dt->add( hours => 1 );
    is(
        $dt->datetime, '2003-10-26T01:00:00',
        'add one hour to midnight, get 1 am'
    );

    $dt->add( hours => 1 );
    is(
        $dt->datetime, '2003-10-26T01:00:00',
        'add one hour to 1 am, get 1 am (again)'
    );

    $dt->add( hours => 1 );
    is(
        $dt->datetime, '2003-10-26T02:00:00',
        'add one hour to 1 am (2nd time), get 2 am'
    );

    $dt->subtract( hours => 1 );
    is(
        $dt->datetime, '2003-10-26T01:00:00',
        'subtract 1 hour from 2 am, get 1 am'
    );

    $dt->subtract( hours => 1 );
    is(
        $dt->datetime, '2003-10-26T01:00:00',
        'subtract 1 hour from 1 am, get 1 am (again)'
    );

    $dt->subtract( hours => 1 );
    is(
        $dt->datetime, '2003-10-26T00:00:00',
        'subtract 1 hour from 1 am (2nd), get midnight'
    );
}

# date addition is "leave the clock alone, just change the date
# portion".  this means it acts on local components
{
    my $dt = DateTime->new(
        year      => 2003, month => 4, day => 6,
        time_zone => 'America/Chicago',
    );

    $dt->add( days => 1 );
    is(
        $dt->datetime, '2003-04-07T00:00:00',
        'add 1 day at midnight, same clock time'
    );

    $dt->add( months => 7 );
    is(
        $dt->datetime, '2003-11-07T00:00:00',
        'add 7 months at midnight, same clock time'
    );

    $dt->subtract( months => 7 );
    is(
        $dt->datetime, '2003-04-07T00:00:00',
        'subtract 7 months at midnight, same clock time'
    );

    $dt->subtract( days => 1 );
    is(
        $dt->datetime, '2003-04-06T00:00:00',
        'subtract 1 day at midnight, same clock time'
    );
}

{
    my $dt = DateTime->new(
        year      => 2003, month => 10, day => 26,
        time_zone => 'America/Chicago',
    );

    $dt->add( days => 1 );
    is(
        $dt->datetime, '2003-10-27T00:00:00',
        'add 1 day at midnight, get midnight'
    );

    $dt->add( months => 7 );
    is(
        $dt->datetime, '2004-05-27T00:00:00',
        'add 7 months at midnight, get midnight'
    );

    $dt->subtract( months => 7 );
    is(
        $dt->datetime, '2003-10-27T00:00:00',
        'subtract 7 months at midnight, get midnight'
    );

    $dt->subtract( days => 1 );
    is(
        $dt->datetime, '2003-10-26T00:00:00',
        'subtract 1 day at midnight, get midnight'
    );
}

# date and time addition in one call is still two separate operations.
# First we do date, then time.
{
    my $dt = DateTime->new(
        year      => 2003, month => 4, day => 5,
        time_zone => 'America/Chicago',
    );

    $dt->add( days => 1, hours => 2 );
    is(
        $dt->datetime, '2003-04-06T03:00:00',
        'add one day & 2 hours from midnight, get 3 am'
    );

    # !!! - not reversible this way - needs some good docs
    my $dt1 = $dt->clone->subtract( days => 1, hours => 2 );
    is(
        $dt1->datetime, '2003-04-05T01:00:00',
        'subtract one day & 2 hours from 3 am, get 1 am'
    );

    # is reversible this way - also needs docs
    my $dt2 = $dt->clone->subtract( hours => 2 )->subtract( days => 1 );
    is(
        $dt2->datetime, '2003-04-05T00:00:00',
        'subtract 2 hours and then one day from 3 am, get midnight'
    );
}

{
    my $dt = DateTime->new(
        year      => 2003, month => 10, day => 25,
        time_zone => 'America/Chicago',
    );

    $dt->add( days => 1, hours => 2 );
    is(
        $dt->datetime, '2003-10-26T01:00:00',
        'add one day & 2 hours from midnight, get 1 am'
    );

    my $dt1 = $dt->clone->subtract( days => 1, hours => 2 );
    is(
        $dt1->datetime, '2003-10-24T23:00:00',
        'add one day & 2 hours from midnight, get 11 pm'
    );

    my $dt2 = $dt->clone->subtract( hours => 2 )->subtract( days => 1 );
    is(
        $dt2->datetime, '2003-10-25T00:00:00',
        'subtract 2 hours and then one day from 3 am, get midnight'
    );
}

# an example from the docs
{
    my $dt = DateTime->new(
        year      => 2003, month => 4, day => 5,
        hour      => 2,
        time_zone => 'America/Chicago',
    );

    $dt->add( hours => 24 );

    is(
        $dt->datetime, '2003-04-06T03:00:00',
        'datetime after adding 24 hours is 2003-04-06T03:00:00'
    );
}

done_testing();