Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Date Checking module

by tradez (Pilgrim)
on Jan 14, 2002 at 09:22 UTC ( #138521=perlquestion: print w/ replies, xml ) Need Help??
tradez has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks, I recently was writing a report that took date ranges and spit out purty Graphs, btw thanks for the responses to my GD::Graph q a few days ago. I wrote this little method to check the date and make sure that they are valid dates, ie you do not put in feb 31 or anything else that would be invalid. I would like to petition you all to look it up and down and see if I have forgotten anything, or if there is any tightening of the code that can be done. Just want to make sure that nothing slips through the perverbial crack. Here she is
sub dateCheck { my $date = @{{@_}}{qw/date/}; my $err = '0'; if ($date) { my $year = substr($date,0,4); my $month = substr($date,4,2); my $day = substr($date,6,2); my %month; $month{'01'} = "January"; $month{'02'} = "February"; $month{'03'} = "March"; $month{'04'} = "April"; $month{'05'} = "May"; $month{'06'} = "June"; $month{'07'} = "July"; $month{'08'} = "August"; $month{'09'} = "September"; $month{'10'} = "October"; $month{'11'} = "November"; $month{'12'} = "December"; my $errDate = "The date you supplied: $month{$month} $day, $year"; if (substr($date,7,1) eq "") { $err="The date you supplied does not contain enough digits!"; } elsif (($year < 1900)||($year > 2100)) { $err="$errDate, did not contain a valid year!"; } elsif (($month < 1)||($month > 12)) { $err="$errDate, did not contain a valid month!<br>"; } elsif (($day < 1)||($day > 31)) { $err="$errDate, did not contain a valid day!"; } elsif ("$month" eq "02") { my $leap = ($year-2000) % 4; if ($leap == 0) { if ($day > 29) { $err="$errDate, does not exist!<br>There are only 29 days in + $month{$month} $year."; } } elsif ($day > 28) { $err="$errDate, does not exist!<br>There are only 28 days in $ +month{$month} $year."; } } elsif ($month < 8) { if (($month % 2 == 0)&&($day > 30)) { $err="$errDate, does not exist!<br>There are only 30 days in $ +month{$month}."; } } elsif ($month > 8) { if (($month % 2 == 1)&&($day > 30)) { $err="$errDate, does not exist!<br>There are only 30 days in $ +month{$month}.."; } } } return $err; }
Thank you and have a nice day :). tradez

Comment on Date Checking module
Download Code
Re: Date Checking module
by jryan (Vicar) on Jan 14, 2002 at 09:40 UTC
    You might want to check out Date::Calc. Isn't CPAN great? :)
Re: Date Checking module
by grep (Monsignor) on Jan 14, 2002 at 09:44 UTC
    You are not handling Leap Years correctly. I'd check CPAN. There are a dozen or modules that handle dates in including Date::Calc.

    UPDATE: I see jryan has pointed this out also. There is also Date-Leapyear

    FYI:The rules for Leap Year are:
  • Every year divisible by 4 is a leap year.
  • But every year divisible by 100 is NOT a leap year
  • Unless the the year is also divisible by 400, then it is still a leap year.

    grep
    grep> cd pub grep> more beer
Re: Date Checking module
by blakem (Monsignor) on Jan 14, 2002 at 09:48 UTC
    A couple good ideas on how to do this properly (leap-year will trip yours up every 400 years or so...) can be found at How do I validate a date?

    -Blake

(ar0n) Re: Date Checking module
by ar0n (Priest) on Jan 14, 2002 at 10:00 UTC
    use Date::Calc qw/check_date/; my $valid = check_date($year, $month, $day);

    [ ar0n -- want job (boston) ]

Re: Date Checking module
by mrbbking (Hermit) on Jan 14, 2002 at 18:10 UTC
    Yeah, as the others have said, you're probably better off going to CPAN for something as complicated as date checking can be.

    On the style front, you might consider using Perl's unpack function, instead of substr.

    # you could use this: my ($month, $day, $year) = unpack('a4 a2 a2', $date); # instead of this: my $year = substr($date,0,4); my $month = substr($date,4,2); my $day = substr($date,6,2);
    ...if you wanted to. There's nothing wrong with using substr here (other than that it takes a lot of typing...) - just pointing out an alternative that you might want to look into.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (18)
As of 2014-07-14 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (260 votes), past polls