Tools/Time.pm

filter: Statementstmt 89.1% 231 / 259 Branchbran 75.8% 47 / 62 Conditioncond 86.6% 13 / 15 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 62 / 62 Podpod 7.4% 4 / 54 totaltotal 78.9% 363 / 460 CC 39 SCAR 12.5
CC
39
Cov
87%
unix_time_diff_in_words_short
14.8
unix_time_diff_in_words
7.9
julian_date_diff_in_words_relative
6.0
CRAP 3.5
« Tools/CSV.pm ↑ Summary Tools/Math.pm »
1 ##############################################################################
2 #
3 # Data::Tools::Time perl module
4 # Copyright (c) 2013-2024 Vladi Belperchinov-Shabanski "Cade"
5 # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
6 # http://cade.noxrun.com/
7 #
8 # GPL
9 #
10 ##############################################################################
11 package Data::Tools::Time;
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Tools;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Date::Calc qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Time::JulianDay;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our $VERSION = '1.60';
20  
21 1 our @ISA = qw( Exporter );
22 1 our @EXPORT = qw(
23  
24 unix_time_diff_in_words
25 unix_time_diff_in_words_short
26 unix_time_diff_in_words_relative
27 unix_time_diff_in_words_relative_short
28
29 julian_date_diff_in_words
30 julian_date_diff_in_words_relative
31  
32 get_local_time_only
33 get_local_julian_day
34 get_local_year
35 get_year_month_days
36  
37 julian_date_from_utime
38 julian_date_add_ymd
39 julian_date_to_ymd
40 julian_date_from_ymd
41 julian_date_from_md
42  
43 julian_date_goto_first_dow
44 julian_date_goto_last_dow
45 julian_date_goto_first_dom
46 julian_date_goto_last_dom
47 julian_date_goto_first_doy
48 julian_date_goto_last_doy
49 julian_date_get_dow
50 julian_date_get_day
51 julian_date_get_month
52 julian_date_get_year
53 julian_date_month_days_ym
54 julian_date_month_days
55  
56 julian_date_to_iso
57 julian_date_from_iso
58  
59 utime_from_julian_date
60 utime_from_ymdhms
61 utime_to_ymdhms
62
63 utime_to_iso
64 utime_from_iso
65 utime_to_iso_ext
66 utime_from_iso_ext
67
68 utime_split_to_jdt
69 utime_split_to_utt
70 utime_join_jdt
71 utime_join_utt
72  
73 utime_add_ymdhms
74 utime_add_ymd
75 utime_add_hms
76  
77 utime_month_days
78  
79 utime_goto_midnight
80 utime_goto_first_dow
81 utime_goto_last_dow
82 utime_goto_first_dom
83 utime_goto_last_dom
84 utime_goto_first_doy
85 utime_goto_last_doy
86  
87 utime_get_dow
88 utime_get_moy
89 utime_get_woy
90
91 );
92  
93 1 our %EXPORT_TAGS = (
94
95 'all' => \@EXPORT,
96 'none' => [],
97
98 );
99  
100 ##############################################################################
101  
102 sub unix_time_diff_in_words
103 10 ▶{
Subroutine unix_time_diff_in_words: called CC=7 SCAR=20.6
Pod documented
104 10 my $utd = abs( int( shift() ) ); # absolute difference in seconds
105  
106 10 ▶ if( $utd < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 1) 1 9 10
107 {
108 1 return "now";
109 }
110 9 ▶ if( $utd < 60 ) # less than 1 minute
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 60) 2 7 9
111 {
112 2 my $ss = str_countable( $utd, "second", "seconds" );
113 2 return "$utd $ss";
114 };
115 7 ▶ if( $utd < 60*60 ) # less than 1 hour
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 3600) 1 6 7
116 {
117 1 my $m = int( $utd / 60 );
118 1 my $ms = str_countable( $m, "minute", "minutes" );
119 1 return "$m $ms";
120 };
121 6 ▶ if( $utd < 2*24*60*60 ) # less than 2 days (48 hours)
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 172800) 5 1 6
122 {
123 5 my $h = int( $utd / ( 60 * 60 ) );
124 5 my $m = int( $utd % ( 60 * 60 ) / 60 );
125 5 my $hs = str_countable( $h, "hour", "hours" );
126 5 my $ms = str_countable( $m, "minute", "minutes" );
127 5 return "$h $hs, $m $ms";
128 };
129 1 ▶ if( $utd < 7*24*60*60 ) # less than 1 week (168 hours)
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 604800) 1 0 1
130 {
131 1 my $d = int( $utd / ( 24 * 60 * 60 ) );
132 1 my $h = int( $utd % ( 24 * 60 * 60 ) / ( 60 * 60 ) );
133 1 my $ds = str_countable( $d, "day", "days" );
134 1 my $hs = str_countable( $h, "hour", "hours" );
135 1 return "$d $ds, $h $hs";
136 };
137 0 ▶ if( $utd < 60*24*60*60 ) # less than 2 months
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 5184000) 0 0 0
138 {
139 0 my $d = int( $utd / ( 24 * 60 * 60 ) );
140 0 my $ds = str_countable( $d, "day", "days" );
141 0 return "$d $ds";
142 };
143 0 if( 42 ) # more than 2 months
144 {
145 0 my $m = int( $utd / ( 30*24*60*60 ) ); # "month" is approximated to 30 days
146 0 my $ms = str_countable( $m, "month", "months" );
147 0 return "$m $ms";
148 }
149 }
150  
151 sub unix_time_diff_in_words_short
152 2 ▶{
Subroutine unix_time_diff_in_words_short: called CC=6 SCAR=26.9
Pod undocumented
153 2 my $utd = abs( int( shift() ) ); # absolute difference in seconds
154  
155 2 ▶ if( $utd < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 1) 0 2 2
156 {
157 0 return "now";
158 }
159 2 ▶ if( $utd < 60 ) # less than 1 minute
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 60) 0 2 2
160 {
161 0 my $ss = str_countable( $utd, "second", "seconds" );
162 0 return "$utd $ss";
163 };
164 2 ▶ if( $utd < 60*60 ) # less than 1 hour
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 3600) 0 2 2
165 {
166 0 my $m = int( $utd / 60 );
167 0 my $ms = str_countable( $m, "minute", "minutes" );
168 0 return "$m $ms";
169 };
170 2 ▶ if( $utd < 2*24*60*60 ) # less than 2 days (48 hours)
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 172800) 2 0 2
171 {
172 2 my $h = int( $utd / ( 60 * 60 ) );
173 2 my $hs = str_countable( $h, "hour", "hours" );
174 2 return "$h $hs";
175 };
176 0 ▶ if( $utd < 60*24*60*60 ) # less than 2 months
Branch: 1 branch
BranchTrueFalseTotal
if ($utd < 5184000) 0 0 0
177 {
178 0 my $d = int( $utd / ( 24 * 60 * 60 ) );
179 0 my $ds = str_countable( $d, "day", "days" );
180 0 return "$d $ds";
181 };
182 0 if( 42 ) # more than 2 months
183 {
184 0 my $m = int( $utd / ( 30*24*60*60 ) ); # "month" is approximated to 30 days
185 0 my $ms = str_countable( $m, "month", "months" );
186 0 return "$m $ms";
187 }
188 }
189  
190 sub __relative_str
191 3 ▶{
Subroutine __relative_str: called CC=3 SCAR=11.2
192 3 my $utd = shift(); # relative difference in seconds
193 3 my $uts = shift(); # relative difference in words
194  
195 3 ▶ if( $utd < 0 )
Branch: 2 branches
BranchTrueFalseTotal
if ($utd < 0) { } 1 2 3
elsif ($utd > 0) { } 2 0 2
196 {
197 1 return "after $uts";
198 }
199 elsif( $utd > 0 )
200 {
201 2 return "before $uts";
202 }
203 else
204 {
205 0 return $uts;
206 }
207 }
208  
209 sub unix_time_diff_in_words_relative
210 2 ▶{
Subroutine unix_time_diff_in_words_relative: called CC=1 SCAR=0.0
Pod documented
211 2 my $utd = int( shift() ); # relative difference in seconds
212  
213 2 my $uts = unix_time_diff_in_words( $utd );
214  
215 2 return __relative_str( $utd, $uts );
216 }
217  
218 sub unix_time_diff_in_words_relative_short
219 1 ▶{
Subroutine unix_time_diff_in_words_relative_short: called CC=1 SCAR=0.0
Pod undocumented
220 1 my $utd = int( shift() ); # relative difference in seconds
221  
222 1 my $uts = unix_time_diff_in_words_short( $utd );
223  
224 1 return __relative_str( $utd, $uts );
225 }
226  
227 ##############################################################################
228  
229 sub julian_date_diff_in_words
230 7 ▶{
Subroutine julian_date_diff_in_words: called CC=2 SCAR=8.7
Pod documented
231 7 my $jdd = abs( int( shift() ) ); # absolute difference in days
232  
233 7 ▶ if( $jdd < 90 )
Branch: 1 branch
BranchTrueFalseTotal
if ($jdd < 90) 7 0 7
234 {
235 7 my $d = int( $jdd );
236 7 my $ds = str_countable( $d, "day", "days" );
237 7 return "$d $ds";
238 }
239 0 if( 42 )
240 {
241 0 my $m = int( $jdd / 30 );
242 0 my $ms = str_countable( $m, "month", "months" );
243 0 return "$m $ms";
244 };
245 }
246  
247 sub julian_date_diff_in_words_relative
248 5 ▶{
Subroutine julian_date_diff_in_words_relative: called CC=6 SCAR=18.0
Pod documented
249 5 my $jdd = int( shift() ); # relative difference in days
250  
251 5 ▶ if( $jdd == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($jdd == 0) 1 4 5
252 {
253 1 return "today";
254 }
255 4 ▶ if( $jdd == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($jdd == -1) 1 3 4
256 {
257 1 return "tomorrow";
258 }
259 3 ▶ if( $jdd == +1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($jdd == 1) 1 2 3
260 {
261 1 return "yesterday";
262 }
263  
264 2 my $jds = julian_date_diff_in_words( $jdd );
265 2 ▶ if( $jdd < 0 )
Branch: 2 branches
BranchTrueFalseTotal
if ($jdd < 0) { } 1 1 2
elsif ($jdd > 0) { } 1 0 1
266 {
267 1 return "in $jds";
268 }
269 elsif( $jdd > 0 )
270 {
271 1 return "before $jds";
272 }
273 else
274 {
275 0 return $jds;
276 }
277 }
278  
279 ##############################################################################
280  
281 # returns time of the day (in the current day only)
282 sub get_local_time_only
283 1 ▶{
Subroutine get_local_time_only: called CC=2 SCAR=7.2
Pod undocumented
284 1 ▶ my ( $s, $m, $h ) = localtime( shift() || time() );
Condition50%1/2
shift() || time
l!l
10
MC/DC0%0/1
shift() || time
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
285 1 return $h*60*60 + $m*60 + $s;
286 }
287  
288 sub get_local_julian_day
289 3 ▶{
Subroutine get_local_julian_day: called CC=1 SCAR=0.0
Pod undocumented
290 3 return local_julian_day( time() );
291 }
292  
293 sub get_local_year
294 1 ▶{
Subroutine get_local_year: called CC=1 SCAR=0.0
Pod undocumented
295 1 my ( $y ) = inverse_julian_day( local_julian_day( time() ) );
296 1 return $y;
297 }
298  
299 sub get_year_month_days
300 2 ▶{
Subroutine get_year_month_days: called CC=1 SCAR=0.0
Pod undocumented
301 2 return Days_in_Month( @_ );
302 }
303  
304 sub julian_date_from_utime
305 5 ▶{
Subroutine julian_date_from_utime: called CC=1 SCAR=0.0
Pod undocumented
306 5 return local_julian_day( shift() );
307 }
308  
309 # return julian date, moved with positive or negative deltas ( y, m, d )
310 sub julian_date_add_ymd
311 3 ▶{
Subroutine julian_date_add_ymd: called CC=1 SCAR=0.0
Pod undocumented
312 3 my $wd = shift; # original/work date
313 3 my $dy = shift; # add delta year
314 3 my $dm = shift; # add delta month
315 3 my $dd = shift; # add delta day
316  
317 3 my ( $y, $m, $d ) = inverse_julian_day( $wd );
318  
319 3 ( $y, $m, $d ) = Add_Delta_YMD( $y, $m, $d, $dy, $dm, $dd );
320  
321 3 $wd = julian_day( $y, $m, $d );
322  
323 3 return $wd;
324  
325 }
326  
327 # return ( year, month, day ) from julian date
328 sub julian_date_to_ymd
329 1624 ▶{
Subroutine julian_date_to_ymd: called CC=1 SCAR=0.0
Pod undocumented
330 1624 my $wd = shift; # original/work date
331  
332 1624 my ( $y, $m, $d ) = inverse_julian_day( $wd );
333 1624 return ( $y, $m, $d );
334 }
335  
336 # return julian date from ( year, month, day )
337 sub julian_date_from_ymd
338 1623 ▶{
Subroutine julian_date_from_ymd: called CC=3 SCAR=11.0
Pod undocumented
339 1623 my $y = shift; # set year
340 1623 ▶ my $m = shift || 1; # set month
Condition100%2/2
shift() || 1
l!l
16221
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
341 1623 ▶ my $d = shift || 1; # set day
Condition100%2/2
shift() || 1
l!l
16212
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
342  
343 1623 my $wd = julian_day( $y, $m, $d );
344 1623 return $wd;
345 }
346  
347 # return julian date from month and day only
348 # finds closest date which matches, regardless if it is in the future or past
349 sub julian_date_from_md
350 1 ▶{
Subroutine julian_date_from_md: called CC=5 SCAR=16.7
Pod undocumented
351 1 my $mon = shift;
352 1 ▶ my $day = shift || 1;
Condition50%1/2
shift() || 1
l!l
10
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
353  
354 1 my $now = local_julian_day( time() );
355 1 my ( $cy ) = julian_date_to_ymd( $now );
356
357 1 my $cd = julian_date_from_ymd( $cy, $mon, $day );
358  
359 1 ▶ return $cd if $now == $cd;
Branch: 1 branch
BranchTrueFalseTotal
if $now == $cd 0 1 1
360  
361 1 ▶ my $oy = $cy + ( $cd > $now ? -1 : +1 );
Branch: 1 branch
BranchTrueFalseTotal
$cd > $now ? : 0 1 1
362 1 my $od = julian_date_from_ymd( $oy, $mon, $day );
363  
364 1 ▶ return abs( $cd - $now ) < abs( $od - $now ) ? $cd : $od;
Branch: 1 branch
BranchTrueFalseTotal
abs $cd - $now < abs $od - $now ? : 0 1 1
365 }
366  
367 # return julian date, moved to the first day of its week (starting Mon=1 as in ISO8601)
368 sub julian_date_goto_first_dow
369 1 ▶{
Subroutine julian_date_goto_first_dow: called CC=1 SCAR=0.0
Pod undocumented
370 1 my $wd = shift; # original/work date
371
372 1 return $wd - ( julian_date_get_dow( $wd ) - 1 );
373 }
374  
375 # return julian date, moved to the last day of its week (ending Sun=7 as in ISO8601)
376 sub julian_date_goto_last_dow
377 1 ▶{
Subroutine julian_date_goto_last_dow: called CC=1 SCAR=0.0
Pod undocumented
378 1 my $wd = shift; # original/work date
379  
380 1 return $wd + 7 - julian_date_get_dow( $wd );
381 }
382  
383 # return julian date, moved to the first day of its month
384 sub julian_date_goto_first_dom
385 1 ▶{
Subroutine julian_date_goto_first_dom: called CC=1 SCAR=0.0
Pod undocumented
386 1 my $wd = shift; # original/work date
387  
388 1 my ( $y, $m, $d ) = julian_date_to_ymd( $wd );
389 1 return julian_date_from_ymd( $y, $m, 1 );
390 }
391  
392 # return julian date, moved to the last day of its month
393 sub julian_date_goto_last_dom
394 1 ▶{
Subroutine julian_date_goto_last_dom: called CC=1 SCAR=0.0
Pod undocumented
395 1 my $wd = shift; # original/work date
396  
397 1 my ( $y, $m, $d ) = julian_date_to_ymd( $wd );
398 1 return julian_date_from_ymd( $y, $m, Days_in_Month( $y, $m ) );
399 }
400  
401 # return julian date, moved to the first day of its year
402 sub julian_date_goto_first_doy
403 1 ▶{
Subroutine julian_date_goto_first_doy: called CC=1 SCAR=0.0
Pod undocumented
404 1 my $wd = shift; # original/work date
405  
406 1 my ( $y, $m, $d ) = julian_date_to_ymd( $wd );
407 1 return julian_date_from_ymd( $y, 1, 1 );
408 }
409  
410 # return julian date, moved to the last day of its year
411 sub julian_date_goto_last_doy
412 1 ▶{
Subroutine julian_date_goto_last_doy: called CC=1 SCAR=0.0
Pod undocumented
413 1 my $wd = shift; # original/work date
414  
415 1 my ( $y, $m, $d ) = julian_date_to_ymd( $wd );
416 1 return julian_date_from_ymd( $y, 12, 31 );
417 }
418  
419 # return day of the month, 1 .. 31
420 sub julian_date_get_day
421 1 ▶{
Subroutine julian_date_get_day: called CC=1 SCAR=0.0
Pod undocumented
422 1 my $d = shift; # original date
423  
424 1 my ( $y, $m, $d ) = julian_date_to_ymd( $d );
425 1 return $d;
426 }
427  
428 # return month of the year, 1 .. 12
429 sub julian_date_get_month
430 1 ▶{
Subroutine julian_date_get_month: called CC=1 SCAR=0.0
Pod undocumented
431 1 my $d = shift; # original date
432  
433 1 my ( $y, $m, $d ) = julian_date_to_ymd( $d );
434 1 return $m;
435 }
436  
437 # return year of the given date
438 sub julian_date_get_year
439 1 ▶{
Subroutine julian_date_get_year: called CC=1 SCAR=0.0
Pod undocumented
440 1 my $d = shift; # original date
441  
442 1 my ( $y, $m, $d ) = julian_date_to_ymd( $d );
443 1 return $y;
444 }
445  
446 # return day of the week, for julian date -- 1 Mon .. 7 Sun
447 sub julian_date_get_dow
448 4 ▶{
Subroutine julian_date_get_dow: called CC=2 SCAR=6.9
Pod undocumented
449 4 my $d = shift; # original date
450  
451 4 my $dow = day_of_week( $d ); # this returns Sun=0 ... Sat=6, but...
452  
453 4 ▶ return 7 if $dow == 0; # return Sun=7 as in ISO8601
Branch: 1 branch
BranchTrueFalseTotal
if $dow == 0 1 3 4
454 3 return $dow; # return Mon=1 as in ISO8601
455 }
456  
457 # return month days count for given ( year, month ) (not strictly julian_ namespace)
458 sub julian_date_month_days_ym
459 2 ▶{
Subroutine julian_date_month_days_ym: called CC=1 SCAR=0.0
Pod undocumented
460 2 my $y = shift; # set year
461 2 my $m = shift; # set month
462  
463 2 return Days_in_Month( $y, $m );
464 }
465  
466 # return month days count for given julian date
467 sub julian_date_month_days
468 1 ▶{
Subroutine julian_date_month_days: called CC=1 SCAR=0.0
Pod undocumented
469 1 my $d = shift;
470  
471 1 return Days_in_Month( ( julian_date_to_ymd( $d ) )[0,1] );
472 }
473  
474 sub julian_date_to_iso
475 811 ▶{
Subroutine julian_date_to_iso: called CC=1 SCAR=0.0
Pod undocumented
476 811 return sprintf( "%04d-%02d-%02d", julian_date_to_ymd( shift() ) );
477 }
478  
479 # converts iso date to julian date. accepts exactly the same formats as
480 # utime_from_iso() and utime_from_iso_ext() -- basic YYYYMMDDTHHMMSS and
481 # extended YYYY-MM-DDTHH:MM:SS, zero padded, with the time part optional and
482 # the 'T' of the extended form optionally a space. a time part is accepted for
483 # format compatibility and must be a valid time, but a julian date holds no
484 # time of day, so it is discarded. it is the exact inverse of
485 # julian_date_to_iso(), which writes the zero padded extended form.
486 # returns undef if the string is not a well formed and existing date
487 sub julian_date_from_iso
488 1617 ▶{
Subroutine julian_date_from_iso: called CC=6 SCAR=17.9
Pod undocumented
489 1617 my $s = shift;
490  
491 1617 my ( $y, $m, $d, $hh, $mm, $ss );
492  
493 1617 ▶ if( $s =~ /^\s*(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d))?\s*$/
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\s*(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d))?\s*$/ or $s =~ /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T ](\d\d):(\d\d):(\d\d))?\s*$/) { } 1614 3 1617
Condition100%3/3
$s =~ /^\s*(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d))?\s*$/ or $s =~ /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T ](\d\d):(\d\d):(\d\d))?\s*$/
l!l&&r!l&&!r
8068083
MC/DC100%2/2
$s =~ /^\s*(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d))?\s*$/ or $s =~ /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T ](\d\d):(\d\d):(\d\d))?\s*$/
$s =~ /^\s*(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d))?\s*$/ $s =~ /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T ](\d\d):(\d\d):(\d\d))?\s*$/
Truth table100%3/3
A or B
A= $s =~ /^\s*(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d))?\s*$/B= $s =~ /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T ](\d\d):(\d\d):(\d\d))?\s*$/
ABresult
1X1
011
000
494 or $s =~ /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T ](\d\d):(\d\d):(\d\d))?\s*$/ )
495 {
496 1614 ( $y, $m, $d, $hh, $mm, $ss ) = ( $1, $2, $3, $4, $5, $6 );
497 }
498 else
499 {
500 3 return undef;
501 }
502  
503 1614 ( $y, $m, $d ) = map { $_ + 0 } ( $y, $m, $d );
504 1614 ▶ ( $hh, $mm, $ss ) = map { ( $_ || 0 ) + 0 } ( $hh, $mm, $ss );
Condition100%2/2
$_ || 0
l!l
184824
MC/DC100%1/1
$_ || 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
505  
506 1614 ▶ return undef unless check_date( $y, $m, $d );
Branch: 1 branch
BranchTrueFalseTotal
unless check_date($y, $m, $d) 2 1612 1614
507 1612 ▶ return undef unless check_time( $hh, $mm, $ss ); # discarded, still validated
Branch: 1 branch
BranchTrueFalseTotal
unless check_time($hh, $mm, $ss) 1 1611 1612
508  
509 1611 return julian_date_from_ymd( $y, $m, $d );
510 }
511  
512 ##############################################################################
513  
514 sub utime_from_julian_date
515 2 ▶{
Subroutine utime_from_julian_date: called CC=1 SCAR=0.0
Pod undocumented
516 2 my ( $year, $month, $day ) = inverse_julian_day( shift() );
517 2 return Mktime( $year, $month, $day, 0, 0, 0 );
518 }
519  
520 sub utime_from_ymdhms
521 420 ▶{
Subroutine utime_from_ymdhms: called CC=1 SCAR=0.0
Pod undocumented
522 420 my @args = ( @_, 0, 0, 0, );
523 420 return Mktime( @args[ 0 .. 5 ] );
524 }
525  
526 # TODO: handle TZ
527 sub utime_to_ymdhms
528 419 ▶{
Subroutine utime_to_ymdhms: called CC=1 SCAR=0.0
Pod undocumented
529 419 return Localtime( shift() );
530 }
531  
532 # NOTE: returns date/time string without timezone
533  
534 # convert to basic format
535 sub utime_to_iso
536 203 ▶{
Subroutine utime_to_iso: called CC=1 SCAR=0.0
Pod undocumented
537 203 return sprintf("%04d%02d%02dT%02d%02d%02d", utime_to_ymdhms( shift() ) );
538 }
539  
540 # convert to extended format
541 sub utime_to_iso_ext
542 214 ▶{
Subroutine utime_to_iso_ext: called CC=1 SCAR=0.0
Pod undocumented
543 214 return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", utime_to_ymdhms( shift() ) );
544 }
545  
546 # shared back end of utime_from_iso() and utime_from_iso_ext()
547 sub __utime_from_iso_parts
548 415 ▶{
Subroutine __utime_from_iso_parts: called CC=4 SCAR=13.9
549 415 my ( $y, $m, $d, $hh, $mm, $ss ) = @_;
550  
551 # numify, both to drop leading zeroes and to default a missing time part
552 415 ( $y, $m, $d ) = map { $_ + 0 } ( $y, $m, $d );
553 415 ▶ ( $hh, $mm, $ss ) = map { ( $_ || 0 ) + 0 } ( $hh, $mm, $ss );
Condition100%2/2
$_ || 0
l!l
12369
MC/DC100%1/1
$_ || 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
554  
555 415 ▶ return undef unless check_date( $y, $m, $d );
Branch: 1 branch
BranchTrueFalseTotal
unless check_date($y, $m, $d) 2 413 415
556 413 ▶ return undef unless check_time( $hh, $mm, $ss );
Branch: 1 branch
BranchTrueFalseTotal
unless check_time($hh, $mm, $ss) 1 412 413
557  
558 412 return utime_from_ymdhms( $y, $m, $d, $hh, $mm, $ss );
559 }
560  
561 # converts basic iso format to utime -- YYYYMMDDTHHMMSS, the exact inverse of
562 # utime_to_iso(). the time part is optional and defaults to midnight.
563 # the string is read as local time, as utime_to_iso() writes it.
564 # returns undef if the string is not a well formed and existing date/time
565 sub utime_from_iso
566 212 ▶{
Subroutine utime_from_iso: called CC=2 SCAR=6.9
Pod undocumented
567 212 my $s = shift;
568  
569 212 ▶ return undef unless $s =~ /^\s*(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d))?\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /^\s*(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d))?\s*$/ 4 208 212
570  
571 208 return __utime_from_iso_parts( $1, $2, $3, $4, $5, $6 );
572 }
573  
574 # converts extended iso format to utime -- YYYY-MM-DDTHH:MM:SS, the exact
575 # inverse of utime_to_iso_ext(). the time part is optional and defaults to
576 # midnight, and the 'T' separator may also be a space.
577 # the string is read as local time, as utime_to_iso_ext() writes it.
578 # returns undef if the string is not a well formed and existing date/time
579 sub utime_from_iso_ext
580 209 ▶{
Subroutine utime_from_iso_ext: called CC=2 SCAR=6.9
Pod undocumented
581 209 my $s = shift;
582  
583 209 ▶ return undef unless $s =~ /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T ](\d\d):(\d\d):(\d\d))?\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T ](\d\d):(\d\d):(\d\d))?\s*$/ 2 207 209
584  
585 207 return __utime_from_iso_parts( $1, $2, $3, $4, $5, $6 );
586 }
587  
588 # returns local julian day and time from unix time
589 sub utime_split_to_jdt
590 1 ▶{
Subroutine utime_split_to_jdt: called CC=1 SCAR=0.0
Pod undocumented
591 1 my ($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst) = Localtime(shift());
592 1 my $jd = julian_day( $year, $month, $day );
593 1 my $tt = $hour * 60 * 60 + $min * 60 + $sec;
594 1 return ( $jd, $tt );
595 }
596  
597 # returns unix time of the 00:00 of the day given as utime and time from unix time
598 sub utime_split_to_utt
599 4 ▶{
Subroutine utime_split_to_utt: called CC=1 SCAR=0.0
Pod undocumented
600 4 my ($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst) = Localtime(shift());
601
602 4 my $ut = Mktime( $year, $month, $day, 0, 0, 0 );
603 4 my $tt = $hour * 60 * 60 + $min * 60 + $sec;
604 4 return ( $ut, $tt );
605 }
606  
607 # joins julian date and day time only into unix time
608 sub utime_join_jdt
609 1 ▶{
Subroutine utime_join_jdt: called CC=1 SCAR=0.0
Pod undocumented
610 1 my $jd = shift;
611 1 my $tt = shift;
612
613 1 return utime_from_julian_date( $jd ) + $tt;
614 }
615  
616 # joins unix time of the day midnight and day time only into unix time
617 # mostly redundant but exists for completeness
618 sub utime_join_utt
619 1 ▶{
Subroutine utime_join_utt: called CC=1 SCAR=0.0
Pod undocumented
620 1 return shift() + shift();
621 }
622  
623 sub utime_add_ymdhms
624 4 ▶{
Subroutine utime_add_ymdhms: called CC=1 SCAR=0.0
Pod undocumented
625 4 my $tt = shift;
626 4 my ( $dy, $do, $dd, $dh, $dm, $ds ) = @_;
627
628 4 my ( $year, $month, $day, $hour, $min, $sec ) = Localtime( $tt );
629
630 4 ( $year, $month, $day, $hour, $min, $sec ) =
631 Add_Delta_YMDHMS( $year, $month, $day, $hour, $min, $sec,
632 $dy, $do, $dd, $dh, $dm, $ds );
633  
634 4 return Mktime( $year, $month, $day, $hour, $min, $sec );
635 }
636  
637 sub utime_add_ymd
638 1 ▶{
Subroutine utime_add_ymd: called CC=1 SCAR=0.0
Pod undocumented
639 1 my $tt = shift;
640 1 my ( $dy, $do, $dd ) = @_;
641 1 return utime_add_ymdhms( $tt, $dy, $do, $dd, 0, 0, 0 );
642 }
643  
644 sub utime_add_hms
645 1 ▶{
Subroutine utime_add_hms: called CC=1 SCAR=0.0
Pod undocumented
646 1 my $tt = shift;
647 1 my ( $dh, $dm, $ds ) = @_;
648 1 return utime_add_ymdhms( $tt, 0, 0, 0, $dh, $dm, $ds );
649 }
650  
651 sub utime_month_days
652 1 ▶{
Subroutine utime_month_days: called CC=1 SCAR=0.0
Pod undocumented
653 1 return get_year_month_days( ( utime_to_ymdhms( shift() ) )[ 0, 1 ] );
654 }
655  
656 sub utime_goto_midnight
657 3 ▶{
Subroutine utime_goto_midnight: called CC=1 SCAR=0.0
Pod undocumented
658 3 return ( utime_split_to_utt( shift() ) )[0];
659 }
660  
661 # return unix time, moved to the midnight of the first day of its week (starting Mon=1 as in ISO8601)
662 sub utime_goto_first_dow
663 1 ▶{
Subroutine utime_goto_first_dow: called CC=1 SCAR=0.0
Pod undocumented
664 1 my $tt = shift;
665
666 1 return utime_goto_midnight( $tt - ( utime_get_dow( $tt ) - 1 ) * 24 * 3600 );
667 }
668  
669 # return unix time, moved to the midnight of the last day of its week (ending Sun=7 as in ISO8601)
670 sub utime_goto_last_dow
671 1 ▶{
Subroutine utime_goto_last_dow: called CC=1 SCAR=0.0
Pod undocumented
672 1 my $tt = shift;
673  
674 1 return utime_goto_midnight( $tt + ( 7 - utime_get_dow( $tt ) ) * 24 * 3600 );
675 }
676  
677 sub utime_goto_first_dom
678 1 ▶{
Subroutine utime_goto_first_dom: called CC=1 SCAR=0.0
Pod undocumented
679 1 my $tt = shift;
680 1 my ( $year, $month, $day, $hour, $min, $sec ) = Localtime( $tt );
681 1 return Mktime( $year, $month, 1, 0, 0, 0 );
682 }
683  
684 sub utime_goto_last_dom
685 1 ▶{
Subroutine utime_goto_last_dom: called CC=1 SCAR=0.0
Pod undocumented
686 1 my $tt = shift;
687 1 my ( $year, $month, $day, $hour, $min, $sec ) = Localtime( $tt );
688 1 return Mktime( $year, $month, Days_in_Month( $year, $month ), 0, 0, 0 );
689 }
690  
691 sub utime_goto_first_doy
692 1 ▶{
Subroutine utime_goto_first_doy: called CC=1 SCAR=0.0
Pod undocumented
693 1 my $tt = shift;
694 1 my ( $year, $month, $day, $hour, $min, $sec ) = Localtime( $tt );
695 1 return Mktime( $year, 1, 1, 0, 0, 0 );
696 }
697  
698 sub utime_goto_last_doy
699 1 ▶{
Subroutine utime_goto_last_doy: called CC=1 SCAR=0.0
Pod undocumented
700 1 my $tt = shift;
701 1 my ( $year, $month, $day, $hour, $min, $sec ) = Localtime( $tt );
702 1 return Mktime( $year, 12, 31, 0, 0, 0 );
703 }
704  
705 sub utime_get_dow
706 3 ▶{
Subroutine utime_get_dow: called CC=1 SCAR=0.0
Pod undocumented
707 3 my $tt = shift;
708 3 my ( $year, $month, $day, $hour, $min, $sec ) = Localtime( $tt );
709 3 return Day_of_Week( $year, $month, $day );
710 }
711  
712 sub utime_get_moy
713 1 ▶{
Subroutine utime_get_moy: called CC=1 SCAR=0.0
Pod undocumented
714 1 my $tt = shift;
715 1 my ( $year, $month, $day, $hour, $min, $sec ) = Localtime( $tt );
716 1 return $month;
717 }
718  
719 sub utime_get_woy
720 2 ▶{
Subroutine utime_get_woy: called CC=2 SCAR=6.9
Pod undocumented
721 2 my $tt = shift;
722 2 my ( $year, $month, $day, $hour, $min, $sec ) = Localtime( $tt );
723 2 my ( $week, $year ) = Week_of_Year( $year, $month, $day );
724 2 ▶ return wantarray ? ( $week, $year ) : $week;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
725 }
726  
727 ##############################################################################
728  
729 =pod
730  
731  
732 =head1 NAME
733  
734 Data::Tools::Time provides set of basic functions for time processing.
735  
736 =head1 SYNOPSIS
737  
738 use Data::Tools::Time qw( :all ); # import all functions
739 use Data::Tools::Time; # the same as :all :)
740 use Data::Tools::Time qw( :none ); # do not import anything
741  
742 # --------------------------------------------------------------------------
743  
744 my $time_diff_str = unix_time_diff_in_words( $time1 - $time2 );
745 my $time_diff_str_rel = unix_time_diff_in_words_relative( $time1 - $time2 );
746  
747 # --------------------------------------------------------------------------
748
749 my $date_diff_str = julian_date_diff_in_words( $date1 - $date2 );
750 my $date_diff_str_rel = julian_date_diff_in_words_relative( $date1 - $date2 );
751  
752 # --------------------------------------------------------------------------
753  
754 # return seconds after last midnight, i.e. current day time
755 my $seconds_in_the_current_day = get_local_time_only()
756
757 # returns current julian day
758 my $jd = get_local_julian_day()
759
760 # returns current year
761 my $year = get_local_year()
762
763 # gets current julian date, needs Time::JulianDay
764 my $jd = local_julian_day( time() );
765 # or
766 my $jd = get_local_julian_day();
767  
768 # move current julian date to year ago, one month ahead and 2 days ahead
769 $jd = julian_date_add_ymd( $jd, -1, 1, 2 );
770  
771 # get year, month and day from julian date
772 my ( $y, $m, $d ) = julian_date_to_ymd( $jd );
773  
774 # get julian date from year, month and day
775 $jd = julian_date_from_ymd( $y, $m, $d );
776  
777 # move julian date ($jd) to the first day of its current month
778 $jd = julian_date_goto_first_dom( $jd );
779  
780 # move julian date ($jd) to the last day of its current month
781 $jd = julian_date_goto_last_dom( $jd );
782  
783 # get day of week for given julian date ( 0 => Mon .. 6 => Sun )
784 my $dow = julian_date_get_dow( $jd );
785 print( ( qw( Mon Tue Wed Thu Fri Sat Sun ) )[ $dow ] . "\n" );
786  
787 # get month days count for the given julian date's month
788 my $mdays = julian_date_month_days( $jd );
789  
790 # get month days count for the given year and month
791 my $mdays = julian_date_month_days_ym( $y, $m );
792  
793 =head1 FUNCTIONS
794  
795 =head2 unix_time_diff_in_words( $unix_time_diff )
796  
797 Returns human-friendly text for the given time difference (in seconds).
798 This function returns absolute difference text, for relative
799 (before/after/ago/in) see unix_time_diff_in_words_relative().
800  
801 =head2 unix_time_diff_in_words_relative( $unix_time_diff )
802  
803 Same as unix_time_diff_in_words() but returns relative text
804 (i.e. with before/after/ago/in)
805  
806 =head2 julian_date_diff_in_words( $julian_date_diff );
807  
808 Returns human-friendly text for the given date difference (in days).
809 This function returns absolute difference text, for relative
810 (before/after/ago/in) see julian_date_diff_in_words_relative().
811  
812 =head2 julian_date_diff_in_words_relative( $julian_date_diff );
813  
814 Same as julian_date_diff_in_words() but returns relative text
815 (i.e. with before/after/ago/in)
816  
817 =head1 TODO
818  
819 * support for language-dependent wording (before/ago)
820 * support for user-defined thresholds (48 hours, 2 months, etc.)
821  
822 =head1 REQUIRED MODULES
823  
824 Data::Tools::Time uses:
825  
826 * Data::Tools (from the same package)
827 * Date::Calc
828 * Time::JulianDay
829  
830 =head1 TEXT TRANSLATION NOTES
831  
832 time/date difference wording functions does not have translation functions
833 and return only english text. This is intentional since the goal is to keep
834 the translation mess away but still allow simple (yet bit strange)
835 way to translate the result strings with regexp and language hash:
836
837 my $time_diff_str_rel = unix_time_diff_in_words_relative( $time1 - $time2 );
838
839 my %TRANS = (
840 'now' => 'sega',
841 'today' => 'dnes',
842 'tomorrow' => 'utre',
843 'yesterday' => 'vchera',
844 'in' => 'sled',
845 'before' => 'predi',
846 'year' => 'godina',
847 'years' => 'godini',
848 'month' => 'mesec',
849 'months' => 'meseca',
850 'day' => 'den',
851 'days' => 'dni',
852 'hour' => 'chas',
853 'hours' => 'chasa',
854 'minute' => 'minuta',
855 'minutes' => 'minuti',
856 'second' => 'sekunda',
857 'seconds' => 'sekundi',
858 );
859
860 $time_diff_str_rel =~ s/([a-z]+)/$TRANS{ lc $1 } || $1/ge;
861  
862 I know this is no good for longer sentences but works fine in this case.
863  
864 =head1 GITHUB REPOSITORY
865  
866 git@github.com:cade-vs/perl-data-tools.git
867
868 git clone git://github.com/cade-vs/perl-data-tools.git
869
870 =head1 AUTHOR
871  
872 Vladi Belperchinov-Shabanski "Cade"
873 <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
874 http://cade.noxrun.com/
875  
876  
877 =cut
878  
879 ##############################################################################
880 1 1;
« Tools/CSV.pm ↑ Summary Tools/Math.pm »