File Coverage

blib/lib/Data/MuForm/Field/Time.pm
Criterion Covered Total %
statement 15 26 57.6
branch 6 12 50.0
condition 6 17 35.2
subroutine 2 3 66.6
pod 1 2 50.0
total 30 60 50.0


line stmt bran cond sub pod time code
1             package Data::MuForm::Field::Time;
2             # ABSTRACT: Time field
3 1     1   514 use Moo;
  1         1  
  1         4  
4             extends 'Data::MuForm::Field::Text';
5              
6              
7             has 'use_seconds' => ( is => 'rw', default => 0 );
8             has 'twelve_hour_clock' => ( is => 'rw', default => 0 );
9              
10             our $class_messages = {
11             'time_format' => 'Invalid format for time',
12             };
13              
14             sub get_class_messages {
15 0     0 0 0 my $self = shift;
16             return {
17 0         0 %{ $self->next::method },
  0         0  
18             %$class_messages,
19             }
20             }
21              
22             sub validate {
23 2     2 1 2 my $self = shift;
24              
25 2         3 my $value = $self->value;
26 2         2 my $hour;
27             my $minutes;
28 0         0 my $seconds;
29 2 50       4 if ( $value =~ /:/ ) {
30 2         7 ($hour, $minutes, $seconds) = split(':', $value);
31             }
32             else {
33 0         0 $hour = $value;
34 0         0 $minutes = 0;
35 0         0 $seconds = 0;
36             }
37 2   50     6 $seconds //= 0;
38 2 50       7 my $max_hours = $self->twelve_hour_clock ? 12 : 24;
39 2 50 33     15 unless ( defined $hour && $hour >= 0 && $hour <= $max_hours ) {
      33        
40 0         0 $self->add_error($self->get_message('time_format'));
41             }
42 2 50 33     12 unless ( defined $minutes && $minutes >= 0 && $minutes < 60 ) {
      33        
43 0         0 $self->add_error($self->get_message('time_format'));
44             }
45 2 50 33     7 unless ( $seconds >= 0 && $seconds < 60 ) {
46 0         0 $self->add_error($self->get_message('time_format'));
47             }
48              
49 2 50       5 if ( $self->use_seconds ) {
50 0         0 $self->value(sprintf('%02d:%02d:%02d', $hour, $minutes, $seconds));
51             }
52             else {
53 2         11 $self->value(sprintf('%02d:%02d', $hour, $minutes));
54             }
55              
56             }
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =encoding UTF-8
65              
66             =head1 NAME
67              
68             Data::MuForm::Field::Time - Time field
69              
70             =head1 VERSION
71              
72             version 0.04
73              
74             =head1 DESCRIPTION
75              
76             A 'Time' field;
77              
78             =head1 NAME
79              
80             Data::MuForm::Field::Time
81              
82             =head1 AUTHOR
83              
84             Gerda Shank
85              
86             =head1 COPYRIGHT AND LICENSE
87              
88             This software is copyright (c) 2017 by Gerda Shank.
89              
90             This is free software; you can redistribute it and/or modify it under
91             the same terms as the Perl 5 programming language system itself.
92              
93             =cut