Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

date::manip question

by e5z8652 (Novice)
on Feb 02, 2013 at 20:33 UTC ( #1016753=perlquestion: print w/ replies, xml ) Need Help??
e5z8652 has asked for the wisdom of the Perl Monks concerning the following question:

I have a small CGI script that accepts dates from users and lets them do a database lookup. This script has been working for years with minor changes. The last time I changed the script was to upgrade the server to Debian Squeeze, where there were some minor syntax changes.

However in the last month or so, the date search function stopped working. Many people had the dates default to the current day, regardless of what they entered into the CGI. Testing revealed that the date function worked fine, as long as proper syntax was used. But I use date::manip to make it as user friendly as possible!

I've cut the troublesome part of the script out, and it is included below together with sample output.

#!/usr/bin/perl -T use strict; use Date::Manip; my $raw_date1 == $ARGV[0]; + # Default to today if date is blank: + if ( $raw_date1 == "" ) { + print "In blank date check. But is the date truly blank? It +is in fact \"$raw_date1\"\n"; + $raw_date1 = UnixDate("today","%Y-%m-%d"); }; $raw_date1 = ParseDate($raw_date1); + # fix the dates to MySQL format: + my $date1 = UnixDate($raw_date1,"%Y-%m-%d"); + print "$raw_date1 was changed to $date1\n";

sorry for the cut & paste munging.

And here is the output:

I expect this:

jfzuelow@city-liza-lnx:~$ ./ + In blank date check. But is the date truly blank? It is in fact "" + 2013020200:00:00 was changed to 2013-02-02

And I expect this:

jfzuelow@city-liza-lnx:~$ ./ "15 jan" 2013011500:00:00 was changed to 2013-01-15

However this is very confusing to me, and my users:

jfzuelow@city-liza-lnx:~$ ./ "jan 15" In blank date check. But is the date truly blank? It is in fact "jan + 15" 2013020200:00:00 was changed to 2013-02-02


Comment on date::manip question
Select or Download Code
Re: date::manip question
by Kenosis (Priest) on Feb 02, 2013 at 20:39 UTC

    You have:

    my $raw_date1 == $ARGV[0]; # Default to today if date is blank: if ( $raw_date1 == "" ) { ...

    Didn't you mean:

    my $raw_date1 = $ARGV[0]; # Default to today if date is blank: if ( $raw_date1 eq "" ) { ...
      Yes, I did. I had warnings turned off, and when I added them in my cutout Perl told me right away that == was the wrong operator. And of course now it works. And that infers the answer. I'm guessing that "15 Jan" worked because it started with a number, where "Jan 15" is a string through and through. But this particular typo has been in place for YEARS without being caught. Still wondering what changed in Debian land where it stopped working in the last month. Oddness.

        On a side note, perhaps the following may be helpful:

        my $date1 = UnixDate( ParseDate($raw_date1) || 'today', "%Y-%m-%d" );

        This, instead of checking for an empty string, will either send a successful ParseDate or 'today' to UnixDate. I'd suggest a more comprehensive sanity check on the date data, but this will catch more than just an empty string.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1016753]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-07-29 10:57 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (214 votes), past polls