Handler.pm

filter: Statementstmt 45.2% 162 / 358 Branchbran 40.0% 44 / 110 Conditioncond 37.2% 19 / 51 MC/DCmcdc 10.0% 3 / 30 Subroutinesub 47.4% 28 / 59 Podpod 7.1% 3 / 42 totaltotal 39.8% 259 / 650 CC 75 SCAR 32.8
CC
75
Cov
43%
Add
94.3
new
42.1
IsLeapYear
20.0
CRAP 26.5
« Handler/Delta.pm ↑ Summary Handler/Range.pm »
1 #!/usr/bin/perl
2 package Date::Handler;
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 $VERSION = '1.2';
10  
11 8 ▶use POSIX qw(floor strftime mktime setlocale);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use Date::Handler::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use constant DEFAULT_FORMAT_STRING => '%c';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use constant DEFAULT_TIMEZONE => 'GMT';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use constant DEFAULT_LOCALE => 'en_US';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use constant DELTA_CLASS => 'Date::Handler::Delta';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 8 ▶use constant INTUITIVE_MONTH_CALCULATIONS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use constant INTUITIVE_TIME_CALCULATIONS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use constant INTUITIVE_DST_ADJUSTMENTS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 8 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=1.2
25 '""' => 'AsScalar',
26 '0+' => 'AsNumber',
27 '+' => 'Add',
28 '-' => 'Sub',
29 '<=>' => 'Cmp',
30 '++' => 'Incr',
31 0 ▶ '*' => sub { croak "Cannot multiply an absolute date"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 ▶ '**' => sub { croak "Cannot power an absolute date"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 ▶ '/' => sub { croak "Cannot divide an absolute date"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 8 fallback => 1,
35 8 );
36  
37  
38 sub new
39 499 ▶{
Subroutine new: called CC=19 SCAR=37.4
Pod undocumented
40 499 my $classname = shift;
41 499 my $args = shift;
42  
43  
44 # Allow classic style arguments passing. # Thanks to Roland Rauch <roland@rauch.com> for the spot
45 499 ▶ unless (ref($args) eq "HASH")
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args eq "HASH") 0 499 499
46 {
47 0 unshift(@_, $args);
48 0 $args = {@_};
49 }
50  
51 499 my $self = {};
52 499 bless $self, $classname;
53  
54 499 ▶ croak "No args to new()" if not defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 499 499
55 499 ▶ croak "Argument to new() is not a hashref" if not ref($args) =~ /HASH/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($args) =~ /HASH/ 0 499 499
56 499 ▶ croak "No date specified for new()" if not defined $args->{date};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"date"} 0 499 499
57  
58 499 my $date = $args->{date};
59  
60 499 ▶ my $timezone = $args->{time_zone} || $self->DEFAULT_TIMEZONE();
Condition66%2/3
$args->{'time_zone'} || $self->DEFAULT_TIMEZONE
l!l&&r!l&&!r
448510
MC/DC0%0/2
$args->{'time_zone'} || $self->DEFAULT_TIMEZONE
$args->{'time_zone'} $self->DEFAULT_TIMEZONE
Truth table66%2/3
A || B
A= $args->{'time_zone'}B= $self->DEFAULT_TIMEZONE
ABresult
1X1
011
000
61 499 $self->TimeZone($timezone);
62  
63 499 $self->{locale} = "";
64 499 ▶ if(defined $args->{locale})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'locale'}) { } 0 499 499
65 {
66 0 ▶ $self->SetLocale($args->{locale}) || $self->SetLocale($self->DEFAULT_LOCALE());
Branch: 1 branch
BranchTrueFalseTotal
$self->SetLocale($args->{"locale"}) or $self->SetLocale($self->DEFAULT_LOCALE) 0 0 0
67 }
68 else
69 {
70 499 $self->SetLocale($self->DEFAULT_LOCALE());
71 }
72  
73 499 ▶ if(not defined $self->Locale())
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->Locale) 0 499 499
74 {
75 0 warn "Impossible to set locale OR default locale correctly. Defaulting to GMT/UTC.";
76 0 $self->SetLocale('GMT');
77 }
78  
79  
80 499 ▶ if(ref($date) =~ /SCALAR/)
Branch: 3 branches
BranchTrueFalseTotal
if (ref($date) =~ /SCALAR/) { } 0 499 499
elsif (ref($date) =~ /ARRAY/) { } 357 142 499
elsif (ref($date) =~ /HASH/) { } 0 142 142
81 {
82 0 ▶ if($date !~ /\s/ && $date !~ /[A-Za-z]/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($date =~ /\s/ or $date =~ /[A-Za-z]/) 0 0 0
Condition0%0/3
$date =~ /\s/ or $date =~ /[A-Za-z]/
l!l&&r!l&&!r
000
MC/DC0%0/2
$date =~ /\s/ or $date =~ /[A-Za-z]/
$date =~ /\s/ $date =~ /[A-Za-z]/
Truth table0%0/3
A or B
A= $date =~ /\s/B= $date =~ /[A-Za-z]/
ABresult
1X1
011
000
83 {
84 0 $self->{epoch} = $date;
85 }
86 }
87 elsif(ref($date) =~ /ARRAY/)
88 {
89 357 $self->{epoch} = $self->Array2Epoch($date);
90 }
91 elsif(ref($date) =~ /HASH/)
92 {
93 $self->{epoch} = $self->Array2Epoch([
94 $date->{year},
95 $date->{month},
96 $date->{day},
97 $date->{hour},
98 $date->{min},
99 $date->{sec},
100 0 ]);
101 }
102 else
103 {
104 142 ▶ if($date !~ /\s/ && $date !~ /[A-Za-z]/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($date =~ /\s/ or $date =~ /[A-Za-z]/) 142 0 142
Condition33%1/3
$date =~ /\s/ or $date =~ /[A-Za-z]/
l!l&&r!l&&!r
00142
MC/DC0%0/2
$date =~ /\s/ or $date =~ /[A-Za-z]/
$date =~ /\s/ $date =~ /[A-Za-z]/
Truth table33%1/3
A or B
A= $date =~ /\s/B= $date =~ /[A-Za-z]/
ABresult
1X1
011
000
105 {
106 142 $self->{epoch} = $date;
107 }
108 }
109  
110 499 ▶ $self->{_intuitive_day} = $args->{intuitive_day} if($self->INTUITIVE_MONTH_CALCULATIONS());
Branch: 1 branch
BranchTrueFalseTotal
if $self->INTUITIVE_MONTH_CALCULATIONS 121 378 499
111 499 ▶ $self->{_intuitive_hour} = $args->{intuitive_hour} if($self->INTUITIVE_TIME_CALCULATIONS());
Branch: 1 branch
BranchTrueFalseTotal
if $self->INTUITIVE_TIME_CALCULATIONS 121 378 499
112  
113 499 ▶ croak "Date format not recognized." if not defined $self->{epoch};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"epoch"} 0 499 499
114  
115 499 return $self;
116 }
117  
118  
119 #Accessors (Might want to optimised some of those)
120 0 ▶sub Year { return shift->AsArray()->[0]; }
Subroutine Year: not called CC=1 SCAR=6.9
Pod undocumented
121 9 ▶sub Day { return shift->AsArray()->[2]; }
Subroutine Day: called CC=1 SCAR=0.0
Pod undocumented
122 0 ▶sub Hour { return shift->AsArray()->[3]; }
Subroutine Hour: not called CC=1 SCAR=6.9
Pod undocumented
123 0 ▶sub Min { return shift->AsArray()->[4]; }
Subroutine Min: not called CC=1 SCAR=6.9
Pod undocumented
124 0 ▶sub Sec { return shift->AsArray()->[5]; }
Subroutine Sec: not called CC=1 SCAR=6.9
Pod undocumented
125  
126  
127 #To be consistent with our WeekDay function, wich is zero based.
128 sub Month
129 102 ▶{
Subroutine Month: called CC=1 SCAR=0.0
Pod undocumented
130 102 my $self = shift;
131  
132 102 local $ENV{'TZ'} = $self->TimeZone();
133 102 local $ENV{'LC_TIME'} = $self->Locale();
134  
135 102 return strftime('%m', localtime($self->{epoch}));
136 }
137  
138 sub Epoch
139 464 ▶{
Subroutine Epoch: called CC=2 SCAR=6.9
Pod documented
140 464 my $self = shift;
141  
142 464 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 148 316 464
143 {
144 148 my $epoch = shift;
145  
146 148 $self->{epoch} = $epoch;
147 }
148  
149 464 return $self->{epoch};
150 }
151  
152 sub TimeZone
153 2402 ▶{
Subroutine TimeZone: called CC=2 SCAR=6.9
Pod documented
154 2402 my $self = shift;
155
156 2402 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 499 1903 2402
157 {
158 499 my $time_zone = shift;
159 499 $self->{time_zone} = $time_zone;
160 }
161  
162 2402 return $self->{time_zone};
163 }
164  
165 sub Locale
166 2118 ▶{
Subroutine Locale: called CC=2 SCAR=8.4
Pod undocumented
167 2118 my $self = shift;
168  
169 2118 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2118 2118
170 {
171 0 warn "Calling Locale() with an argument to set the locale is deprecated. Please use SetLocale(locale) instead.\n";
172  
173 0 return $self->SetLocale(@_);
174 }
175  
176 2118 return $self->{locale};
177 }
178  
179 sub SetLocale
180 499 ▶{
Subroutine SetLocale: called CC=3 SCAR=12.3
Pod documented
181 499 my $self = shift;
182 499 my $locale = shift;
183  
184 499 ▶ croak "No locale passed to SetLocale()" if not defined $locale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $locale 0 499 499
185  
186 499 my $locale_return = POSIX::setlocale(&POSIX::LC_TIME, $locale);
187  
188 499 ▶ if( defined $locale_return )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $locale_return) 0 499 499
189 {
190 0 $self->{locale} = $locale;
191 0 $self->{locale_realname} = $locale_return;
192  
193 0 return $self->{locale};
194 }
195  
196 499 print STDERR "Locale $locale does not seem to be implemented on this system, keeping locale ".$self->{locale}."\n";
197 499 return undef;
198 }
199  
200
201 sub LocaleRealName
202 0 ▶{
Subroutine LocaleRealName: not called CC=2 SCAR=17.9
Pod undocumented
203 0 my $self = shift;
204  
205 0 ▶ return $self->{locale_realname} || $self->DEFAULT_LOCALE();
Condition0%0/3
$self->{'locale_realname'} || $self->DEFAULT_LOCALE
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'locale_realname'} || $self->DEFAULT_LOCALE
$self->{'locale_realname'} $self->DEFAULT_LOCALE
Truth table0%0/3
A || B
A= $self->{'locale_realname'}B= $self->DEFAULT_LOCALE
ABresult
1X1
011
000
206 }
207
208 #Time Conversion and info methods
209  
210 sub TimeZoneName
211 0 ▶{
Subroutine TimeZoneName: not called CC=1 SCAR=6.9
Pod undocumented
212 0 my $self = shift;
213  
214 0 local $ENV{'TZ'} = $self->TimeZone();
215 0 local $ENV{'LC_TIME'} = $self->Locale();
216  
217 #Old code.
218 #my ($std,$dst) = POSIX::tzname();
219 #return $std." / ".$dst;
220  
221 0 return strftime("%Z", localtime($self->{epoch}) );
222 }
223  
224 sub LocalTime
225 0 ▶{
Subroutine LocalTime: not called CC=1 SCAR=6.9
Pod undocumented
226 0 my $self = shift;
227  
228 0 local $ENV{'TZ'} = $self->TimeZone();
229 0 local $ENV{'LC_TIME'} = $self->Locale();
230  
231 0 return localtime($self->{epoch});
232 }
233  
234  
235 sub TimeFormat
236 402 ▶{
Subroutine TimeFormat: called CC=2 SCAR=7.1
Pod undocumented
237 402 my $self = shift;
238 402 my $format_string = shift;
239  
240 402 local $ENV{'TZ'} = $self->TimeZone();
241 402 local $ENV{'LC_TIME'} = $self->Locale();
242
243 402 ▶ $format_string ||= $self->DEFAULT_FORMAT_STRING();
Condition33%1/3
$format_string ||= $self->DEFAULT_FORMAT_STRING
l!l&&r!l&&!r
04020
MC/DC0%0/2
$format_string ||= $self->DEFAULT_FORMAT_STRING
$format_string $self->DEFAULT_FORMAT_STRING
Truth table33%1/3
A ||= B
A= $format_stringB= $self->DEFAULT_FORMAT_STRING
ABresult
1X1
011
000
244  
245 402 return strftime($format_string, localtime($self->{epoch}));
246 }
247  
248  
249 sub GmtTime
250 0 ▶{
Subroutine GmtTime: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my $self = shift;
252  
253 0 local $ENV{'TZ'} = $self->TimeZone();
254 0 local $ENV{'LC_TIME'} = $self->Locale();
255  
256 0 return gmtime($self->{epoch});
257 }
258  
259 sub UtcTime
260 0 ▶{
Subroutine UtcTime: not called CC=1 SCAR=6.9
Pod undocumented
261 0 my $self = shift;
262  
263 0 local $ENV{'TZ'} = $self->TimeZone();
264 0 local $ENV{'LC_TIME'} = $self->Locale();
265  
266 0 return gmtime($self->{epoch});
267 }
268  
269  
270 #Idea and base code for this function from:
271 # Larry Rosler, February 13, 1999, Thanks Larry! -<bbeausej@pobox.com>
272  
273 sub GmtOffset
274 0 ▶{
Subroutine GmtOffset: not called CC=2 SCAR=17.9
Pod undocumented
275 0 my $self = shift;
276  
277 0 local $ENV{'TZ'} = $self->TimeZone();
278 0 local $ENV{'LC_TIME'} = $self->Locale();
279  
280 #Old code.
281 #use Time::Local;
282 #my $gmt_time = timegm( gmtime $self->{epoch} );
283 #my $local_time = timelocal( gmtime $self->{epoch} );
284  
285  
286 0 my $now = $self->Epoch();
287
288 0 my ($l_min, $l_hour, $l_year, $l_yday) = (localtime $now)[1, 2, 5, 7];
289 0 my ($g_min, $g_hour, $g_year, $g_yday) = (gmtime $now)[1, 2, 5, 7];
290  
291 0 ▶ return (($l_min - $g_min)/60 + $l_hour - $g_hour + 24 * ($l_year - $g_year || $l_yday - $g_yday)) * 3600;
Condition0%0/3
$l_year - $g_year || $l_yday - $g_yday
l!l&&r!l&&!r
000
MC/DC0%0/2
$l_year - $g_year || $l_yday - $g_yday
$l_year - $g_year $l_yday - $g_yday
Truth table0%0/3
A || B
A= $l_year - $g_yearB= $l_yday - $g_yday
ABresult
1X1
011
000
292 }
293  
294  
295 #Useful methods
296 sub MonthName
297 0 ▶{
Subroutine MonthName: not called CC=1 SCAR=6.9
Pod undocumented
298 0 my $self = shift;
299  
300 0 local $ENV{'TZ'} = $self->TimeZone();
301 0 local $ENV{'LC_TIME'} = $self->Locale();
302  
303 0 return strftime('%B', localtime($self->{epoch}));
304 }
305  
306 sub WeekDay
307 0 ▶{
Subroutine WeekDay: not called CC=1 SCAR=6.9
Pod undocumented
308 0 my $self = shift;
309  
310 0 local $ENV{'TZ'} = $self->TimeZone();
311 0 local $ENV{'LC_TIME'} = $self->Locale();
312  
313 0 return strftime('%u', localtime($self->{epoch}));
314 }
315  
316 sub WeekDayName
317 0 ▶{
Subroutine WeekDayName: not called CC=1 SCAR=6.9
Pod undocumented
318 0 my $self = shift;
319  
320 0 local $ENV{'TZ'} = $self->TimeZone();
321 0 local $ENV{'LC_TIME'} = $self->Locale();
322  
323 0 return strftime('%A', localtime($self->{epoch}));
324 }
325  
326 sub FirstWeekDayOfMonth
327 0 ▶{
Subroutine FirstWeekDayOfMonth: not called CC=1 SCAR=6.9
Pod undocumented
328 0 my $self = shift;
329  
330 0 local $ENV{'TZ'} = $self->TimeZone();
331 0 local $ENV{'LC_TIME'} = $self->Locale();
332
333 0 return (($self->WeekDay() - $self->Day() % 7) + 8) % 7;
334 }
335  
336 sub WeekOfMonth
337 0 ▶{
Subroutine WeekOfMonth: not called CC=1 SCAR=6.9
Pod undocumented
338 0 my $self = shift;
339  
340 0 local $ENV{'TZ'} = $self->TimeZone();
341 0 local $ENV{'LC_TIME'} = $self->Locale();
342  
343 0 return int(($self->Day() + $self->FirstWeekDayOfMonth() - 1) / 7) + 1;
344 }
345
346  
347 sub DaysInMonth
348 0 ▶{
Subroutine DaysInMonth: not called CC=3 SCAR=24.8
Pod undocumented
349 0 my $self = shift;
350  
351 0 local $ENV{'TZ'} = $self->TimeZone();
352 0 local $ENV{'LC_TIME'} = $self->Locale();
353  
354 0 my $month = $self->Month() - 1;
355  
356 0 ▶ if($month == 1) #Feb
Branch: 1 branch
BranchTrueFalseTotal
if ($month == 1) { } 0 0 0
357 {
358 0 ▶ return 29 if $self->IsLeapYear();
Branch: 1 branch
BranchTrueFalseTotal
if $self->IsLeapYear 0 0 0
359 0 return 28;
360 }
361 else
362 {
363 0 return $DAYS_IN_MONTH->{$month};
364 }
365 }
366
367 sub DayLightSavings
368 465 ▶{
Subroutine DayLightSavings: called CC=1 SCAR=0.0
Pod undocumented
369 465 my $self = shift;
370  
371 465 local $ENV{'TZ'} = $self->TimeZone();
372 465 local $ENV{'LC_TIME'} = $self->Locale();
373  
374 465 my @self_localtime = localtime($self->{epoch});
375  
376 465 return $self_localtime[8];
377 }
378  
379 sub DayOfYear
380 0 ▶{
Subroutine DayOfYear: not called CC=1 SCAR=6.9
Pod undocumented
381 0 my $self = shift;
382
383 0 local $ENV{'TZ'} = $self->TimeZone();
384 0 local $ENV{'LC_TIME'} = $self->Locale();
385
386 0 my @self_localtime = localtime($self->{epoch});
387  
388 0 return $self_localtime[7];
389 }
390  
391 sub DaysInYear
392 0 ▶{
Subroutine DaysInYear: not called CC=3 SCAR=24.8
Pod undocumented
393 0 my $self = shift;
394  
395 0 local $ENV{'TZ'} = $self->TimeZone();
396 0 local $ENV{'LC_TIME'} = $self->Locale();
397  
398 0 ▶ return 365 if !$self->IsLeapYear();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->IsLeapYear 0 0 0
399 0 ▶ return 366 if $self->IsLeapYear();
Branch: 1 branch
BranchTrueFalseTotal
if $self->IsLeapYear 0 0 0
400 }
401  
402 sub DaysLeftInYear
403 0 ▶{
Subroutine DaysLeftInYear: not called CC=1 SCAR=6.9
Pod undocumented
404 0 my $self = shift;
405  
406 0 local $ENV{'TZ'} = $self->TimeZone();
407 0 local $ENV{'LC_TIME'} = $self->Locale();
408  
409 0 my $days = $self->DaysInYear();
410 0 my $day = $self->DayOfYear();
411  
412 0 return $days - $day;
413 }
414  
415 sub LastDayOfMonth
416 0 ▶{
Subroutine LastDayOfMonth: not called CC=2 SCAR=17.9
Pod undocumented
417 0 my $self = shift;
418  
419 0 local $ENV{'TZ'} = $self->TimeZone();
420 0 local $ENV{'LC_TIME'} = $self->Locale();
421  
422 0 ▶ if($self->Day() >= $self->DaysInMonth())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Day >= $self->DaysInMonth) 0 0 0
423 {
424 0 return 1;
425 }
426
427 }
428  
429 sub IsLeapYear
430 0 ▶{
Subroutine IsLeapYear: not called CC=4 SCAR=30.0
Pod undocumented
431 0 my $self = shift;
432  
433 0 local $ENV{'TZ'} = $self->TimeZone();
434 0 local $ENV{'LC_TIME'} = $self->Locale();
435  
436 0 my $year = $self->Year();
437
438 0 ▶ return 1 if( !($year % 400) );
Branch: 1 branch
BranchTrueFalseTotal
unless $year % 400 0 0 0
439 0 ▶ return 1 if( !($year %4) && ($year % 100) );
Branch: 1 branch
BranchTrueFalseTotal
if not $year % 4 and $year % 100 0 0 0
Condition0%0/3
not $year % 4 and $year % 100
!ll&&!rl&&r
000
MC/DC0%0/2
not $year % 4 and $year % 100
not $year % 4 $year % 100
Truth table0%0/3
not(A) and B
A= not $year % 4B= $year % 100
ABresult
0X0
100
111
440 0 return 0;
441 }
442  
443 sub IntuitiveDay
444 0 ▶{
Subroutine IntuitiveDay: not called CC=2 SCAR=17.9
Pod undocumented
445 0 my $self = shift;
446 0 my $intuitive_day = shift;
447  
448 0 ▶ if($intuitive_day)
Branch: 1 branch
BranchTrueFalseTotal
if ($intuitive_day) 0 0 0
449 {
450 0 $self->{_intuitive_day} = $intuitive_day;
451 }
452 0 return $self->{_intuitive_day};
453 }
454  
455 sub IntuitiveHour
456 0 ▶{
Subroutine IntuitiveHour: not called CC=2 SCAR=17.9
Pod undocumented
457 0 my $self = shift;
458 0 my $intuitive_hour = shift;
459
460 0 ▶ if($intuitive_hour)
Branch: 1 branch
BranchTrueFalseTotal
if ($intuitive_hour) 0 0 0
461 {
462 0 $self->{_intuitive_hour} = $intuitive_hour;
463 }
464 0 return $self->{_intuitive_hour};
465 }
466  
467 sub Array2Epoch
468 357 ▶{
Subroutine Array2Epoch: called CC=7 SCAR=19.7
Pod undocumented
469 357 my $self = shift;
470 357 my $input = shift;
471  
472 357 my ($y,$m,$d,$h,$mm,$ss) = @{$input}[0,1,2,3,4,5];
473  
474 357 local $ENV{'TZ'} = $self->TimeZone();
475 357 local $ENV{'LC_TIME'} = $self->Locale();
476  
477 357 ▶ return mktime(
Condition75%9/12
$ss || 0
l!l
190167
$mm || 0
l!l
184173
$h || 0
l!l
27582
$d || 1
l!l
3570
$m || 1
l!l
3570
$y || 2000
l!l
3570
MC/DC100%1/1
$ss || 0
$ss
MC/DC100%1/1
$mm || 0
$mm
MC/DC100%1/1
$h || 0
$h
MC/DC0%0/1
$d || 1
$d
MC/DC0%0/1
$m || 1
$m
MC/DC0%0/1
$y || 2000
$y
Truth table100%2/2
A
A= $ss
Aresult
11
00
Truth table100%2/2
A
A= $mm
Aresult
11
00
Truth table100%2/2
A
A= $h
Aresult
11
00
Truth table50%1/2
A
A= $d
Aresult
11
00
Truth table50%1/2
A
A= $m
Aresult
11
00
Truth table50%1/2
A
A= $y
Aresult
11
00
478 $ss || 0,
479 $mm || 0,
480 $h || 0,
481 $d || 1,
482 ($m || 1)-1,
483 ($y || 2000)-1900,
484 0,
485 0,
486 -1);
487 }
488  
489  
490 #Oveload methods.
491  
492 402 ▶sub AsScalar { return shift->TimeFormat(shift); }
Subroutine AsScalar: called CC=1 SCAR=0.0
Pod undocumented
493 0 ▶sub AsNumber { return shift->{epoch}; }
Subroutine AsNumber: not called CC=1 SCAR=6.9
Pod undocumented
494  
495 sub AsArray
496 151 ▶{
Subroutine AsArray: called CC=1 SCAR=0.0
Pod undocumented
497 151 my $self = shift;
498  
499 151 local $ENV{'TZ'} = $self->TimeZone();
500 151 local $ENV{'LC_TIME'} = $self->Locale();
501  
502  
503 151 my ($ss,$mm,$h,$d,$m,$y) = localtime($self->{epoch});
504 151 $y += 1900;
505 151 $m += 1;
506  
507 151 return [$y,$m,$d,$h,$mm,$ss];
508 }
509  
510 sub AsHash
511 0 ▶{
Subroutine AsHash: not called CC=1 SCAR=6.9
Pod undocumented
512 0 my $self = shift;
513  
514 0 my $self_array = $self->AsArray();
515  
516 return {
517 0 year => $self_array->[0],
518 month => $self_array->[1],
519 day => $self_array->[2],
520 hour => $self_array->[3],
521 min => $self_array->[4],
522 sec => $self_array->[5],
523 };
524 }
525  
526  
527 sub Add
528 142 ▶{
Subroutine Add: called CC=23 SCAR=45.5
Pod undocumented
529 142 my ($self, $delta) = @_;
530  
531 142 ▶ if(!ref($delta))
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $delta) { } 0 142 142
elsif ($delta->isa($self->DELTA_CLASS)) { } 142 0 142
532 {
533 0 $delta = $self->DELTA_CLASS()->new([0,0,0,0,0,$delta]);
534 0 return $self + $delta;
535 }
536 elsif($delta->isa($self->DELTA_CLASS()))
537 {
538 142 local $ENV{'TZ'} = $self->TimeZone();
539 142 local $ENV{'LC_TIME'} = $self->Locale();
540  
541  
542 142 my $epoch = $self->{epoch};
543  
544 142 my $newdate = ref($self)->new({ date => $epoch, time_zone => $self->TimeZone() });
545  
546 142 my $self_array = $newdate->AsArray();
547 #Take care of the months.
548 142 $self_array->[1] += $delta->Months();
549  
550 142 my $years = floor(($self_array->[1]-1)/12);
551 142 $self_array->[1] -= 12*$years;
552  
553 #Take care of the years.
554 142 $self_array->[0] += $years;
555  
556 142 my $posix_date = ref($self)->new({ date => $self_array,
557 time_zone => $self->TimeZone(),
558 });
559  
560 142 ▶ if($self->INTUITIVE_MONTH_CALCULATIONS())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->INTUITIVE_MONTH_CALCULATIONS) 51 91 142
561 {
562 51 ▶ if((($self->Month() + $delta->Months() - 1) % 12 + 1) != $posix_date->Month())
Branch: 1 branch
BranchTrueFalseTotal
if (($self->Month + $delta->Months - 1) % 12 + 1 != $posix_date->Month) { } 3 48 51
563 {
564 3 my $compensation_seconds = 86400 * $posix_date->Day();
565 3 my $compensated_epoch = $posix_date->Epoch();
566
567 3 $compensated_epoch -= $compensation_seconds;
568
569 3 $posix_date->Epoch($compensated_epoch);
570
571 3 ▶ $posix_date->{_intuitive_day} = $self->{_intuitive_day} || $self->Day();
Condition33%1/3
$self->{'_intuitive_day'} || $self->Day
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->{'_intuitive_day'} || $self->Day
$self->{'_intuitive_day'} $self->Day
Truth table33%1/3
A || B
A= $self->{'_intuitive_day'}B= $self->Day
ABresult
1X1
011
000
572 }
573 else
574 {
575 48 ▶ if($self->{_intuitive_day})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_intuitive_day"}) 3 45 48
576 {
577 3 my $lastdayofmonth = $self->{_intuitive_day};
578 3 my $compensated_seconds = 86400 * ($lastdayofmonth - $posix_date->Day());
579 3 ▶ if($compensated_seconds > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($compensated_seconds > 0) 3 0 3
580 {
581 3 my $epoch = $posix_date->Epoch();
582 3 $epoch += $compensated_seconds;
583 3 $posix_date->Epoch($epoch);
584 }
585
586 3 ▶ if($self->{_intuitive_day} > $lastdayofmonth)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_intuitive_day"} > $lastdayofmonth) 0 3 3
587 {
588 0 $posix_date->{_intuitive_day} = $self->{_intuitive_day};
589 }
590
591 }
592 }
593 }
594  
595 #Take care of the seconds
596 142 my $posix_epoch = $posix_date->Epoch();
597 142 $posix_epoch += $delta->Seconds();
598 142 $posix_date->Epoch($posix_epoch);
599  
600  
601 142 my $adjustment_epoch = $posix_date->Epoch();
602 142 my $add_intuitive_hour = 0;
603 142 my $intuitive_hour;
604  
605 142 ▶ if($posix_date->DayLightSavings() && !$self->DayLightSavings())
Branch: 2 branches
BranchTrueFalseTotal
if ($posix_date->DayLightSavings and not $self->DayLightSavings) { } 0 142 142
elsif (not $posix_date->DayLightSavings and $self->DayLightSavings) { } 0 142 142
Condition33%1/3
$posix_date->DayLightSavings and not $self->DayLightSavings
!ll&&!rl&&r
14200
MC/DC0%0/2
$posix_date->DayLightSavings and not $self->DayLightSavings
$posix_date->DayLightSavings not $self->DayLightSavings
Truth table33%1/3
A and not(B)
A= $posix_date->DayLightSavingsB= not $self->DayLightSavings
ABresult
0X0
100
111
606 {
607 0 my $posix_hour = $posix_date->Hour();
608 0 $posix_hour -= 1;
609 0 $intuitive_hour = $posix_hour;
610  
611 0 ▶ if($self->INTUITIVE_DST_ADJUSTMENTS())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->INTUITIVE_DST_ADJUSTMENTS) 0 0 0
612 {
613 0 $adjustment_epoch -= 3600;
614 0 $posix_date->Epoch($adjustment_epoch);
615  
616 0 ▶ $posix_hour = 0 if $posix_hour == 24;
Branch: 1 branch
BranchTrueFalseTotal
if $posix_hour == 24 0 0 0
617 0 ▶ if($posix_date->Hour() != $posix_hour)
Branch: 1 branch
BranchTrueFalseTotal
if ($posix_date->Hour != $posix_hour) 0 0 0
618 {
619 0 $add_intuitive_hour = 1;
620 0 $adjustment_epoch += 3600;
621 0 $posix_date->Epoch($adjustment_epoch);
622 }
623 }
624 }
625 ▶ elsif(!$posix_date->DayLightSavings() && $self->DayLightSavings())
Condition33%1/3
not $posix_date->DayLightSavings and $self->DayLightSavings
!ll&&!rl&&r
01420
MC/DC0%0/2
not $posix_date->DayLightSavings and $self->DayLightSavings
not $posix_date->DayLightSavings $self->DayLightSavings
Truth table33%1/3
not(A) and B
A= not $posix_date->DayLightSavingsB= $self->DayLightSavings
ABresult
0X0
100
111
626 {
627  
628 0 my $posix_hour = $posix_date->Hour();
629 0 $posix_hour += 1;
630 0 $intuitive_hour = $posix_hour;
631  
632 0 ▶ if($self->INTUITIVE_DST_ADJUSTMENTS())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->INTUITIVE_DST_ADJUSTMENTS) 0 0 0
633 {
634 0 $adjustment_epoch += 3600;
635 0 $posix_date->Epoch($adjustment_epoch);
636  
637 0 ▶ $posix_hour = 0 if $posix_hour == 24;
Branch: 1 branch
BranchTrueFalseTotal
if $posix_hour == 24 0 0 0
638 0 ▶ if($posix_date->Hour() != $posix_hour)
Branch: 1 branch
BranchTrueFalseTotal
if ($posix_date->Hour != $posix_hour) 0 0 0
639 {
640 0 $add_intuitive_hour = 1;
641 0 $adjustment_epoch -= 3600;
642 0 $posix_date->Epoch($adjustment_epoch);
643 }
644 }
645 }
646  
647 142 ▶ if($self->INTUITIVE_TIME_CALCULATIONS())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->INTUITIVE_TIME_CALCULATIONS) 51 91 142
648 {
649 51 ▶ if($add_intuitive_hour)
Branch: 1 branch
BranchTrueFalseTotal
if ($add_intuitive_hour) 0 51 51
650 {
651 0 $posix_date->{_intuitive_hour} = $intuitive_hour;
652 }
653  
654 51 ▶ if(defined $self->{_intuitive_hour})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_intuitive_hour"}) 0 51 51
655 {
656 0 my $hour = $posix_date->Hour();
657 0 my $intuitive_epoch = $posix_date->Epoch();
658  
659 0 ▶ if($hour > $self->{_intuitive_hour})
Branch: 1 branch
BranchTrueFalseTotal
if ($hour > $self->{"_intuitive_hour"}) 0 0 0
660 {
661 0 $intuitive_epoch -= 3600;
662 0 $posix_date->Epoch($intuitive_epoch);
663 }
664 #elsif($hour < $self->{_intuitive_hour})
665 #{
666 # print STDERR "Intuitive Adjust +1 hour\n";
667 # $intuitive_epoch += 3600;
668 # $posix_date->Epoch($intuitive_epoch);
669 #}
670 }
671 }
672  
673 142 return $posix_date;
674  
675 }
676 else
677 {
678 0 croak "Trying to add/substract an unknown object to a Date::Handler";
679 }
680 }
681  
682  
683 sub Sub
684 47 ▶{
Subroutine Sub: called CC=8 SCAR=27.2
Pod undocumented
685 47 my ($self, $delta) = @_;
686  
687 47 ▶ if(!ref($delta))
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $delta) { } 0 47 47
elsif ($delta->isa($self->DELTA_CLASS)) { } 34 13 47
elsif ($delta->isa('Date::Handler')) { } 13 0 13
688 {
689 0 $delta = $self->DELTA_CLASS()->new([0,0,0,0,0,$delta]);
690 0 return $self - $delta;
691 }
692 elsif($delta->isa($self->DELTA_CLASS()))
693 {
694 34 return $self->Add(-$delta);
695 }
696 elsif($delta->isa('Date::Handler'))
697 {
698  
699 13 my $seconds = $self->Epoch() - $delta->Epoch();
700  
701 13 ▶ if(($self->DayLightSavings() && !$delta->DayLightSavings()) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->DayLightSavings and not $delta->DayLightSavings or not $self->DayLightSavings and $delta->DayLightSavings) 0 13 13
Condition33%3/9
$self->DayLightSavings and not $delta->DayLightSavings or not $self->DayLightSavings and $delta->DayLightSavings
l!l&&r!l&&!r
0013
$self->DayLightSavings and not $delta->DayLightSavings
!ll&&!rl&&r
1300
not $self->DayLightSavings and $delta->DayLightSavings
!ll&&!rl&&r
0130
MC/DC0%0/4
$self->DayLightSavings and not $delta->DayLightSavings or not $self->DayLightSavings and $delta->DayLightSavings
$self->DayLightSavings not $delta->DayLightSavings not $self->DayLightSavings $delta->DayLightSavings
Truth table14%1/7
A and not(B) or not(C) and D
A= $self->DayLightSavingsB= not $delta->DayLightSavingsC= not $self->DayLightSavingsD= $delta->DayLightSavings
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
702 !$self->DayLightSavings() && $delta->DayLightSavings())
703 {
704 0 $seconds += 3600;
705 }
706  
707 13 return $self->DELTA_CLASS()->new($seconds);
708 }
709 else
710 {
711 0 croak "Cannot substract something else than a ".$self->DELTA_CLASS()." or Date::Handler or constant from a Date::Handler";
712 }
713 }
714  
715  
716 sub Cmp
717 0 ▶{
Subroutine Cmp: not called CC=4 SCAR=30.0
Pod undocumented
718 0 my ($self, $date, $reverse) = @_;
719  
720 0 my $cmp_date;
721  
722 0 ▶ if(!ref($date))
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $date) { } 0 0 0
elsif ($date->isa('Date::Handler')) { } 0 0 0
elsif ($date->isa($self->DELTA_CLASS)) { } 0 0 0
723 {
724 0 $cmp_date = $date;
725 }
726 elsif($date->isa('Date::Handler'))
727 {
728 0 $cmp_date = $date->{epoch};
729 }
730 elsif($date->isa($self->DELTA_CLASS()))
731 {
732 0 croak "Cannot compare a Date::Handler to a Delta.";
733 }
734 else
735 {
736 0 croak "Trying to compare a Date::Handler to an unknown object.";
737 }
738
739 0 return $self->{epoch} <=> $cmp_date;
740 }
741  
742 sub Incr
743 0 ▶{
Subroutine Incr: not called CC=1 SCAR=6.9
Pod undocumented
744 0 my ($self) = @_;
745  
746 0 my $epoch = $self->{epoch};
747 0 $epoch++;
748  
749 0 return ref($self)->new({ date => $epoch, time_zone => $self->TimeZone() });
750 }
751  
752  
753 sub AllInfo
754 0 ▶{
Subroutine AllInfo: not called CC=1 SCAR=6.9
Pod undocumented
755 0 my $self = shift;
756 0 my $out_string;
757  
758 0 local $ENV{'TZ'} = $self->TimeZone();
759 0 local $ENV{'LC_TIME'} = $self->Locale();
760  
761 0 $out_string .= "LocalTime: ".$self->LocalTime()."\n";
762 0 $out_string .= "TimeFormat: ".$self->TimeFormat()."\n";
763 0 $out_string .= "Epoch: ".$self->Epoch()."\n";
764 0 $out_string .= "Locale: ".$self->Locale()."\n";
765 0 $out_string .= "LocaleRealName: ".$self->LocaleRealName()."\n";
766 0 $out_string .= "TimeZone: ".$self->TimeZone()." (".$self->TimeZoneName().")\n";
767 0 $out_string .= "DayLightSavings: ".$self->DayLightSavings()."\n";
768 0 $out_string .= "GMT Time: ".$self->GmtTime()."\n";
769 0 $out_string .= "GmtOffset: ".$self->GmtOffset()." (".($self->GmtOffset() / 60 / 60).")\n";
770 0 $out_string .= "Year: ".$self->Year()."\n";
771 0 $out_string .= "Month: ".$self->Month()."\n";
772 0 $out_string .= "Day: ".$self->Day()."\n";
773 0 $out_string .= "Hour: ".$self->Hour()."\n";
774 0 $out_string .= "Min: ".$self->Min()."\n";
775 0 $out_string .= "Sec: ".$self->Sec()."\n";
776 0 $out_string .= "WeekDay: ".$self->WeekDay()."\n";
777 0 $out_string .= "WeekDayName: ".$self->WeekDayName()."\n";
778 0 $out_string .= "FirstWeekDayOfMonth: ".$self->FirstWeekDayOfMonth()."\n";
779 0 $out_string .= "WeekOfMonth: ".$self->WeekOfMonth()."\n";
780 0 $out_string .= "DayOfYear: ".$self->DayOfYear()."\n";
781 0 $out_string .= "MonthName: ".$self->MonthName()."\n";
782 0 $out_string .= "DaysInMonth: ".$self->DaysInMonth()."\n";
783 0 $out_string .= "Leap Year: ".$self->IsLeapYear()."\n";
784 0 $out_string .= "DaysInYear: ".$self->DaysInYear()."\n";
785 0 $out_string .= "DaysLeftInYear: ".$self->DaysLeftInYear()."\n";
786 0 $out_string .= "Intuitive Day: ".$self->IntuitiveDay()."\n";
787 0 $out_string .= "Intuitive Hour: ".$self->IntuitiveHour()."\n";
788 0 $out_string .= "\n\n";
789 0 return $out_string;
790 }
791  
792 8 666;
793 __END__
« Handler/Delta.pm ↑ Summary Handler/Range.pm »