File Coverage

blib/lib/HTML/Widget/Constraint/DateTime.pm
Criterion Covered Total %
statement 33 33 100.0
branch 4 6 66.6
condition 15 36 41.6
subroutine 5 5 100.0
pod 1 1 100.0
total 58 81 71.6


line stmt bran cond sub pod time code
1             package HTML::Widget::Constraint::DateTime;
2              
3 88     88   82307 use warnings;
  88         191  
  88         2791  
4 88     88   490 use strict;
  88         183  
  88         2983  
5 88     88   466 use base 'HTML::Widget::Constraint';
  88         187  
  88         7861  
6 88     88   84047 use Date::Calc;
  88         4230704  
  88         51919  
7              
8             =head1 NAME
9              
10             HTML::Widget::Constraint::DateTime - DateTime Constraint
11              
12             =head1 SYNOPSIS
13              
14             my $c =
15             $widget->constraint( 'DateTime', 'year', 'month', 'day', 'hour',
16             'minute', 'second' );
17              
18             =head1 DESCRIPTION
19              
20             DateTime Constraint.
21              
22             =head1 METHODS
23              
24             =head2 process
25              
26             =cut
27              
28             sub process {
29 2     2 1 3 my ( $self, $w, $params ) = @_;
30              
31             # PS: Commented out. What if I don't want to provide time? I could use
32             # 'Date', but, all these fields are allowed to be 0 anyway. And I may
33             # not want to check seconds or minutes. So we'll change this to #3
34              
35             # return []
36             # unless ( $self->names && @{ $self->names } == 6 );
37 2 50 33     8 return [] unless ( $self->names && @{ $self->names } >= 3 );
  2         17  
38              
39 2         15 my ( $year, $month, $day, $hour, $min, $sec ) = @{ $self->names };
  2         6  
40 2         14 my $y = $params
41             ->{$year}; # 0 is a valid year, but Date::Calc doesn't support it
42 2         3 my $mo = $params->{$month};
43 2         3 my $d = $params->{$day};
44 2   50     7 my $h = $params->{$hour} || 0;
45 2   50     14 my $mi = $params->{$min} || 0;
46 2   50     6 my $s = $params->{$sec} || 0;
47              
48             # PS: Commented out. This is silly. Hour, minute, and second
49             # can all be validly 0.
50             # return [] unless ( $y && $mo && $d && $h && $mi && $s );
51 2 50 33     38 return [] unless ( $y && $mo && $d );
      33        
52              
53 2         3 my $results = [];
54              
55 2 100 33     61 unless ( $y =~ /^\d+$/
      33        
      33        
      33        
      33        
      66        
      66        
56             && $mo =~ /^\d+$/
57             && $d =~ /^\d+$/
58             && $h =~ /^\d+$/
59             && $mi =~ /^\d+$/
60             && $s =~ /^\d+$/
61             && Date::Calc::check_date( $y, $mo, $d )
62             && Date::Calc::check_time( $h, $mi, $s ) )
63             {
64 1         39 push @$results, HTML::Widget::Error->new(
65             { name => $year, message => $self->mk_message } );
66 1         13 push @$results, HTML::Widget::Error->new(
67             { name => $month, message => $self->mk_message } );
68 1         34 push @$results, HTML::Widget::Error->new(
69             { name => $day, message => $self->mk_message } );
70 1         13 push @$results, HTML::Widget::Error->new(
71             { name => $hour, message => $self->mk_message } );
72 1         13 push @$results, HTML::Widget::Error->new(
73             { name => $min, message => $self->mk_message } );
74 1         12 push @$results, HTML::Widget::Error->new(
75             { name => $sec, message => $self->mk_message } );
76             }
77 2         84 return $results;
78             }
79              
80             =head1 AUTHOR
81              
82             Sebastian Riedel, C
83              
84             =head1 LICENSE
85              
86             This library is free software, you can redistribute it and/or modify it under
87             the same terms as Perl itself.
88              
89             =cut
90              
91             1;