This file is indexed.

/usr/share/perl5/Data/FormValidator/Constraints/DateTime.pm is in libdata-formvalidator-constraints-datetime-perl 1.11-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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
package Data::FormValidator::Constraints::DateTime;
use strict;
use DateTime;
use DateTime::Format::Strptime;
use Scalar::Util qw(blessed);
use Exporter;
use Carp qw(croak);
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
    to_datetime
    ymd_to_datetime
    before_today
    after_today
    ymd_before_today
    ymd_after_today
    before_datetime
    after_datetime
    between_datetimes
    to_mysql_datetime
    to_mysql_date
    to_mysql_timestamp
    to_pg_datetime
);

our %EXPORT_TAGS = (
    all     => \@EXPORT_OK,
    mysql   => [qw(to_mysql_datetime to_mysql_date to_mysql_timestamp)],
    pg      => [qw(to_pg_datetime)],
);
our $VERSION = '1.11';

=head1 NAME

Data::FormValidator::Constraints::DateTime - D::FV constraints for dates and times

=head1 DESCRIPTION

This package provides constraint routines for L<Data::FormValidator> for
dealing with dates and times. It provides an easy mechanism for validating
dates of any format (using strptime(3)) and transforming those dates (as long
as you 'untaint' the fields) into valid L<DateTime> objects, or into strings 
that would be properly formatted for various database engines.

=head1 ABSTRACT

  use Data::FormValidator;
  use Data::FormValidator::Constraints::DateTime qw(:all);
    
  # create our profile
  my $profile = {
      required                => [qw(my_date)],
      constraint_methods      => {
          my_date   => to_datetime('%D'), # in the format MM/DD/YYYY
      },
      untaint_all_constraints => 1,
  };

  # validate 'my_date'
  my $results = Data::FormValidator->check($my_input, $profile);

  if( $results->success ) {
    # if we got here then $results->valid('my_date')
    # is a valid DateTime object 
    my $datetime = $results->valid('my_date');
    .
    .
  }

=head1 STRPTIME FORMATS

Most of the validation routines provided by this module use
strptime(3) format strings to know what format your date string
is in before we can process it. You specify this format for each
date you want to validate using by passing it to constraint
generation routine (see the example above).

We use L<DateTime::Format::Strptime> for this transformation. 
If you need a list of these formats (if you haven't yet committed 
them to memory) you can see the strptime(3) man page (if you are 
on a *nix system) or you can see the L<DateTime::Format::Strptime> 
documentation.

There are however some routines that can live without the format
param. These include routines which try and validate according
to rules for a particular database (C<< to_mysql_* >> and 
C<< to_pg_* >>). If no format is provided, then we will attempt to
validate according to the rules for that datatype in that database
(using L<DateTime::Format::MySQL> and L<DateTime::Format::Pg>).
Here are some examples:

without a format param

 my $profile = {
   required                => [qw(my_date)],
   constraint_methods      => {
       my_date => to_mysql_datetime(),
   },
 };

with a format param

 my $profile = {
   required                => [qw(my_date)],
   constraint_methods      => {
       my_date => to_mysql_datetime('%m/%d/%Y'),
   },
 };

=head2 DateTime::Format Objects

Using strptime(3) format strings gives a lot of flexibility, but sometimes
not enough. Suppose you have a web form that allows the user to input a date
in the format '11/21/2006' or simply '11/21/06'. A simple format string is
not enough. To take full advantage of the DateTime project, any place that
you can pass in a strptime(3) format string, you can also pass in a
L<DateTime::Format> object. To solve the above problem you might have code
that looks like this:

  # your formatter code
  package MyProject::DateTime::FlexYear;
  use DateTime::Format::Strptime;

  use DateTime::Format::Builder (
    parsers => { 
      parse_datetime => [
        sub { eval { DateTime::Format::Strptime->new(pattern => '%m/%d/%Y')->parse_datetime($_[1]) } },
        sub { eval { DateTime::Format::Strptime->new(pattern => '%m/%d/%y')->parse_datetime($_[1]) } },
      ] 
    }
  );

  1;

  # in your web validation code
  my $profile = {
    required           => [qw(my_date)],
    constraint_methods => {
        my_date => to_mysql_datetime(MyProject::DateTime::FlexYear->new()),
    },
  };


=head1 VALIDATION ROUTINES

Following is the list of validation routines that are provided
by this module.

=head2 to_datetime

The routine will validate the date aginst a strptime(3) format and
change the date string into a DateTime object. This routine B<must> 
have an accompanying L<strptime|DateTime::Format::Strptime> format param.

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub to_datetime {
    my $format = shift;
    # dereference stuff if we need to

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
        return match_to_datetime($dfv, $format);
    }
}

sub match_to_datetime {
    my ($dfv, $format) = @_;
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
    # get the DateTime
    my $dt = _get_datetime_from_strp($value, $format);
    return $dt;
}

sub _get_datetime_from_strp {
    my ($value, $format) = @_;
    $format = $$format if( ref $format eq 'SCALAR' );
    my $formatter;
    # if we have a simple scalar for the format
    if( ! ref $format ) {
        # create the formatter
        $formatter = DateTime::Format::Strptime->new(
            pattern => $format
        );
    # else we assume it's a DateTime::Format based object
    } else {
        $formatter = $format;
    }

    # create the DateTime object
    my $dt;
    eval { $dt = $formatter->parse_datetime($value); };
    # set the formatter (if we can) so that the object
    # stringifies to the same format as we parsed
    $dt->set_formatter($formatter)
        if( $dt && $formatter->can('format_datetime') );
    return $dt;
}

=head2 ymd_to_datetime

This routine is used to take multiple inputs (one each for the
year, month, and day) and combine them into a L<DateTime> object,
validate the resulting date, and give you the resulting DateTime
object in your C<< valid() >> results. It must receive as C<< params >>
the year, month, and day inputs in that order. You may also specify
additional C<< params >> that will be interpretted as 'hour', 'minute'
and 'second' values to use. If none are provided, then the time '00:00:00'
will be used.

 my $profile = {
   required                => [qw(my_year)],
   constraint_methods      => {
      my_year => ymd_to_datetime(qw(my_year my_month my_day my_hour my_min my_sec)),
   },
 };

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub ymd_to_datetime {
    my ($year, $month, $day, $hour, $min, $sec) = @_;
    
    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
        my $data = $dfv->get_input_data(as_hashref => 1);
        return match_ymd_to_datetime(
            $dfv, 
            _get_value($year,  $data),
            _get_value($month, $data),
            _get_value($day,   $data),
            _get_value($hour,  $data),
            _get_value($min,   $data),
            _get_value($sec,   $data),
        );
    };
}

sub _get_value {
    my ($value, $data) = @_;
    if( $value && exists $data->{$value} ) {
        return $data->{$value};
    } else {
        return $value;
    }
}

sub match_ymd_to_datetime {
    my ($dfv, $year, $month, $day, $hour, $min, $sec);

    # if we were called as a 'constraint_method'
    if( ref $_[0] ) {
        ($dfv, $year, $month, $day, $hour, $min, $sec) = @_;
    # else we were called as a 'constraint'
    } else {
        ($year, $month, $day, $hour, $min, $sec) = @_;
    }
        
    # make sure year, month and day are positive numbers
    if( 
        defined $year && $year ne "" 
        && defined $month && $month ne "" 
        && defined $day && $day ne "" 
    ) {
        # set the defaults for time if we don't have any
        $hour ||= 0;
        $min  ||= 0;
        $sec  ||= 0;
    
        my $dt;
        eval {
            $dt = DateTime->new(
                year    => $year,
                month   => $month,
                day     => $day,
                hour    => $hour,
                minute  => $min,
                second  => $sec,
            );
        };
        
        return $dt;
    } else {
        return;
    }
}

=head2 before_today

This routine will validate the date and make sure it less than or
equal to today (using C<< DateTime->today >>). It takes one param
which is the <strptime|DateTime::Format::Strptime> format string for the date.

If it validates and you tell D::FV to untaint this parameter it will be
converted into a DateTime object.

 # make sure they weren't born in the future
 my $profile = {
   required                => [qw(birth_date)],
   constraint_methods      => {
      birth_date => before_today('%m/%d/%Y'),
   },
 };

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub before_today {
    my $format = shift;

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
        return match_before_today($dfv, $format);
    };
}

sub match_before_today {
    my ($dfv, $format) = @_;
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
    # get the DateTime
    my $dt = _get_datetime_from_strp($value, $format);
    my $dt_target = DateTime->today();
    # if we have valid DateTime objects and they have the correct
    # temporaral relationship
    if( $dt && $dt_target && $dt <= $dt_target ) {
        return $dt;
    } else {
        return;
    }
}

=head2 after_today

This routine will validate the date and make sure it is greater
than or equal to today (using C<< DateTime->today() >>). It takes
only one param, which is the L<strptime|DateTime::Format::Strptime> format for the date being
validated.

If it validates and you tell D::FV to untaint this parameter it will be
converted into a DateTime object.

 # make sure the project isn't already due
 my $profile = {
   required                => [qw(death_date)],
   constraint_methods      => {
      death_date => after_today('%m/%d/%Y'),
   },
   untaint_all_constraints => 1,
 };

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub after_today {
    my $format = shift;

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
        return match_after_today($dfv, $format);
    };
}

sub match_after_today {
    my ($dfv, $format) = @_;
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
    # get the DateTime
    my $dt = _get_datetime_from_strp($value, $format);
    my $dt_target = DateTime->today();
    # if we have valid DateTime objects and they have the correct
    # temporaral relationship
    if( $dt && $dt_target && $dt >= $dt_target ) {
        return $dt;
    } else {
        return;
    }
}


=head2 ymd_before_today

This routine will validate the date and make sure it less than or
equal to today (using C<< DateTime->today >>). It works just like
L<ymd_to_datetime> in the parameters it takes.

If it validates and you tell D::FV to untaint this parameter it will be
converted into a DateTime object.

 # make sure they weren't born in the future
 my $profile = {
   required                => [qw(birth_date)],
   constraint_methods      => {
      birth_date => ymd_before_today(qw(dob_year dob_month dob_day)),
   },
   untaint_all_constraints => 1,
 };

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub ymd_before_today {
    my ($year, $month, $day, $hour, $min, $sec) = @_;
    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );

        my $data = $dfv->get_input_data(as_hashref => 1);
        return match_ymd_before_today(
            $dfv, 
            _get_value($year,  $data),
            _get_value($month, $data),
            _get_value($day,   $data),
            _get_value($hour,  $data),
            _get_value($min,   $data),
            _get_value($sec,   $data),
        );
    };
}

sub match_ymd_before_today {
    my $dt = match_ymd_to_datetime(@_);
    if( $dt && ( $dt <= DateTime->today ) ) {
      return $dt;
    }
    return; # if we get here then it's false
}

=head2 ymd_after_today

This routine will validate the date and make sure it greater than or
equal to today (using C<< DateTime->today >>). It works just like
L<ymd_to_datetime> in the parameters it takes.

If it validates and you tell D::FV to untaint this parameter it will be
converted into a DateTime object.

 # make sure the project isn't already due
 my $profile = {
   required                => [qw(due_date)],
   constraint_methods      => {
      birth_date => ymd_after_today(qw(dob_year dob_month dob_day)),
   },
   untaint_all_constraints => 1,
 };

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub ymd_after_today {
    my ($year, $month, $day, $hour, $min, $sec) = @_;
    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );

        my $data = $dfv->get_input_data(as_hashref => 1);
        return match_ymd_after_today(
            $dfv, 
            _get_value($year,  $data),
            _get_value($month, $data),
            _get_value($day,   $data),
            _get_value($hour,  $data),
            _get_value($min,   $data),
            _get_value($sec,   $data),
        );
    };
}

sub match_ymd_after_today {
    my $dt = match_ymd_to_datetime(@_);
    if( $dt && ( $dt >= DateTime->today ) ) {
      return $dt;
    }
    return; # if we get here then it's false
}

=head2 before_datetime

This routine will validate the date and make sure it occurs before
the specified date. It takes two params: 

=over

=item * first, the L<strptime|DateTime::Format::Strptime> format 

(for both the date we are validating and also the date we want to 
compare against) 

=item * second, the date we are comparing against. 

This date we are comparing against can either be a specified date (using 
a scalar ref), or a named parameter from your form (using a scalar name).

=back

If it validates and you tell D::FV to untaint this parameter it will be
converted into a DateTime object.

 # make sure they were born before 1979
 my $profile = {
   required                => [qw(birth_date)],
   constraint_methods      => {
      birth_date => before_datetime('%m/%d/%Y', '01/01/1979'),
   },
   untaint_all_constraints => 1,
 };

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub before_datetime {
    my ($format, $date) = @_;
    # dereference stuff if we need to
    $date = $$date if( ref $date eq 'SCALAR' );

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );

        # are we using a real date or the name of a parameter
        my $data = $dfv->get_input_data(as_hashref => 1);
        $date = $data->{$date} if( $data->{$date} );
        return match_before_datetime($dfv, $format, $date);
    };
}

sub match_before_datetime {
    my ($dfv, $format, $target_date) = @_;
    $target_date = $$target_date if( ref $target_date eq 'SCALAR' );
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
    # get the DateTime
    my $dt = _get_datetime_from_strp($value, $format);
    my $dt_target = _get_datetime_from_strp($target_date, $format);
    # if we have valid DateTime objects and they have the correct
    # temporaral relationship
    if( $dt && $dt_target && $dt < $dt_target ) {
        return $dt;
    } else {
        return;
    }
}

=head2 after_datetime

This routine will validate the date and make sure it occurs after
the specified date. It takes two params: 

=over

=item * first, the L<strptime|DateTime::Format::Strptime> format 

(for both the date we are validating and also the date we want to 
compare against)

=item * second, the date we are comparing against. 

This date we are comparing against can either be a specified date (using a 
scalar ref), or a named parameter from your form (using a scalar name).

=back

 # make sure they died after they were born
 my $profile = {
   required                => [qw(birth_date death_date)],
   constraint_methods      => {
      death_date => after_datetime('%m/%d/%Y', 'birth_date'),
   },
   untaint_all_constraints => 1,
 };

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub after_datetime {
    my ($format, $date) = @_;
    # dereference stuff if we need to
    $date = $$date if( ref $date eq 'SCALAR' );

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );

        # are we using a real date or the name of a parameter
        my $data = $dfv->get_input_data(as_hashref => 1);
        $date = _get_value($date, $data);
        return match_after_datetime($dfv, $format, $date);
    };
}

sub match_after_datetime {
    my ($dfv, $format, $target_date) = @_;
    $target_date = $$target_date if( ref $target_date eq 'SCALAR' );
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
    # get the DateTime
    my $dt = _get_datetime_from_strp($value, $format);
    my $dt_target = _get_datetime_from_strp($target_date, $format);
    # if we have valid DateTime objects and they have the correct
    # temporaral relationship
    if( $dt && $dt_target && $dt > $dt_target ) {
        return $dt;
    } else {
        return;
    }
}

=head2 between_datetimes

This routine will validate the date and make sure it occurs after
the first specified date and before the second specified date. It 
takes three params: 

=over

=item * first, the L<strptime|DateTime::Format::Strptime> format 

(for both the date we are validating and also the dates we want to 
compare against)

=item * second, the first date we are comparing against. 

=item * third, the second date we are comparing against. 

This date (and the second) we are comparing against can either be a specified date 
(using a scalar ref), or a named parameter from your form (using a scalar name).

=back

 # make sure they died after they were born
 my $profile = {
   required                => [qw(birth_date death_date marriage_date)],
   constraint_methods      => {
      marriage_date => between_datetimes('%m/%d/%Y', 'birth_date', 'death_date'),
   },
   untaint_all_constraints => 1,
 };

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub between_datetimes {
    my ($format, $target1, $target2) = @_;
    # dereference stuff if we need to
    $target1 = $$target1 if( ref $target1 eq 'SCALAR' );
    $target2 = $$target2 if( ref $target2 eq 'SCALAR' );

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );

        # are we using a real date or the name of a parameter
        my $data = $dfv->get_input_data(as_hashref => 1);
        $target1 = _get_value($target1, $data);
        $target2 = _get_value($target2, $data);
        return match_between_datetimes($dfv, $format, $target1, $target2);
    }
}

sub match_between_datetimes {
    my ($dfv, $format, $target1, $target2) = @_;
    $target1 = $$target1 if( ref $target1 eq 'SCALAR' );
    $target2 = $$target2 if( ref $target2 eq 'SCALAR' );

    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
    # get the DateTime
    my $dt = _get_datetime_from_strp($value, $format);
    my $dt_target1 = _get_datetime_from_strp($target1, $format);
    my $dt_target2 = _get_datetime_from_strp($target2, $format);
    # if we have valid DateTime objects and they have the correct
    # temporaral relationship
    if( 
        $dt 
        && $dt_target1 
        && $dt_target2 
        && $dt > $dt_target1 
        && $dt < $dt_target2 
    ) {
        return $dt;
    } else {
        return;
    }
}

=head1 DATABASE RELATED VALIDATION ROUTINES

=head2 to_mysql_datetime

The routine will change the date string into a DATETIME datatype
suitable for MySQL. If you don't provide a format parameter then
this routine will just validate the data as a valid MySQL DATETIME
datatype (using L<DateTime::Format::MySQL>).

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub to_mysql_datetime {
    my $format = shift;

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
        return match_to_mysql_datetime($dfv, $format);
    }
}

sub match_to_mysql_datetime {
    my ($dfv, $format) = @_;
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;

    # make sure they have DateTime::Format::MySQL
    eval { require DateTime::Format::MySQL; };
    die "DateTime::Format::MySQL is required to use this routine"
        if( $@ );
    my $dt;

    # if they gave us a format (through params as a scalar ref)
    # then translate the value
    if( $format ) {
        $dt = _get_datetime_from_strp($value, $format);
    # else there is no format, so just use parse_datetime
    } else {
        eval { $dt = DateTime::Format::MySQL->parse_datetime($value) };
    }
    if( $dt ) {
        return DateTime::Format::MySQL->format_datetime($dt); 
    } else {
        return undef;
    }
}

=head2 to_mysql_date

The routine will change the date string into a DATE datatype
suitable for MySQL. If you don't provide a format param then
this routine will validate the data as a valid DATE datatype
in MySQL (using L<DateTime::Format::MySQL>).

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub to_mysql_date {
    my $format = shift;

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
        return match_to_mysql_date($dfv, $format);
    };
}

sub match_to_mysql_date {
    my ($dfv, $format) = @_;
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;

    # make sure they have DateTime::Format::MySQL
    eval { require DateTime::Format::MySQL; };
    die "DateTime::Format::MySQL is required to use this routine"
        if( $@ );
    my $dt;

    # if they gave us a format (through params as a scalar ref)
    # then translate the value
    if( $format ) {
        $dt = _get_datetime_from_strp($value, $format);
    # else there is no format, so just use parse_datetime
    } else {
        eval { $dt = DateTime::Format::MySQL->parse_date($value) };
    }
    if( $dt ) {
        return DateTime::Format::MySQL->format_date($dt);
    } else {
        return undef;
    }
}

=head2 to_mysql_timestamp

The routine will change the date string into a TIMESTAMP datatype
suitable for MySQL. If you don't provide a format then the data
will be validated as a MySQL TIMESTAMP datatype.

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub to_mysql_timestamp {
    my $format = shift;

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
        match_to_mysql_timestamp($dfv, $format);
    };
}

sub match_to_mysql_timestamp {
    my ($dfv, $format) = @_;
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
    my $dt;

    # if they gave us a format (through params as a scalar ref)
    # then translate the value
    if( $format ) {
        $dt = _get_datetime_from_strp($value, $format);
    # else there is no format, so parse into a timestamp
    } else {
        # if it matches a timestamp format YYYYMMDDHHMMSS
        # but we're actually a little looser than that... we take
        # YYYY-MM-DD HH:MM:SS with any other potential separators
        if( $value =~ /(\d{4})\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*(\d{2})/ ) {
            eval { 
                $dt = DateTime->new(
                    year    => $1,
                    month   => $2,
                    day     => $3,
                    hour    => $4,
                    minute  => $5,
                    second  => $6,
                );
            };
        }
    }
    if( $dt ) {
        return $dt->ymd('') . $dt->hms('');
    } else {
        return undef;
    }
}

=head2 to_pg_datetime

The routine will change the date string into a DATETIME datatype
suitable for PostgreSQL. If you don't provide a format then the
data will validated as a DATETIME datatype in PostgreSQL (using
L<DateTime::Format::Pg>).

If the value is untainted (using C<untaint_all_constraints> or
C<untaint_constraint_fields>, it will change the date string into a DateTime
object.

=cut

sub to_pg_datetime {
    my $format = shift;

    return sub {
        my $dfv = shift;
        croak("Must be called using 'constraint_methods'!")
            unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
        match_to_pg_datetime($dfv, $format);
    };
}

sub match_to_pg_datetime {
    my ($dfv, $format) = @_;
    # if $dfv is a ref then we are called as 'constraint_method'
    # else as 'constaint'
    my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;

    # make sure they have DateTime::Format::MySQL
    eval { require DateTime::Format::Pg; };
    die "DateTime::Format::Pg is required to use this routine"
        if( $@ );
    my $dt;

    # if they gave us a format (through params as a scalar ref)
    # then translate the value
    if( $format ) {
        $dt = _get_datetime_from_strp($value, $format);
    # else there is no format, so just use parse_datetime
    } else {
        eval { $dt = DateTime::Format::Pg->parse_datetime($value) };
    }
    if( $dt ) {
        return DateTime::Format::Pg->format_datetime($dt);
    } else {
        return undef;
    }
}


=head1 AUTHOR

Michael Peters <mpeters@plusthree.com>

Thanks to Plus Three, LP (http://www.plusthree.com) for sponsoring my work on this module

=head1 CONTRIBUTORS

=over 

=item Mark Stosberg <mark@summersault.com>

=item Charles Frank <cfrank@plusthree.com>

=item Aaron Ross <aaronelliotross@gmail.com>

=back

=head1 SUPPORT

This module is a part of the larger L<Data::FormValidator> project. If you have
questions, comments, bug reports or feature requests, please join the 
L<Data::FormValidator>'s mailing list.

=head1 CAVEAT

When passing parameters to typical L<Data::FormValidator> constraints you pass
plain scalars to refer to query params and scalar-refs to refer to literals. We get
around that in this module by assuming everything could be refering to a query param,
and if one is not found, then it's a literal. This works well unless you have query
params with names like C<'01/02/2005'> or C<'%m/%d/%Y'>. 

And if you do, shame on you for having such horrible names.

=head1 SEE ALSO

L<Data::FormValidator>, L<DateTime>. L<DateTime::Format::Strptime>,
L<DateTime::Format::MySQL>, L<DateTime::Format::Pg>

=head1 COPYRIGHT & LICENSE

Copyright Michael Peters 2010, all rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.