perl isvaliddate function

Dan Langille dan at langille.org
Sat Oct 25 17:01:30 UTC 2014


I’m coding up a sanity check for FreshPorts.  It will verify that field that should be dates are dates.

Feedback welcome.  The system is already using the DATE module, so I’m leveraging that.

Pasted at http://dpaste.com/1H0Q8RR.txt but included below, because.

$ cat test-date.pl
#!/usr/bin/perl

use Date::Parse qw( str2time );

sub IsValidDate($) {
  my ($string) = @_;
  
  my $Date = str2time($string);
  
  return defined($Date);
}

my $a = '2014-11-30 unless *coin ports remain unfixed';

if (IsValidDate($a)) {
  print "'$a' is a valid date\n";
} else {
  print "'$a' is NOT a valid date\n";
}

my $b = '2014-02-30';

if (IsValidDate($b)) {
  print "'$b' is a valid date\n";
} else {
  print "'$b' is NOT a valid date\n";
}

my $c = '2014-02-28';

if (IsValidDate($c)) {
  print "'$c' is a valid date\n";
} else {
  print "'$c' is NOT a valid date\n";
}


$ perl test-date.pl
'2014-11-30 unless *coin ports remain unfixed' is NOT a valid date
'2014-02-30' is NOT a valid date
'2014-02-28' is a valid date
$ 

— 
Dan Langille

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 333 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20141025/c9f62e78/attachment.sig>


More information about the freebsd-hackers mailing list