ctp has asked for the wisdom of the Perl Monks concerning the following question:
#!usr/bin/perl #Script to parse dates with the following formats: #Apr 8 1984, Apr 08 84, 4/8/84, 04/08/84, 08 Apr 1984 use warnings; use strict; #declare my vars and make sure they're empty my ( $MM, $DD, $YY, $YYYY ); #take in date at command line and make text lowercase print "\n\n\n\n"; print "Date Converter, v1.0\n"; print "this program assumes that a year from 10 to 99 is in the 1900's +\n"; print "and that a year from 00 to 09 is in the 2000's\n"; print "please enter a date\n"; chomp (my $date = <>); $date = lc ($date); if ($date =~ /[a-zA-Z]{3}\s\d{1,2}\s\d{4}/) #parse Apr 8 1984 { my @dateparts = split (/\s/ , $date); $MM = $dateparts [0]; $DD = $dateparts [1]; $YYYY = $dateparts [2]; output ($MM, $DD, $YYYY); } elsif ($date =~ /[a-zA-Z]{3}\s\d{2}\s\d{2}/) #parse Apr 08 84 { my @dateparts = split (/\s/ , $date); $MM = $dateparts [0]; $DD = $dateparts [1]; $YY = $dateparts [2]; if (10 <= $YY && $YY <= 99) {$YYYY = $YY + 1900} elsif (0 <= $YY && $YY <= 9) {$YYYY = $YY + 2000} output ($MM, $DD, $YYYY); } elsif ($date =~ /\d\/\d\/\d{2}/) #parse 4/8/84 { my @dateparts = split (/\// , $date); $MM = $dateparts [0]; $DD = $dateparts [1]; $YY = $dateparts [2]; if (10 <= $YY && $YY <= 99) {$YYYY = $YY + 1900} elsif (0 <= $YY && $YY <= 9) {$YYYY = $YY + 2000} output ($MM, $DD, $YYYY); } elsif ($date =~ /\d{2}\/\d{2}\/\d{2}/) #parse 04/08/84 { my @dateparts = split (/\// , $date); $MM = $dateparts [0]; $DD = $dateparts [1]; $YY = $dateparts [2]; if (10 <= $YY && $YY <= 99) {$YYYY = $YY + 1900} elsif (0 <= $YY && $YY <= 9) {$YYYY = $YY + 2000} output ($MM, $DD, $YYYY); } elsif ($date =~ /\d{1,2}\s[a-zA-Z]{3}\s\d{4}/) #parse 08 Apr 1984 { my @dateparts = split (/\s/ , $date); $DD = $dateparts [0]; $MM = $dateparts [1]; $YYYY = $dateparts [2]; output ($MM, $DD, $YYYY); } else #contingency plan { print "your date is not of a recognizable format...good day.\n"; } #take in $MM $DD $YYYY, parse $MM with %months #and print "fullmonth day, year" to STDOUT sub output { my @outputdates = @_; my %months = ( jan => "January", feb => "February", mar => "March", apr => "April", may => "May", jun => "June", jul => "July", aug => "August", sep => "September", oct => "October", nov => "November", dec => "December", 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December", 01 => "January", 02 => "February", 03 => "March", 04 => "April", 05 => "May", 06 => "June", 07 => "July", 08 => "August", 09 => "September" ); #unfinished print "$outputdates[1], $outputdates[2]\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: more date conversion happiness, part 3
by CountZero (Bishop) on Jan 11, 2004 at 22:11 UTC | |
Re: more date conversion happiness, part 3
by pg (Canon) on Jan 11, 2004 at 22:05 UTC | |
by CountZero (Bishop) on Jan 11, 2004 at 22:16 UTC | |
Re: more date conversion happiness, part 3
by neuroball (Pilgrim) on Jan 12, 2004 at 01:23 UTC | |
Re: more date conversion happiness, part 3
by ysth (Canon) on Jan 12, 2004 at 03:11 UTC | |
by ctp (Beadle) on Jan 12, 2004 at 03:24 UTC | |
by ysth (Canon) on Jan 12, 2004 at 03:45 UTC | |
by ctp (Beadle) on Jan 12, 2004 at 03:49 UTC |