How to validate date and time?

Y

yashgt

I have a program that deals with files with names such as
SA_2005-01-03_01-00-00_sqf1974_1.0.0_Iub_Iu_CT.csv
The format is SA_<date>_<time>_....
Befor processing a file, I would like the program to validate the
filename by checking whether the date and time parts are correct.
I tried using mktime, hoping for perl to return an error value if the
date and time are wrong. But for some reason even if I give plain text
in the time part, mktime does not return an error. I use
ActivePerl 5.8.2 on Windows 2000. This does not have the Date module.
Can someone suggest a way to do the validation?

Thanks
Yash
 
G

Gunnar Hjalmarsson

I have a program that deals with files with names such as
SA_2005-01-03_01-00-00_sqf1974_1.0.0_Iub_Iu_CT.csv
The format is SA_<date>_<time>_....
Befor processing a file, I would like the program to validate the
filename by checking whether the date and time parts are correct.
I tried using mktime, hoping for perl to return an error value if the
date and time are wrong. But for some reason even if I give plain text
in the time part, mktime does not return an error. I use
ActivePerl 5.8.2 on Windows 2000. This does not have the Date module.

Which Date module are you talking about? There are plenty of them, but
the standard module Time::Local should be sufficient in this case:

use Time::Local;
my @t = $filename =~ /^SA_(\d+)-(\d+)-(\d+)_(\d+)-(\d+)-(\d+)/;
eval { timelocal @t[5,4,3,2], $t[1]-1, $t[0]-1900 };
print ($@ or "Date/time is valid\n");

See "perldoc Time::Local"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top