blib/lib/Azure/SAS/Timestamp.pm

filter: Statementstmt 85.0% 34 / 40 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 3 totaltotal 77.7% 49 / 63 CC 7 SCAR 12.1
CC
7
Cov
81%
__ANON__
5.0
parse_timestamp_str
4.9
epoch
2.0
CRAP 3.4
↑ Summary
1 package Azure::SAS::Timestamp;
2 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Types::Standard qw(Int Str InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Regexp::Common 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.0.4';
8  
9 3 has time_piece => (
10 is => 'rw',
11 isa => InstanceOf['Time::Piece']
12 );
13  
14 6 ▶sub sas_time {
Subroutine sas_time: called CC=1 SCAR=0.0
Pod undocumented
15 ## Azure SAS requires time in UTC, but the timestamp must be "Z", not "UTC"
16 6 my $self = shift;
17 6 return $self->time_piece->strftime( '%Y-%m-%dT%TZ' );
18 }
19  
20 0 ▶sub epoch {
Subroutine epoch: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my $self = shift;
22 0 return $self->time_piece->epoch;
23 }
24  
25 6 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
26 6 my ( $orig, $class, @args ) = @_;
27  
28 6 my $arg = $args[0];
29  
30 6 my $time_piece;
31 6 my $int_check = Int;
32 6 my $str_check = Str;
33 6 my $tp_check = InstanceOf['Time::Piece'];
34 6 my $dt_check = InstanceOf['DateTime'];
35  
36 ## If the argument is an integer, assume it's an epoch stamp.
37 6 ▶ if ( $int_check->check( $arg ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($int_check->check($arg)) { } 1 5 6
elsif ($str_check->check($arg)) { } 3 2 5
elsif ($tp_check->check($arg)) { } 1 1 2
elsif ($dt_check->check($arg)) { } 1 0 1
38 1 $time_piece = Time::Piece->strptime( $arg, '%s');
39 }
40 elsif ( $str_check->check( $arg ) ) {
41 3 $time_piece = parse_timestamp_str( $arg );
42 }
43 elsif ( $tp_check->check( $arg ) ) { ## If $arg is a Time::Piece object
44 1 $time_piece = $arg;
45 }
46 elsif ( $dt_check->check( $arg ) ) {
47 1 $time_piece = Time::Piece->strptime( $arg->epoch, '%s' );
48 }
49 else {
50 0 die "Couldn't parse argument to Time::Piece";
51 }
52
53 6 return { time_piece => $time_piece }
54  
55 3 };
56  
57  
58  
59 3 ▶sub parse_timestamp_str {
Subroutine parse_timestamp_str: called CC=3 SCAR=16.0
Pod undocumented
60 3 my $str = shift;
61
62 ## NOTE: It looks like Time::Piece strptime will not support timezone by
63 ## name, so we can't support arguments where the zone is expressed this
64 ## way (for example 2020-05-10T10:00:00CST). It (maybe?) can parse an
65 ## offset. Also, DateTime could (of course) handle this. Of course,
66 ## DateTime will not handle parsing the string as well. For now, we won't
67 ## support alternate time zones.
68 3 ▶ if ( $str =~ /^
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^ (?<timestamp> # Start capture $1 \d{4} - \d{2} - \d{2} T \d{2}:\d{2} # Matches YYYY-MM-DDTHH:mm (:\d{2})? # Optionally matches :SS ) (?<timezone> Z|\w{3})? ## Could have timezone or literal "Z" $/x) 3 0 3
69 (?<timestamp> # Start capture $1
70 \d{4} - \d{2} - \d{2} T \d{2}:\d{2} # Matches YYYY-MM-DDTHH:mm
71 (:\d{2})? # Optionally matches :SS
72 )
73 (?<timezone> Z|\w{3})? ## Could have timezone or literal "Z"
74 $/x
75 ) {
76 3 return Time::Piece->strptime( $1, '%Y-%m-%dT%T' );
77 }
78  
79 0 ▶ if ( $str =~ /^\d{4} - \d{2} - \d{2}$/) { ## Matches YYYY-MM-DD
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\d{4} - \d{2} - \d{2}$/) { } 0 0 0
80 0 return Time::Piece->strptime( $str, '%Y-%m-%d' );
81
82 }
83  
84 else {
85 0 die("$str does not look like an iso8601 datetime");
86 }
87  
88 }
89  
90  
91 3 1;
92  
93 __END__
↑ Summary