如何在Perl中验证日期字符串?我想说明闰年和时区.有人可能会按以下格式输入日期:
11/17/2008 11/17/2008 3pm 11/17/2008 12:01am 11/17/2008 12:01am EST 11/17/2008 12:01am CST
draegtun.. 10
我使用DateTime :: Format :: DateManip来做这样的事情.使用你的约会....
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.我在GMT这里!
我使用DateTime :: Format :: DateManip来做这样的事情.使用你的约会....
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.我在GMT这里!
CPAN有许多用于处理日期的包,例如DateTime和Date :: Manip.要解析您的日期,TimeDate中的Date :: Parse模块可能会起作用(是的,有DateTime和TimeDate).
通常,只要您需要在Perl中执行某些操作,请检查CPAN.可能有一个模块.看看有什么可用,看看适合你的情况.
祝好运, :)