File Coverage

blib/lib/Salvation/TC/Type/Date.pm
Criterion Covered Total %
statement 18 29 62.0
branch 0 14 0.0
condition 0 12 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 25 63 39.6


line stmt bran cond sub pod time code
1             package Salvation::TC::Type::Date;
2              
3 4     4   18 use strict;
  4         4  
  4         102  
4 4     4   12 use warnings;
  4         5  
  4         92  
5              
6 4     4   14 use base 'Salvation::TC::Type';
  4         6  
  4         235  
7              
8 4     4   1134 use Salvation::TC::Type::Time ();
  4         6  
  4         54  
9 4     4   15 use Salvation::TC::Exception::WrongType ();
  4         6  
  4         35  
10 4     4   1652 use Time::Piece ();
  4         29409  
  4         751  
11              
12             my $re = qr/^(\d{1,2})[\.\/\-](\d{1,2})[\.\/\-](\d{4})\s*(\d+:\d+(:\d+)*?)?$/;
13              
14             sub Check {
15              
16 0     0 1   my ( $class, $date ) = @_;
17              
18 0           eval {
19              
20 0 0 0       die "Wrong date format. Expected day[.-/]month[./-]year time" if ( ! defined( $date ) || $date !~ $re );
21              
22 0           my ( $day, $month, $year, $time ) = ( $1, $2, $3, $4 );
23              
24 0 0 0       die "Month must be between 1 and 12. Current value is $month." unless $month >= 1 && $month <= 12;
25 0 0 0       die "Day must be between 1 and 31. Current value is $day." unless $day >= 1 && $day <= 31;
26 0 0 0       die "Year must be between 1900 and 2110. Current value is $year." unless $year >= 1900 && $year <= 2110;
27              
28 0 0         Salvation::TC::Type::Time->Check( $time ) if ( $time );
29              
30 0 0         defined( Time::Piece -> strptime( "$year-$month-$day", '%Y-%m-%d' ) ) || die "Unknow date format: $date.";
31             };
32              
33 0 0         if( $@ ) {
34              
35 0           Salvation::TC::Exception::WrongType -> throw( type => 'Date', value => $date );
36             };
37             }
38              
39             1;
40             __END__