[grand-rapids-pm-list] Perl Date Validator Test Chokes!

grand-rapids-pm-list at pm.org grand-rapids-pm-list at pm.org
Mon May 4 06:21:59 PDT 2009


Exactly. The main problem is the "day of the month" ($mday) element of
localtime begins with "1 not "0" and does not need to be incremented.

#  0    1    2     3     4    5     6     7     8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime
<http://perldoc.perl.org/functions/localtime.html>(time
<http://perldoc.perl.org/functions/time.html>);

It does seem pretty arbitrary, since even the "day of the year" ($yday)
element begins with "0" and goes through "364". Not sure the history behind
it - there may be a good story there.

The second problem with that test was that it was just plain confusing. Not
sure if the intent was to test yesterday's date or today's. But, you cannot
get today by incrementing yesterday (at least not in that manner). So, I
fixed the test by doing as you suggested, plus I began testing today's date
instead.

Ed Eddington



On Fri, May 1, 2009 at 8:24 PM, <grand-rapids-pm-list at pm.org> wrote:

> Looks like it never really worked right. It may have passed the test but
> the
> date would have been today's date except on the first day of the month when
> it would have been one day more than the possible number of days in the
> last
> month 12/32/2008 if run on 1/1/2009.  The localtime function is a little
> confusing because some things are zero based (like the month) and some
> things not (the day of the month) does anyone know of any other functions
> that do things like this?  Does anyone know or have a guess why they did
> this?  Looks like things that are always numbers do not need conversion and
> things that would be converted to names like days and months start at zero.
>
> So did it work yesterday or just not fail?
>
> To fix it I would take out '+ 1' after '$time[3]' in the 12th line.
>
>
>
> Is Perl Mongers meeting in May?
>
>
>
> -----Original Message-----
> From: grand-rapids-pm-list-bounces+dave=hopasaurus.com at pm.org
> [mailto:grand-rapids-pm-list-bounces+dave<grand-rapids-pm-list-bounces%2Bdave>
> =hopasaurus.com at pm.org] On Behalf
> Of grand-rapids-pm-list at pm.org
> Sent: Friday, May 01, 2009 2:57 PM
> To: grand-rapids-pm-list at pm.org
> Subject: [grand-rapids-pm-list] Perl Date Validator Test Chokes!
>
> Today I noticed a test failing in a piece of old code that tests a date
> validator. After some quick debugging, I found the problem. Can you find
> what is wrong with the second test below? (Btw, you can't actually run the
> code below, as I'm only including the tests from the pod.) Hint: It worked
> yesterday! How would you fix it? I'll share my solution a bit later.
>
>    use Test::Exception;
>
>    # TEST 1 valid date, in the future
>    $q->param("${field}", '01/01/3005');
>    lives_ok {
>        %ret = $date_v->validate;
>    } 'Valid date validated';
>    isnt($ret{'valid'}, 1, '> 6 months ago date validated correctly');
>    like($ret{'error'}, qr/\w+/, 'Correct error message returned.');
>
>    # TEST 2 valid date
>    my @time = localtime(time - 86400); # yesterday
>    my $date = sprintf('%02d/%02d/%04d', $time[4] + 1, $time[3] + 1,
> $time[5] + 1900);
>    diag("Date: $date");
>    $q->param("${field}", $date);
>    lives_ok {
>        %ret = $date_v->validate;
>    } 'Valid date validated';
>    is($ret{'valid'}, 1, 'Valid date validated correctly');
>
>
>
> Ed Eddington
> Gr.pm
>
> ** ** **  PRIVILEGED AND CONFIDENTIAL  ** ** **
> This email transmission contains privileged and confidential information
> intended only for the use of the individual(s) or entity named above.  Any
> unauthorized review, use, disclosure or distribution is prohibited and may
> be a violation of law.  If you are not the intended recipient or a person
> responsible for delivering this message to an intended recipient, please
> delete the email and immediately notify the sender via the email return
> address or mailto:postmaster at priorityhealth.com.  Thank you.
>
>
> _______________________________________________
> grand-rapids-pm-list mailing list
> grand-rapids-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list
>
> _______________________________________________
> grand-rapids-pm-list mailing list
> grand-rapids-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/grand-rapids-pm-list/attachments/20090504/959b2a43/attachment.html>


More information about the grand-rapids-pm-list mailing list