From owner-freebsd-hackers@FreeBSD.ORG Mon Oct 27 16:16:06 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 0361C5AC for ; Mon, 27 Oct 2014 16:16:06 +0000 (UTC) Received: from udns.ultimatedns.net (unknown [IPv6:2602:d1:b4d6:e600:4261:86ff:fef6:aa2a]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id CA38FD1E for ; Mon, 27 Oct 2014 16:16:05 +0000 (UTC) Received: from ultimatedns.net (localhost [127.0.0.1]) by udns.ultimatedns.net (8.14.9/8.14.9) with ESMTP id s9RGHKVm020191 for ; Mon, 27 Oct 2014 09:17:20 -0700 (PDT) (envelope-from bsd-lists@bsdforge.com) To: In-Reply-To: References: <4EA9EE9C-5049-4C50-B361-07F58FA19896@langille.org>, From: "Chris H" Subject: Re: perl isvaliddate function Date: Mon, 27 Oct 2014 09:17:20 -0700 Content-Type: text/plain; charset=UTF-8; format=fixed MIME-Version: 1.0 Message-id: <11ae82fc8cb55d5f932b24be655b6c6e@ultimatedns.net> Content-Transfer-Encoding: 8bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Oct 2014 16:16:06 -0000 On Sat, 25 Oct 2014 13:21:35 -0500 "B. Estrade" wrote > On Sat, Oct 25, 2014 at 11:56 AM, Dan Langille 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"