perl isvaliddate function

B. Estrade estrabd at gmail.com
Mon Oct 27 13:13:10 UTC 2014


use POSIX qw/strftime/;

sub IsValidDate($) {
  my $string = shift;
  my ($year, $mon, $mday) = split /-/, $string;
  my $test = strftime("%Y-%m-%d", 0, 0, 0, $mday, $mon - 1, $year - 1900);
  return ($test eq $string) ? $string : undef;
}

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";
}


On Sat, Oct 25, 2014 at 2:50 PM, Dan Langille <dan at langille.org> wrote:

> On Oct 25, 2014, at 2:21 PM, B. Estrade <estrabd at gmail.com> wrote:
>
> > Looks fine to just get it working. If you wanted to be more efficient, I
> believe there is a way to use the core POSIX::strfmtime in a way that would
> verify that the date you start with is the same date as the one returned
> after the format.  This core function is also very useful for date addition
> and subtraction.
> >
> > I don't have time at this moment to create a proof of concept, but if
> you're interested let me know and I will when I have a minute.
>
> Yes, please, when you have time, please try that proof for me.  I would
> appreciate that.
>
> FYI: I believe all dates within the ports tree must be YYYY-MM-DD so using
> something like that would be useful.
>
> Comparing the starting date to the supplied date is good too, to catch
> edge cases like the first example.
>
>> Dan Langille
>
>


More information about the freebsd-hackers mailing list