[Perl] time/date differences

Eli Bendersky spur4444 at yahoo.com
Thu Oct 31 04:27:39 PST 2002


--- Shlomo Yona <shlomo at cs.haifa.ac.il> wrote:
> Hello.
> 
> I am writing code for an "automatic homework grader"
> for an introduction course
> in c. Following is a description of something I had
> to do, and I wonder if you
> guys have ideas for better code than I do.
> 
> Thanks:
> 
> 
> I'm writing a script to parse lines like this:
> 
> -rw-r--r--    1 shlomo   shlomo        760 Oct 29
> 23:36 1/022308399/1/hw1.c
> -rw-r--r--    1 shlomo   shlomo        198 Oct 30
> 17:29 1/066589623/1/targil1.c
> -rw-r--r--    1 shlomo   shlomo        884 Oct 28
> 13:57 1/306370966/1/home.c
> -rw-r--r--    1 shlomo   shlomo      24583 Oct 28
> 22:02 1/035846088/1/hw1.c
> -rw-r--r--    1 shlomo   shlomo        218 Oct 28
> 21:55 1/035846088/1/noam.c
> -rw-r--r--    1 shlomo   shlomo        385 Oct 30
> 18:09 1/035848688/1/Homework1.c
> -rw-r--r--    1 shlomo   shlomo        266 Oct 30
> 20:40 1/041955584/1/hw1.c
> -rw-r--r--    1 shlomo   shlomo        584 Oct 28
> 14:47 1/036000479/1/dudi.c
> 
> I want to extract only 3 field:
> 
> 	1. date
> 	2. time
> 	3. filename (including the directory)
> 
> The input is produced by a unix oneliner:
> 
> 	find SOMEDIR -type f -iname "*.c" -exec ls -ltr {}
> \;
> 
> SOMEDIR in this case was 1/
> 
> two questions:
> 
> 1. Can you suggest a oneliner that outputs just what
> I need?
> 2. Can you suggest a nice clean Perl code that can
> take two strings of the form
> 	Oct 29 23:36
> and can perform the following operations:
> 
> 		 recent(@date_time_strings)
> 	should return one string from the list which is the
> more recent date_time
> 	or undef if the list is empty. You can assume the
> strings are of the form
> 	of the date_time you get from 'ls -l' like the
> examples above.
> 
> 	
>
recent(@date_time_strings,$deadline_date_time_string)
> 	should return one string most recent to the
> deadline but not after the deadline.
> 	undef should be returned if no such string exists.
> 
> 
> Thanks.

Hello Shlomo,

For extracting the fields, use:

perl -ne '@k = split; print "$k[5] $k[6] $k[7] $k[8]
$k[9]\n"'

That is, pipe your find into this one-liner.

To parse date strings, you may want to take a look at
the Date::Manip module from CPAN. However, if I
understood your intentions correctly, perhaps it will
suffice to save all file names in an array and use the
stat() function to find out their change dates. 
stat() (perldoc -f stat) can give you time in the
epoch format, which is easily comparable.

Kind regards

Eli


__________________________________________________
Do you Yahoo!?
HotJobs - Search new jobs daily now
http://hotjobs.yahoo.com/



More information about the Perl mailing list