Like this
use strict; use Date::Calc qw(check_date check_time); =head1 Date::Calc functions if (check_date($year,$month,$day)) This function returns "true" ("1") if the given three numerical values + "$year", "$month" and "$day" constitute a valid date, and "false" (" +0") otherwise. if (check_time($hour,$min,$sec)) This function returns "true" ("1") if the given three numerical values + "$hour", "$min" and "$sec" constitute a valid time (0 <= $hour < 24, + 0 <= $min < 60 and 0 <= $sec < 60), and "false" ("0") otherwise. =cut # test routine while (<DATA>){ chomp; my $ok = check($_) ? "valid" : "NOT valid"; print "$_ is $ok\n"; } # check valid date time YYYY-MM-DD hh:mm:ss sub check { my $dt = shift; if ($dt =~ m!^(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$!){ return ( check_date($1,$2,$3) && check_time($4,$5,$6) ); } else { return 0; } } __DATA__ 2001-01-01 12:12:12 2001-02-29 12:12:12 2004-02-29 12:12:12 2001-01-01 25:12:12 2001-01-01 12:61:12 2001-01-01 12:12:61 01-01-2001 12:12:12
poj

In reply to Re: datetime verify by poj
in thread datetime verify by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.