Date: Mon, 27 Oct 2014 09:17:20 -0700 From: "Chris H" <bsd-lists@bsdforge.com> To: <freebsd-hackers@freebsd.org> Subject: Re: perl isvaliddate function Message-ID: <11ae82fc8cb55d5f932b24be655b6c6e@ultimatedns.net> In-Reply-To: <CALSf6fQ%2BiaXrz9bCe7mZEz8K%2BUsbG5jdgibueNK_dyhMUY2xdg@mail.gmail.com> References: <4EA9EE9C-5049-4C50-B361-07F58FA19896@langille.org>, <CALSf6fQ%2BiaXrz9bCe7mZEz8K%2BUsbG5jdgibueNK_dyhMUY2xdg@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, 25 Oct 2014 13:21:35 -0500 "B. Estrade" <estrabd@gmail.com> wrote > On Sat, Oct 25, 2014 at 11:56 AM, Dan Langille <dan@langille.org> wrote: > > > 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 > > > > > 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. > > Cheers, > Brett > I also felt inclined to recommend POSIX::strfmtime. I also thought you might save some cycles modifying your conditional from: if (IsValidDate($b)) { print "'$b' is a valid date\n"; } else { print "'$b' is NOT a valid date\n"; } to: unless (IsValidDate($b)) { print "'$b' is NOT a valid date\n"; } you could even introduce my $ok = "Everything looks AOK"; at the beginning, and revise your whole script to close with the $ok, unless any of the 'unless' s fired. Just my 2¢. :) --Chris > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?11ae82fc8cb55d5f932b24be655b6c6e>