File Coverage

blib/lib/App/timecalc.pm
Criterion Covered Total %
statement 49 58 84.4
branch 17 22 77.2
condition 18 31 58.0
subroutine 6 6 100.0
pod 1 1 100.0
total 91 118 77.1


line stmt bran cond sub pod time code
1             package App::timecalc;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-10-26'; # DATE
5             our $DIST = 'App-timecalc'; # DIST
6             our $VERSION = '0.005'; # VERSION
7              
8 1     1   74027 use 5.010001;
  1         12  
9 1     1   5 use strict;
  1         3  
  1         44  
10 1     1   6 use warnings;
  1         2  
  1         28  
11              
12 1     1   5 use Exporter 'import';
  1         9  
  1         130  
13             our @EXPORT_OK = qw(eval_time_expr);
14              
15             sub eval_time_expr {
16 11     11 1 124 my $str = shift;
17              
18 11         25 my ($h, $m, $s) = (0, 0, 0);
19              
20 11         97 $str =~ s{
21             \s*
22             (?:
23             (?\d\d?):(?\d\d?)(?: :(?\d\d?))? |
24             (?\d\d?)(?\d\d)(?\d\d)?
25             )
26             \s*-\s*
27             (?:
28             (?\d\d?):(?\d\d?)(?: :(?\d\d?))? \s* |
29             (?\d\d?)(?\d\d)(?\d\d)?
30             )
31             |
32             \s*\+
33             (?:
34             (?\d\d?):(?\d\d?)(?: :(?\d\d?))? |
35             (?\d\d?)(?\d\d)(?\d\d)?
36             )
37             \s*
38             |
39             \s*\-
40             (?:
41             (?\d\d?):(?\d\d?)(?: :(?\d\d?))? \s* |
42             (?\d\d?)(?\d\d)(?\d\d)?
43             )
44             \s*
45             }{
46              
47 1 100 100 1   464 if (defined $+{h1_colon} || defined $+{h1_nocolon}) {
  1 100 100     435  
  1 50 66     576  
  15         218  
48             my ($h1, $m1, $s1) = defined $+{h1_colon} ?
49             ($+{h1_colon} , $+{m1_colon} , $+{s1_colon} // 0) :
50 3 100 50     35 ($+{h1_nocolon}, $+{m1_nocolon}, $+{s1_nocolon} // 0);
      50        
51             my ($h2, $m2, $s2) = defined $+{h2_colon} ?
52             ($+{h2_colon} , $+{m2_colon} , $+{s2_colon} // 0) :
53 3 100 50     36 ($+{h2_nocolon}, $+{m2_nocolon}, $+{s2_nocolon} // 0);
      50        
54 3 50       15 if ($h1 > 24) { die "Hour cannot exceed 24: $h1" }
  0         0  
55 3 50       9 if ($h2 > 24) { die "Hour cannot exceed 24: $h2" }
  0         0  
56 3 50 33     11 if ($h2 < $h1 || $h2 <= $h1 && $m2 <= $m1) { $h2 += 24 }
  0   33     0  
57 3         7 $h += ($h2-$h1);
58 3         5 $m += ($m2-$m1);
59 3         6 $s += ($s2-$s1);
60             } elsif (defined $+{hplus_colon} || defined $+{hplus_nocolon}) {
61 8 100       31 if (defined $+{hplus_colon}) {
62 6         25 $h += $+{hplus_colon};
63 6         27 $m += $+{mplus_colon};
64 6   50     31 $s += $+{splus_colon} // 0;
65             } else {
66 2         9 $h += $+{hplus_nocolon};
67 2         9 $m += $+{mplus_nocolon};
68 2   50     12 $s += $+{splus_nocolon} // 0;
69             }
70             } elsif (defined $+{hminus_colon} || defined $+{hminus_nocolon}) {
71 4 100       16 if (defined $+{hminus_colon}) {
72 2         9 $h -= $+{hminus_colon};
73 2         8 $m -= $+{mminus_colon};
74 2   50     12 $s -= $+{sminus_colon} // 0;
75             } else {
76 2         7 $h -= $+{hminus_nocolon};
77 2         8 $m -= $+{mminus_nocolon};
78 2   50     12 $s -= $+{sminus_nocolon} // 0;
79             }
80             }
81              
82 15         68 "";
83             }egsx;
84              
85 11 50       31 die "Unexpected string near '$str'" if length $str;
86              
87 11         29 while ($s < 0) {
88 0         0 $m--;
89 0         0 $s += 60;
90             }
91 11         22 while ($s >= 60) {
92 0         0 $m++;
93 0         0 $s -= 60;
94             }
95 11         25 while ($m < 0) {
96 7         9 $h--;
97 7         14 $m += 60;
98             }
99 11         23 while ($m >= 60) {
100 0         0 $h++;
101 0         0 $m -= 60;
102             }
103              
104 11         86 sprintf "+%02d:%02d:%02d", $h, $m, $s;
105             }
106              
107             1;
108             # ABSTRACT: Time calculator
109              
110             __END__