lib/Dancer2/Core/Time.pm

filter: Statementstmt 100% 45 / 45 Branchbran 94.4% 17 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 95.8% 69 / 72 CC 10 SCAR 12.2
CC
10
Cov
98%
_parse_duration
6.0
_build_seconds
2.0
_build_epoch
2.0
CRAP 3.4
« blib/lib/Dancer2/Logger/File.pm ↑ Summary blib/lib/Dancer2/Serializer/Mutable.pm »
1 package Dancer2::Core::Time;
2 # ABSTRACT: class to handle common helpers for time manipulations
3 213 $Dancer2::Core::Time::VERSION = '2.3.0';
4 213 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 213 has seconds => (
7 is => 'ro',
8 lazy => 1,
9 builder => '_build_seconds',
10 );
11  
12 42 ▶sub _build_seconds {
Subroutine _build_seconds: called CC=2 SCAR=7.0
13 42 my ($self) = @_;
14 42 my $seconds = $self->expression;
15  
16 42 ▶ return $seconds
Branch: 1 branch
BranchTrueFalseTotal
if $seconds =~ /^\d+$/ 0 42 42
17 if $seconds =~ /^\d+$/;
18  
19 42 return $self->_parse_duration($seconds)
20 }
21  
22 213 has epoch => (
23 is => 'ro',
24 lazy => 1,
25 builder => '_build_epoch',
26 );
27  
28 38 ▶sub _build_epoch {
Subroutine _build_epoch: called CC=2 SCAR=6.9
29 38 my ($self) = @_;
30 38 ▶ return $self->seconds if $self->seconds !~ /^[\-\+]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->seconds =~ /^[\-\+]?\d+$/ 8 30 38
31 30 $self->seconds + time;
32 }
33  
34 213 my @MONTHS = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
35 213 my @DAYS = qw(Sun Mon Tue Wed Thu Fri Sat);
36  
37 # The English abbreviated month name for a 0-based month number. These names
38 # are fixed, unlike strftime's %b, which is locale-dependent and which some
39 # platforms cannot render as a name at all.
40 57 ▶sub month_abbrev {
Subroutine month_abbrev: called CC=1 SCAR=0.0
Pod undocumented
41 57 return $MONTHS[ $_[0] ];
42 }
43  
44 213 has gmt_string => (
45 is => 'ro',
46 builder => '_build_gmt_string',
47 lazy => 1,
48 );
49  
50 51 ▶sub _build_gmt_string {
Subroutine _build_gmt_string: called CC=2 SCAR=6.9
51 51 my ($self) = @_;
52 51 my $epoch = $self->epoch;
53 51 ▶ return $epoch if $epoch !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $epoch =~ /^\d+$/ 8 43 51
54  
55 43 my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = gmtime($epoch);
56  
57 43 return sprintf "%s, %02d-%s-%d %02d:%02d:%02d GMT",
58 $DAYS[$wday],
59 $mday,
60 month_abbrev($mon),
61 ( $year + 1900 ),
62 $hour, $min, $sec;
63 }
64  
65 213 has expression => (
66 is => 'ro',
67 required => 1,
68 );
69  
70 87 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
71 87 my ($class, %args) = @_;
72  
73 $args{epoch} = $args{expression}
74 87 ▶ if $args{expression} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"expression"} =~ /^\d+$/ 45 42 87
75  
76 87 return \%args;
77 }
78  
79 # private
80  
81 # This map is taken from Cache and Cache::Cache
82 # map of expiration formats to their respective time in seconds
83 #<<< no perl tidy
84 213 my %Units = ( map(($_, 1), qw(s second seconds sec secs)),
85 map(($_, 60), qw(m minute minutes min mins)),
86 map(($_, 60*60), qw(h hr hour hours)),
87 map(($_, 60*60*24), qw(d day days)),
88 map(($_, 60*60*24*7), qw(w week weeks)),
89 map(($_, 60*60*24*30), qw(M month months)),
90 map(($_, 60*60*24*365), qw(y year years)) );
91 #>>>
92  
93 # This code is taken from Time::Duration::Parse, except if it isn't
94 # understood it just passes it through and it adds the current time.
95 42 ▶sub _parse_duration {
Subroutine _parse_duration: called CC=6 SCAR=17.9
96 42 my ( $self, $timespec ) = @_;
97 42 my $orig_timespec = $timespec;
98  
99 # Treat a plain number as a number of seconds (and parse it later)
100 42 ▶ if ( $timespec =~ /^\s*([-+]?\d+(?:[.,]\d+)?)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timespec =~ /^\s*([-+]?\d+(?:[.,]\d+)?)\s*$/) 2 40 42
101 2 $timespec = "$1s";
102 }
103  
104 # Convert hh:mm(:ss)? to something we understand
105 42 $timespec =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g;
106 42 $timespec =~ s/\b(\d+):(\d\d)\b/$1h $2m/g;
107  
108 42 my $duration = 0;
109 42 ▶ while ( $timespec
Branch: 1 branch
BranchTrueFalseTotal
if ($timespec =~ s/^\s*([-+]?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i) 46 39 85
110 =~ s/^\s*([-+]?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i )
111 {
112 46 my ( $amount, $unit ) = ( $1, $2 );
113 46 ▶ $unit = lc($unit) unless length($unit) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $unit == 1 21 25 46
114  
115 46 ▶ if ( my $value = $Units{$unit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $Units{$unit}) { } 43 3 46
116 43 $amount =~ s/,/./;
117 43 $duration += $amount * $value;
118 }
119 else {
120 3 return $orig_timespec;
121 }
122 }
123  
124 39 ▶ if ( $timespec =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timespec =~ /\S/) 6 33 39
125 6 return $orig_timespec;
126 }
127  
128 33 return sprintf "%.0f", $duration;
129 }
130  
131 213 1;
132  
133 __END__
« blib/lib/Dancer2/Logger/File.pm ↑ Summary blib/lib/Dancer2/Serializer/Mutable.pm »