/usr/share/perl5/Data/FormValidator/Constraints/Dates.pm is in libdata-formvalidator-perl 4.81-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | package Data::FormValidator::Constraints::Dates;
use Exporter 'import';
use 5.005;
use strict;
our %EXPORT_TAGS = ( 'all' => [ qw() ] );
our @EXPORT_OK = (
'date_and_time',
@{ $EXPORT_TAGS{'all'} }
);
our @EXPORT = qw(
match_date_and_time
);
our $VERSION = '4.81';
sub date_and_time {
my $fmt = shift;
return sub {
my $self = shift;
$self->set_current_constraint_name('date_and_time');
return match_date_and_time($self,\$fmt);
}
}
sub match_date_and_time {
my $self = shift;
my $fmt_ref = shift || die q!date_and_time: need format parameter. Be sure to pass it by reference, like this: \'MM/DD/YYYY'!;
my $fmt = $$fmt_ref;
require Date::Calc;
import Date::Calc (qw/check_date check_time/);
my $format = _prepare_date_format($fmt);
my ($date,$Y,$M,$D,$h,$m,$s) = _parse_date_format($format,$self->get_current_constraint_value);
return if not defined $date;
# We need to check the date if we find any in the format string, otherwise, it succeeds
my $date_test = 1;
$date_test = check_date($Y,$M,$D) if ($fmt =~ /[YMD]/) ;
# If we find a time, check that
my $time_test = 1;
$time_test = check_time($h,$m,$s) if ($fmt =~ /[hms]/) ;
# If either the time or date fails, it all fails
return ($date_test && $time_test) ? $date : undef;
}
sub _prepare_date_format {
my $format = shift;
# Originally by Jan Krynicky
# TODO: check that only valid characters appear in the format
# The logic should be: for any character A-Z in the format string,
# die if it's not one of: Y M D h m s p
my ($i, @order) = 0;
$format =~ s{(Y+|M+|D+|h+|m+|s+|pp)(\?)?}{
my ($chr,$q) = ($1,$2);
$chr = '' if not defined $chr;
$q = '' if not defined $chr;
$order[$i++] = substr($chr,0,1);
if ($chr eq 'pp') {
"(AM|PM|am|pm)"
} else {
'(' . ('\d' x length($chr)) . ($q ? $q : "") . ")"
}
}ge;
$format = qr/^((?:$format))$/;
return [$format, \@order];
}
sub _parse_date_format {
# Originally by Jan Krynicky
my ($format, $date) = @_;
my ($untainted_date,@data) = ($date =~ $format->[0])
or return;
my %result;
for(my $i = 0; $i <= $#data; $i++) {
$result{$format->[1]->[$i]} ||= $data[$i];
}
if (exists $result{p}) {
$result{h} += 12 if ($result{p} eq 'PM' and $result{h} != 12);
$result{h} = 0 if ($result{p} eq 'AM' and $result{h} == 12);
}
return $untainted_date, map {defined $result{$_} ? $result{$_} : 0} qw(Y M D h m s);
}
1;
__END__
=head1 NAME
Data::FormValidator::Constraints::Dates - Validate Dates and Times
=head1 SYNOPSIS
use Data::FormValidator::Constraints::Dates qw(date_and_time);
# In a DFV profile...
constraint_methods => {
# 'pp' denotes AM|PM for 12 hour representation
my_time_field => date_and_time('MM/DD/YYYY hh:mm:ss pp'),
}
=head1 DESCRIPTION
=head2 date_and_time
B<Note:> This is a new module is a new addition to Data::FormValidator and is
should be considered "Beta".
This constraint creates a regular expression based on the format string
passed in to validate your date against. It understands the following symbols:
Y year (numeric)
M month (numeric)
D day (numeric)
h hour
m minute
s second
p AM|PM
Other parts of the string become part of the regular expression, so you can
do perlish things like this to create more complex expressions:
'MM?/DD?/YYYY|YYYY-MM?-DD?'
Internally L<Date::Calc> is used to test the functions.
=head1 BACKWARDS COMPATIBILITY
This older, more awkward interface is supported:
# In a Data::FormValidator Profile:
validator_packages => [qw(Data::FormValidator::Constraints::Dates)],
constraints => {
date_and_time_field => {
constraint_method => 'date_and_time',
params=>[\'MM/DD/YYYY hh:mm:ss pp'], # 'pp' denotes AM|PM for 12 hour representation
},
}
=head1 SEE ALSO
=over
=item o
L<Data::FormValidator>
=item o
L<Data::FormValidator::Constraints::DateTime> - This alternative features
returning dates as DateTime objects and validating against the date formats
required for the MySQL and PostgreSQL databases.
=back
=head1 AUTHOR
Mark Stosberg, E<lt>mark@summersault.comE<gt>
Featuring clever code by Jan Krynicky.
=head1 COPYRIGHT AND LICENSE
Copyright 2003-2005 by Mark Stosberg
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|