in reply to Verifying a date
#!/usr/bin/perl use warnings; use strict; use Time::Piece; sub validate_date { my ($date) = @_; return eval { $date eq Time::Piece->strptime($date, '%m.%d.%y')->s +trftime('%m.%d.%y') } } use Test::More tests => 5; ok validate_date('02.28.19'); ok ! validate_date('02.32.19'); ok ! validate_date('02.29.19'); ok validate_date('02.29.16'); ok ! validate_date('02.30.16');
The eq is needed because 02.29.19 is parsed without an error by Time::Piece, returning 03.01.19 when formatted back to a string.
Update: It passes Corion's test cases correctly.
|
|---|