File Coverage

blib/lib/Apache/Voodoo/Validate/time.pm
Criterion Covered Total %
statement 47 50 94.0
branch 28 32 87.5
condition 9 12 75.0
subroutine 6 6 100.0
pod 0 2 0.0
total 90 102 88.2


line stmt bran cond sub pod time code
1             package Apache::Voodoo::Validate::time;
2              
3             $VERSION = "3.0200";
4              
5 2     2   12 use strict;
  2         5  
  2         121  
6 2     2   10 use warnings;
  2         5  
  2         73  
7              
8 2     2   11 use base("Apache::Voodoo::Validate::Plugin");
  2         6  
  2         1368  
9              
10             sub config {
11 6     6 0 8 my ($self,$c) = @_;
12              
13 6         7 my @e;
14 6 100       14 if (defined($c->{min})) {
15 2         5 my $t = _valid_time($c->{min});
16 2 50       9 if ($t) {
17 2         4 $self->{'min'} = $t;
18             }
19             else {
20 0         0 push(@e,"'min' must be a valid time in either civillian or military form.");
21             }
22             }
23              
24 6 100       14 if (defined($c->{max})) {
25 2         6 my $t = _valid_time($c->{max});
26 2 50       5 if ($t) {
27 2         4 $self->{'max'} = $t;
28             }
29             else {
30 0         0 push(@e,"'max' must be a valid time in either civillian or military form.");
31             }
32             }
33              
34 6         14 return @e;
35             }
36              
37             sub valid {
38 12     12 0 20 my ($self,$v) = @_;
39              
40 12         36 $v = _valid_time($v);
41 12 100       28 unless ($v) {
42 2         8 return undef,'BAD';
43             }
44              
45 10 100 100     37 if (defined($self->{min}) && $v lt $self->{min}) {
46 1         5 return undef,'MIN';
47             }
48              
49 9 100 100     36 if (defined($self->{max}) && $v gt $self->{max}) {
50 2         7 return undef,'MAX';
51             }
52              
53 7         23 return $v;
54             }
55              
56              
57             sub _valid_time {
58 16     16   20 my $time = shift;
59              
60 16         119 $time =~ s/\s*//go;
61 16         30 $time =~ s/\.//go;
62              
63 16 100       75 unless ($time =~ /^\d?\d:[0-5]?\d(:[0-5]?\d)?(am|pm)?$/i) {
64 1         3 return undef;
65             }
66              
67 15         21 my ($h,$m,$s);
68 15 100       46 if ($time =~ s/([ap])m$//igo) {
69 6 100       25 my $pm = (lc($1) eq "p")?1:0;
70              
71 6         19 ($h,$m,$s) = split(/:/,$time);
72              
73             # 12 am is midnight and 12 pm is noon...I've always hated that.
74 6 100 66     27 if ($pm eq '1') {
    100          
75 4 100       13 if ($h < 12) {
    50          
76 3         5 $h += 12;
77             }
78             elsif ($h > 12) {
79 1         2 return undef;
80             }
81             }
82             elsif ($pm eq '0' && $h == 12) {
83 1         3 $h = 0;
84             }
85             }
86             else {
87 9         51 ($h,$m,$s) = split(/:/,$time);
88             }
89              
90             # our regexp above validated the minutes and seconds, so
91             # all we need to check that the hours are valid.
92 14 50 33     70 if ($h < 0 || $h > 23) { return undef; }
  0         0  
93              
94 14 100       28 $s = 0 unless (defined($s));
95 14         54 return sprintf("%02d:%02d:%02d",$h,$m,$s);
96             }
97              
98             1;
99              
100             ################################################################################
101             # Copyright (c) 2005-2010 Steven Edwards (maverick@smurfbane.org).
102             # All rights reserved.
103             #
104             # You may use and distribute Apache::Voodoo under the terms described in the
105             # LICENSE file include in this package. The summary is it's a legalese version
106             # of the Artistic License :)
107             #
108             ################################################################################