File Coverage

blib/lib/Protocol/XMLRPC/Value/DateTime.pm
Criterion Covered Total %
statement 27 28 96.4
branch 3 6 50.0
condition 7 15 46.6
subroutine 6 6 100.0
pod 3 3 100.0
total 46 58 79.3


line stmt bran cond sub pod time code
1             package Protocol::XMLRPC::Value::DateTime;
2              
3 9     9   820 use strict;
  9         17  
  9         312  
4 9     9   55 use warnings;
  9         13  
  9         227  
5              
6 9     9   40 use base 'Protocol::XMLRPC::Value';
  9         15  
  9         3652  
7              
8             require Time::Local;
9              
10 1     1 1 751 sub type {'datetime'}
11              
12             sub parse {
13 4     4 1 6 my $class = shift;
14 4         10 my ($datetime) = @_;
15              
16 4         18 my ($year, $month, $mday, $hour, $minute, $second) =
17             ($datetime =~ m/(\d\d\d\d)(\d\d)(\d\d)T(\d\d):(\d\d):(\d\d)/);
18              
19 4 50 66     76 die "Invalid 'Datetime' value"
      66        
      33        
      33        
      33        
20             unless defined $year
21             && defined $month
22             && defined $mday
23             && defined $hour
24             && defined $minute
25             && defined $second;
26              
27 3         5 my $epoch;
28              
29             # Prevent crash
30             eval {
31 3         14 $epoch =
32             Time::Local::timegm($second, $minute, $hour, $mday, --$month, $year);
33 3         93 1;
34 3 50       38 } or do {
35 0         0 die "Invalid 'DateTime' value";
36             };
37              
38 3 50       9 die "Invalid 'DateTime' value" if $epoch < 0;
39              
40 3         15 return $class->new($epoch);
41             }
42              
43             sub to_string {
44 3     3 1 20 my $self = shift;
45              
46 3         7 my $value = $self->value;
47              
48 3         22 my ($second, $minute, $hour, $mday, $month, $year, $wday) = gmtime($value);
49              
50 3         4 $year += 1900;
51 3         3 $month++;
52              
53             #19980717T14:08:55
54 3         15 $value = sprintf('%d%02d%02dT%02d:%02d:%02d',
55             $year, $month, $mday, $hour, $minute, $second);
56              
57 3         13 return "$value";
58             }
59              
60             1;
61             __END__