Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Formatting STDIN for date format

by Ashley Jordan (Novice)
on Sep 18, 2013 at 08:47 UTC ( #1054616=perlquestion: print w/replies, xml ) Need Help??
Ashley Jordan has asked for the wisdom of the Perl Monks concerning the following question:

Hello Guys,

Relativity new to Perl so i will go ahead and try to explain what i am trying to do etc!

I am trying to configure the input to only accept DD-MM-YYYY Format, but i also need to have the day, month and year as variables allowing me to use those variables in a mysql command.

At the moment i have this, it most likely is completely wrong, but if you could give guide me that would be great;

(<STDIN> =~$day/(\d{2})-$month(\d{2})-$year(\d{4})/) { $date = <STDIN>; } else { print "Invalid date, not in DD-MM-YYYY Format\n"; }

My main worry of the above is that the variables are not in the correct place ?

Heres an example of the mysql command ( just to let you know where the variables will be used )

$sql = "UPDATE table SET day='$day' month='$month' year='$year' WHERE field='$variable' AND field='$variable'\G"

I Have changed the names of the fields/table/variables in the mysql to just a generic example. i am just looking for assistance in regards to the 3 fields below;

The three fields in the mysql table i would like to update are as follows;




Any help would be great,

Thanks in advance!


Replies are listed 'Best First'.
Re: Formatting STDIN for date format
by McA (Priest) on Sep 18, 2013 at 09:28 UTC


    please take this as a starting point:

    #!/usr/bin/env perl use strict; use warnings; my $line = <STDIN>; my $day; my $month; my $year; chomp($line); if($line =~ /^([0-3][0-9])-([01][0-9])-([0-9]{4})$/) { $day = $1; $month = $2; $year = $3; } else { die "ERROR: Wrong date format, please provide DD-MM-YYYY: $line"; } print "Date is: $year-$month-$day\n";

    You have to add code to check whether the date is a valid date.


Re: Formatting STDIN for date format
by roboticus (Chancellor) on Sep 18, 2013 at 11:07 UTC


    You can't format STDIN, you can only look at what came in and see if it matches what you want. You can do it something like this:

    # Get a line from the user (or file if redirected) my $line = <STDIN>; while ($line !~ /regex to check input/) { # input didn't match regex, so tell the user and get another line +of data print "Sorry, I don't understand that, please enter the date like: + DD-MM-YYYY\n"; $line = <STDIN>; } ... code that uses properly formatted date ...


    When your only tool is a hammer, all problems look like your thumb.

Re: Formatting STDIN for date format
by boftx (Deacon) on Sep 19, 2013 at 06:09 UTC

    Here is what I use to handle the most common date formats (assume the usual use strict; and use warnings; as well as use Carp; and use DateTime; are present):

    # Utility function to convert a date string to a DT object sub _str2dt { # string that is supposed to be yyyy-mm-dd or mm/dd/yyyy # single digit months/days are allowed with this. my $date = shift; # convert date param to DT object my ($y,$m,$d); if ( $date =~ m/(^\d{4})\-(\d{1,2})\-(\d{1,2})($|\D+)/ ) { $y = $1, $m = $2, $d = $3; } elsif ( $date =~ m/^(\d{1,2})\/(\d{1,2})\/(\d{4})($|\D+)/ ) { $y = $3, $m = $1, $d = $2; } else { croak "Unable to parse date string: $date"; } eval { $date = DateTime->new( year => $y, month => $m, day => $d ) +; } or croak "Invalid date: $date"; return $date; }
Re: Formatting STDIN for date format
by kcott (Chancellor) on Sep 19, 2013 at 07:19 UTC

    G'day Ashley,

    Welcome to the monastery.

    You were sort of on the right track with capturing the elements of the date with "(\d{n})" constructs, but you appear to have thought that including variables within the regex would somehow assign to them. For starters, I'd recommend you look at "perlintro -- a brief introduction and overview of Perl" and "perlretut - Perl regular expressions tutorial".

    If all you want to do is check the format and, if OK, build $sql, you can code this:

    #!/usr/bin/env perl -l use strict; use warnings; while (<STDIN>) { chomp; if (/^(\d{2})-(\d{2})-(\d{4})$/) { print "... day='$1' month='$2' year='$3' ..."; } else { print 'Not DD-MM-YYYY format'; } }

    Here's a sample run:

    $ 19-9-2013 Not DD-MM-YYYY format 19/9/2013 Not DD-MM-YYYY format 30-02-2013 ... day='30' month='02' year='2013' ... 99-99-9999 ... day='99' month='99' year='9999' ... 19-09-2013 ... day='19' month='09' year='2013' ... 19-09-20133 Not DD-MM-YYYY format

    That's correctly weeding out "19-9-2013" and "19/9/2013"; unfortunately, it's quite happy with possible typos like "30-02-2013" and completely bogus dates like "99-99-9999". So, you'll probably need to assign the captured values to variables and then validate them. There's lots of ways of doing this, and it may well depend on what sort of feedback you want to present to your users; here's a fairly straightforward method, using the builtin module Time::Piece, that differentiates between bad format, possible typo or leap year error, and completely nonsense dates:

    #!/usr/bin/env perl -l use strict; use warnings; use Time::Piece; while (<STDIN>) { chomp; if (/^(\d{2})-(\d{2})-(\d{4})$/) { my ($day, $month, $year) = ($1, $2, $3); my $tp = eval { Time::Piece->strptime("$day$month$year", '%d%m +%Y') }; if (defined $tp) { if ($tp->ymd eq "$year-$month-$day") { print "... day='$day' month='$month' year='$year' ..." +; } else { print "INVALID DATE: $day-$month-$year"; } } else { print "BOGUS DATE: $day-$month-$year"; } } else { print 'Not DD-MM-YYYY format'; } }

    Sample run:

    $ 19-9-2013 Not DD-MM-YYYY format 19/09/2013 Not DD-MM-YYYY format 19-09-2013 ... day='19' month='09' year='2013' ... 99-99-9999 BOGUS DATE: 99-99-9999 32-02-2013 BOGUS DATE: 32-02-2013 31-02-2013 INVALID DATE: 31-02-2013 29-02-2013 INVALID DATE: 29-02-2013 29-02-2012 ... day='29' month='02' year='2012' ... 19-09-20133 Not DD-MM-YYYY format

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054616]
Front-paged by Arunbear
and a kettle whistles...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-06-21 12:31 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.