Labyrinth/DTUtils.pm

filter: Statementstmt 100% 155 / 155 Branchbran 92.5% 50 / 54 Conditioncond 78.0% 39 / 50 MC/DCmcdc 60.0% 18 / 30 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 91.3% 285 / 312 CC 49 SCAR 22.0
CC
49
Cov
94%
unformatDate
17.1
YearSelect
11.7
_ext
8.0
CRAP 9.1
« Labyrinth/Constraints.pm ↑ Summary Labyrinth/Phrasebook.pm »
1 package Labyrinth::DTUtils;
2  
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 $VERSION = '5.32';
8  
9 =head1 NAME
10  
11 Labyrinth::DTUtils - Date & Time Utilities for Labyrinth
12  
13 =head1 SYNOPSIS
14  
15 use Labyrinth::DTUtils;
16  
17 =head1 DESCRIPTION
18  
19 Various date & time utilities.
20  
21 =head1 EXPORT
22  
23 everything
24  
25 =cut
26  
27 # -------------------------------------
28 # Export Details
29  
30 13 require Exporter;
31 13 @ISA = qw(Exporter);
32  
33 13 %EXPORT_TAGS = (
34 'all' => [ qw(
35 DaySelect MonthSelect YearSelect PeriodSelect
36 formatDate unformatDate isMonth
37 ) ]
38 );
39  
40 13 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
41 13 @EXPORT = ( @{ $EXPORT_TAGS{'all'} } );
42  
43 #############################################################################
44 #Libraries
45 #############################################################################
46  
47 13 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 13 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 13 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 13 ▶use Labyrinth::MLUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 13 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 #############################################################################
54 #Variables
55 #############################################################################
56  
57 13 my @months = (
58 { 'id' => 1, 'value' => "January", },
59 { 'id' => 2, 'value' => "February", },
60 { 'id' => 3, 'value' => "March", },
61 { 'id' => 4, 'value' => "April", },
62 { 'id' => 5, 'value' => "May", },
63 { 'id' => 6, 'value' => "June", },
64 { 'id' => 7, 'value' => "July", },
65 { 'id' => 8, 'value' => "August", },
66 { 'id' => 9, 'value' => "September", },
67 { 'id' => 10, 'value' => "October", },
68 { 'id' => 11, 'value' => "November", },
69 { 'id' => 12, 'value' => "December" },
70 );
71  
72 13 my @dotw = ( "Sunday", "Monday", "Tuesday", "Wednesday",
73 "Thursday", "Friday", "Saturday" );
74  
75 13 my @days = map {{'id'=>$_,'value'=> $_}} (1..31);
76 13 my @periods = (
77 {act => 'evnt-month', value => 'Month'},
78 {act => 'evnt-week', value => 'Week'},
79 {act => 'evnt-day', value => 'Day'}
80 );
81  
82 13 my %formats = (
83 1 => 'YYYY',
84 2 => 'MONTH YYYY',
85 3 => 'DD/MM/YYYY',
86 4 => 'DABV MABV DD TIME24 YYYY',
87 5 => 'DAY, DD MONTH YYYY',
88 6 => 'DAY, DDEXT MONTH YYYY',
89 7 => 'DAY, DD MONTH YYYY (TIME12)',
90 8 => 'DAY, DDEXT MONTH YYYY (TIME12)',
91 9 => 'YYYY/MM/DD',
92 10 => 'DDEXT MONTH YYYY',
93 11 => 'YYYYMMDDThhmmss', # iCal date string
94 12 => 'YYYY-MM-DDThh:mm:ssZ', # RSS date string
95 13 => 'YYYYMMDD', # backwards date
96 14 => 'DABV, DDEXT MONTH YYYY',
97 15 => 'DD MABV YYYY',
98 16 => 'DABV, dd MABV YYYY hh:mm:ss TZ', # RFC-822 date string
99 17 => 'DAY, DD MONTH YYYY hh:mm:ss',
100 18 => 'DD/MM/YYYY hh:mm:ss',
101 19 => 'DDEXT MONTH YYYY',
102 20 => 'DABV, DD MABV YYYY hh:mm:ss',
103 21 => 'YYYY-MM-DD hh:mm:ss',
104 22 => 'YYYYMMDDhhmm',
105 );
106  
107 13 my %unformats = (
108 11 => '(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})', # iCal date string
109 12 => '(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z', # ISO 8601 date string
110 13 => '(\d{4})(\d{2})(\d{2})', # backwards date
111 22 => '(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})',
112 );
113  
114 # decrees whether the date format above should be UTC
115 # time based, or allow for any Summer Time variations.
116 13 my %zonetime = (12 => 1, 16 => 1);
117  
118 #############################################################################
119 #Subroutines
120 #############################################################################
121  
122 =head1 FUNCTIONS
123  
124 =head2 Dropdown Boxes
125  
126 =over 4
127  
128 =item DaySelect($opt,$blank)
129  
130 Provides a Day dropdown selection box.
131  
132 The option $opt allows the given day (numerical 1 - 31) to be the selected
133 option in the dropdown. If blank is true, a 'Select Day' option is added as
134 the first option to the dropdown.
135  
136 =item MonthSelect($opt,$blank)
137  
138 Provides a Month dropdown selection box.
139  
140 The option $opt allows the given month (numerical 1 - 12) to be the selected
141 option in the dropdown. If blank is true, a 'Select Month' option is added as
142 the first option to the dropdown.
143  
144 =item YearSelect($opt,$range,$blank,$dates)
145  
146 Provides a Year dropdown selection box.
147  
148 The option $opt allows the given month (numerical 1 - 12) to be the selected
149 option in the dropdown. If blank is true, a 'Select Month' option is added as
150 the first option to the dropdown.
151  
152 If is specified, then the following criteria is used:
153  
154 0 - default
155 1 - given dates, see $dates list
156 2 - oldest year to current year
157 3 - current year to future year
158  
159 For oldest year, this is determined by the configuration setting
160 'year_past_offset' or 'year_past'. For the future year, this is determined by
161 the configuration setting 'year_future_offset'.
162  
163 If the range is set to 1, the list of dates given in the $dates array
164 reference will be used.
165  
166 =item PeriodSelect($opt,$blank)
167  
168 Provides a Period dropdown selection box.
169  
170 The option $opt allows the given period to be the selected option in the
171 dropdown. If blank is true, a 'Select Period' option is added as the first
172 option to the dropdown.
173  
174 Current valid periods are:
175  
176 opt value
177 -------------------
178 evnt-month Month
179 evnt-week Week
180 evnt-day Day
181  
182 =back
183  
184 =cut
185  
186 3 ▶sub DaySelect {
Subroutine DaySelect: called CC=3 SCAR=11.0
Pod documented
187 3 my ($opt,$blank) = @_;
188 3 my @list = @days;
189 3 ▶ unshift @list, {id=>0,value=>'Select Day'} if(defined $blank && $blank == 1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $blank and $blank == 1 1 2 3
Condition66%2/3
defined $blank and $blank == 1
!ll&&!rl&&r
201
MC/DC50%1/2
defined $blank and $blank == 1
defined $blank $blank == 1
Truth table66%2/3
A and B
A= defined $blankB= $blank == 1
ABresult
0X0
100
111
190 3 DropDownRows($opt,'day','id','value',@list);
191 }
192  
193 3 ▶sub MonthSelect {
Subroutine MonthSelect: called CC=3 SCAR=11.0
Pod documented
194 3 my ($opt,$blank) = @_;
195 3 my @list = @months;
196 3 ▶ unshift @list, {id=>0,value=>'Select Month'} if(defined $blank && $blank == 1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $blank and $blank == 1 1 2 3
Condition66%2/3
defined $blank and $blank == 1
!ll&&!rl&&r
201
MC/DC50%1/2
defined $blank and $blank == 1
defined $blank $blank == 1
Truth table66%2/3
A and B
A= defined $blankB= $blank == 1
ABresult
0X0
100
111
197 3 DropDownRows($opt,'month','id','value',@list);
198 }
199  
200 6 ▶sub YearSelect {
Subroutine YearSelect: called CC=11 SCAR=24.6
Pod documented
201 6 my ($opt,$range,$blank,$dates) = @_;
202 6 my $year = formatDate(1);
203
204 6 ▶ my $past_offset = $settings{year_past_offset} || 0;
Condition50%1/2
$Labyrinth::DTUtils::settings{'year_past_offset'} || 0
l!l
06
MC/DC0%0/1
$Labyrinth::DTUtils::settings{'year_past_offset'} || 0
$Labyrinth::DTUtils::settings{'year_past_offset'}
Truth table50%1/2
A
A= $Labyrinth::DTUtils::settings{'year_past_offset'}
Aresult
11
00
205 6 ▶ my $future_offset = defined $settings{year_future_offset} ? $settings{year_future_offset} : 4;
Branch: 1 branch
BranchTrueFalseTotal
defined $Labyrinth::DTUtils::settings{'year_future_offset'} ? : 0 6 6
206 6 ▶ my $past = $past_offset ? $year - $past_offset : $settings{year_past};
Branch: 1 branch
BranchTrueFalseTotal
$past_offset ? : 0 6 6
207 6 my $future = $year + $future_offset;
208 6 ▶ $past ||= $year;
Condition33%1/3
$past ||= $year
l!l&&r!l&&!r
060
MC/DC0%0/2
$past ||= $year
$past $year
Truth table33%1/3
A ||= B
A= $pastB= $year
ABresult
1X1
011
000
209  
210 6 my @range = ($past .. $future);
211 6 ▶ if(defined $range) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $range) 5 1 6
212 5 ▶ if($range == 1) { @range = @$dates }
Branch: 3 branches
BranchTrueFalseTotal
if ($range == 1) { } 1 4 5
elsif ($range == 2) { } 1 3 4
elsif ($range == 3) { } 3 0 3
213 1 elsif($range == 2) { @range = ($past .. $year) }
214 3 elsif($range == 3) { @range = ($year .. $future) }
215 }
216  
217 6 my @years = map {{'id'=>$_,'value'=> $_}} @range;
218 6 ▶ unshift @years, {id=>0,value=>'Select Year'} if(defined $blank && $blank == 1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $blank and $blank == 1 2 4 6
Condition66%2/3
defined $blank and $blank == 1
!ll&&!rl&&r
402
MC/DC50%1/2
defined $blank and $blank == 1
defined $blank $blank == 1
Truth table66%2/3
A and B
A= defined $blankB= $blank == 1
ABresult
0X0
100
111
219 6 DropDownRows($opt,'year','id','value',@years);
220 }
221  
222 3 ▶sub PeriodSelect {
Subroutine PeriodSelect: called CC=3 SCAR=11.0
Pod documented
223 3 my ($opt,$blank) = @_;
224 3 my @list = @periods;
225 3 ▶ unshift @list, {act=>'',value=>'Select Period'} if(defined $blank && $blank == 1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $blank and $blank == 1 1 2 3
Condition66%2/3
defined $blank and $blank == 1
!ll&&!rl&&r
201
MC/DC50%1/2
defined $blank and $blank == 1
defined $blank $blank == 1
Truth table66%2/3
A and B
A= defined $blankB= $blank == 1
ABresult
0X0
100
111
226 3 DropDownRowsText($opt,'period','act','value',@list);
227 }
228  
229 ## ------------------------------------
230 ## Date Functions
231  
232 =head2 Date Formatting
233  
234 =over 4
235  
236 =item formatDate
237  
238 =item unformatDate
239  
240 =item isMonth
241  
242 =back
243  
244 =cut
245  
246 30 ▶sub formatDate {
Subroutine formatDate: called CC=5 SCAR=16.1
Pod documented
247 30 my ($format,$time) = @_;
248 30 ▶ my $now = $time ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 22 8 30
249  
250 30 my $dt;
251 30 ▶ my $timezone = $settings{timezone} || 'Europe/London';
Condition100%2/2
$Labyrinth::DTUtils::settings{'timezone'} || 'Europe/London'
l!l
228
MC/DC100%1/1
$Labyrinth::DTUtils::settings{'timezone'} || 'Europe/London'
$Labyrinth::DTUtils::settings{'timezone'}
Truth table100%2/2
A
A= $Labyrinth::DTUtils::settings{'timezone'}
Aresult
11
00
252 30 ▶ if($time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time) { } 22 8 30
253 22 $dt = DateTime->from_epoch( epoch => $time, time_zone => $timezone );
254 } else {
255 8 $dt = DateTime->now( time_zone => $timezone );
256 }
257  
258 30 ▶ return $dt->epoch unless($format);
Branch: 1 branch
BranchTrueFalseTotal
unless $format 2 28 30
259  
260 #LogDebug("formatDate format=$format, time=".$dt->epoch);
261  
262 # create date mini strings
263 28 my $fmonth = $dt->month_name;
264 28 my $amonth = $dt->month_abbr;
265 28 my $fdotw = $dt->day_name;
266 28 my $adotw = $dt->day_abbr;
267 28 my $fsday = sprintf "%d", $dt->day; # short form, ie 6
268 28 my $fday = sprintf "%02d", $dt->day; # long form, ie 06
269 28 my $fmon = sprintf "%02d", $dt->month;
270 28 my $fyear = sprintf "%04d", $dt->year;
271 28 my $fddext = sprintf "%d%s", $dt->day, _ext($dt->day);
272 28 my $time12 = sprintf "%d:%02d%s", $dt->hour_12, $dt->minute, lc $dt->am_or_pm;
273 28 my $time24 = sprintf "%d:%02d:%02d", $dt->hour, $dt->minute, $dt->second;
274 28 my $fhour = sprintf "%02d", $dt->hour;
275 28 my $fminute = sprintf "%02d", $dt->minute;
276 28 my $fsecond = sprintf "%02d", $dt->second;
277 28 my $tz = 'UTC';
278 28 eval { $tz = $dt->time_zone->short_name_for_datetime };
279  
280 28 my $fmt = $formats{$format};
281  
282 # transpose format string into a date string
283 28 $fmt =~ s/hh/$fhour/;
284 28 $fmt =~ s/mm/$fminute/;
285 28 $fmt =~ s/ss/$fsecond/;
286 28 $fmt =~ s/DMY/$fday-$fmon-$fyear/;
287 28 $fmt =~ s/MDY/$fmon-$fday-$fyear/;
288 28 $fmt =~ s/YMD/$fyear-$fmon-$fday/;
289 28 $fmt =~ s/MABV/$amonth/;
290 28 $fmt =~ s/DABV/$adotw/;
291 28 $fmt =~ s/MONTH/$fmonth/;
292 28 $fmt =~ s/DAY/$fdotw/;
293 28 $fmt =~ s/DDEXT/$fddext/;
294 28 $fmt =~ s/YYYY/$fyear/;
295 28 $fmt =~ s/MM/$fmon/;
296 28 $fmt =~ s/DD/$fday/;
297 28 $fmt =~ s/dd/$fsday/;
298 28 $fmt =~ s/TIME12/$time12/;
299 28 $fmt =~ s/TIME24/$time24/;
300 28 $fmt =~ s/TZ/$tz/;
301  
302 28 return $fmt;
303 }
304  
305 24 ▶sub unformatDate {
Subroutine unformatDate: called CC=17 SCAR=28.4
Pod documented
306 24 my ($format,$time) = @_;
307  
308 24 ▶ return time unless($format && $time);
Branch: 1 branch
BranchTrueFalseTotal
unless $format and $time 2 22 24
Condition66%2/3
$format and $time
!ll&&!rl&&r
2022
MC/DC50%1/2
$format and $time
$format $time
Truth table66%2/3
A and B
A= $formatB= $time
ABresult
0X0
100
111
309  
310 22 my (@fields,@values);
311 22 my @basic = qw(ss mm hh DD MM YYYY);
312 22 my %forms = map {$_ => 0 } @basic, 'dd';
313  
314 22 ▶ if($unformats{$format}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unformats{$format}) { } 4 18 22
315 4 @fields = reverse @basic;
316 4 @values = $time =~ /$unformats{$format}/;
317 } else {
318 18 my $pattern = $formats{$format};
319 18 $pattern =~ s!TIME24!hh::mm:ss!;
320 18 $pattern =~ s!TIME12!hh::ampm!;
321  
322 18 @fields = split(qr![ ,/:()-]+!,$pattern);
323 18 @values = split(qr![ ,/:()-]+!,$time);
324 }
325  
326 22 @forms{@fields} = @values;
327 22 ▶ $forms{$_} = int($forms{$_}||0) for(@basic);
Condition100%2/2
$forms{$_} || 0
l!l
7260
MC/DC100%1/1
$forms{$_} || 0
$forms{$_}
Truth table100%2/2
A
A= $forms{$_}
Aresult
11
00
328  
329 #use Data::Dumper;
330 #LogDebug("format=[$format], time=[$time]");
331 #LogDebug("fields=[@fields], values=[@values]");
332 #LogDebug("before=".Dumper(\%forms));
333  
334 22 ▶ ($forms{DD}) = $forms{dd} =~ /(\d+)/ if($forms{dd});
Branch: 1 branch
BranchTrueFalseTotal
if $forms{"dd"} 1 21 22
335 22 ▶ ($forms{DD}) = $forms{DDEXT} =~ /(\d+)/ if($forms{DDEXT});
Branch: 1 branch
BranchTrueFalseTotal
if $forms{"DDEXT"} 5 17 22
336 22 ▶ $forms{MM} = isMonth($forms{MONTH}) if($forms{MONTH});
Branch: 1 branch
BranchTrueFalseTotal
if $forms{"MONTH"} 9 13 22
337 22 ▶ $forms{MM} = isMonth($forms{MABV}) if($forms{MABV});
Branch: 1 branch
BranchTrueFalseTotal
if $forms{"MABV"} 4 18 22
338 22 ▶ ($forms{mm},$forms{AMPM}) = ($forms{ampm} =~ /(\d+)(am|pm)/) if($forms{ampm});
Branch: 1 branch
BranchTrueFalseTotal
if $forms{"ampm"} 2 20 22
339 22 ▶ $forms{hh}+=12 if($forms{AMPM} && $forms{AMPM} eq 'pm');
Branch: 1 branch
BranchTrueFalseTotal
if $forms{"AMPM"} and $forms{"AMPM"} eq "pm" 0 22 22
Condition66%2/3
$forms{"AMPM"} and $forms{"AMPM"} eq "pm"
!ll&&!rl&&r
2020
MC/DC0%0/2
$forms{"AMPM"} and $forms{"AMPM"} eq "pm"
$forms{"AMPM"} $forms{"AMPM"} eq "pm"
Truth table66%2/3
A and B
A= $forms{"AMPM"}B= $forms{"AMPM"} eq "pm"
ABresult
0X0
100
111
340  
341 22 ▶ @values = map {$forms{$_}||0} @basic;
Condition100%2/2
$forms{$_} || 0
l!l
8547
MC/DC100%1/1
$forms{$_} || 0
$forms{$_}
Truth table100%2/2
A
A= $forms{$_}
Aresult
11
00
342  
343 22 ▶ my $timezone = $settings{timezone} || 'Europe/London';
Condition50%1/2
$Labyrinth::DTUtils::settings{'timezone'} || 'Europe/London'
l!l
220
MC/DC0%0/1
$Labyrinth::DTUtils::settings{'timezone'} || 'Europe/London'
$Labyrinth::DTUtils::settings{'timezone'}
Truth table50%1/2
A
A= $Labyrinth::DTUtils::settings{'timezone'}
Aresult
11
00
344 22 ▶ my $dt = DateTime->new(
Condition100%4/4
$values[4] || 1
l!l
211
$values[3] || 1
l!l
202
MC/DC100%1/1
$values[4] || 1
$values[4]
MC/DC100%1/1
$values[3] || 1
$values[3]
Truth table100%2/2
A
A= $values[4]
Aresult
11
00
Truth table100%2/2
A
A= $values[3]
Aresult
11
00
345 year => $values[5], month => $values[4] || 1, day => $values[3] || 1,
346 hour => $values[2], minute => $values[1], second => $values[0],
347 time_zone => $timezone );
348  
349 22 return $dt->epoch;
350 }
351  
352 59 ▶sub _ext {
Subroutine _ext: called CC=8 SCAR=20.8
353 59 my $day = shift;
354 59 my $ext = "th";
355 59 ▶ if($day == 1 || $day == 21 || $day == 31) { $ext = "st" }
Branch: 3 branches
BranchTrueFalseTotal
if ($day == 1 or $day == 21 or $day == 31) { } 3 56 59
elsif ($day == 2 or $day == 22) { } 8 48 56
elsif ($day == 3 or $day == 23) { } 2 46 48
Condition100%6/6
$day == 1 or $day == 21 or $day == 31
l!l&&r!l&&!r
2156
$day == 1 or $day == 21
l!l&&r!l&&!r
1157
MC/DC100%3/3
$day == 1 or $day == 21 or $day == 31
$day == 1 $day == 21 $day == 31
Truth table100%4/4
A or B or C
A= $day == 1B= $day == 21C= $day == 31
ABCresult
1XX1
01X1
0011
0000
356 8 ▶ elsif($day == 2 || $day == 22) { $ext = "nd" }
Condition100%3/3
$day == 2 or $day == 22
l!l&&r!l&&!r
7148
MC/DC100%2/2
$day == 2 or $day == 22
$day == 2 $day == 22
Truth table100%3/3
A or B
A= $day == 2B= $day == 22
ABresult
1X1
011
000
357 2 ▶ elsif($day == 3 || $day == 23) { $ext = "rd" }
Condition100%3/3
$day == 3 or $day == 23
l!l&&r!l&&!r
1146
MC/DC100%2/2
$day == 3 or $day == 23
$day == 3 $day == 23
Truth table100%3/3
A or B
A= $day == 3B= $day == 23
ABresult
1X1
011
000
358 59 return $ext;
359 }
360  
361 40 ▶sub isMonth {
Subroutine isMonth: called CC=6 SCAR=17.9
Pod documented
362 40 my $month = shift;
363 40 ▶ return (localtime)[4]+1 unless(defined $month && $month);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $month and $month 1 39 40
Condition66%2/3
defined $month and $month
!ll&&!rl&&r
1039
MC/DC50%1/2
defined $month and $month
defined $month $month
Truth table66%2/3
A and B
A= defined $monthB= $month
ABresult
0X0
100
111
364  
365 39 foreach (@months) {
366 297 ▶ return $_->{id} if($_->{value} =~ /$month/);
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"value"} =~ /$month/ 25 272 297
367 272 ▶ return $_->{value} if($month eq $_->{id});
Branch: 1 branch
BranchTrueFalseTotal
if $month eq $_->{"id"} 12 260 272
368 }
369 2 return 0;
370 }
371  
372 13 1;
373  
374 __END__
« Labyrinth/Constraints.pm ↑ Summary Labyrinth/Phrasebook.pm »