Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

datetime - How can I validate dates in Perl?

How can I validate date strings in Perl? I'd like to account for leap years and also time zones. Someone may enter dates in the following formats:

11/17/2008
11/17/2008 3pm
11/17/2008 12:01am
11/17/2008 12:01am EST
11/17/2008 12:01am CST
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I use DateTime::Format::DateManip for things like this. Using your dates....

use DateTime::Format::DateManip; 

my @dates = (
    '11/17/2008',
    '11/17/2008 3pm',
    '11/17/2008 12:01am',
    '11/17/2008 12:01am EST',
    '11/17/2008 12:01am CST',
);

for my $date ( @dates ) {
    my $dt = DateTime::Format::DateManip->parse_datetime( $date );
    die "Cannot parse date $date"  unless defined $dt;
    say $dt;
}

# no dies.. produced the following....
# => 2008-11-17T00:00:00
# => 2008-11-17T15:00:00
# => 2008-11-17T00:01:00
# => 2008-11-17T05:01:00
# => 2008-11-17T06:01:00

NB. I'm on GMT here!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...