For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > date and file comparison









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author date and file comparison
Tim Wolak

2006-11-03, 7:57 am

Morning all,

I need to compare the current date with that of a file, if the file is
older than the current date remove it and replace it with a new one from
new data. Below I have the code set for getting the date but can't come
up with an easy way to compare it against the file date. Can anyone
tell me the best way to compare the current date with that of the
date/time stamp on a file?

Thanks,
Tim

my $date;
my ($sec,$min,$hour,$mday,$mon,$year) = (localtime) [0,1,2,3,4,5];
$year=$year+1900;
$mon=$mon+1;
$date = sprintf("%02d%02d%02d\_%02d\:%02d\:%02d",
$year,$mon,$mday,$hour,$min,$sec);

Dr.Ruud

2006-11-03, 7:57 am

"Tim Wolak" schreef:

> I need to compare the current date with that of a file, if the file is
> older than the current date remove it and replace it with a new one
> from new data. Below I have the code set for getting the date but
> can't come up with an easy way to compare it against the file date.
> Can anyone tell me the best way to compare the current date with that
> of the date/time stamp on a file?


See `perldoc -f time` and `perldoc -f stat` (mtime).
Both in number of seconds since the epoch.

--
Affijn, Ruud

"Gewoon is een tijger."

Arnaldo Guzman

2006-11-03, 6:57 pm

On Fri, 2006-11-03 at 08:08 -0600, Tim Wolak wrote:
> Morning all,
>
> I need to compare the current date with that of a file, if the file is
> older than the current date remove it and replace it with a new one from
> new data. Below I have the code set for getting the date but can't come
> up with an easy way to compare it against the file date. Can anyone
> tell me the best way to compare the current date with that of the
> date/time stamp on a file?
>
> Thanks,
> Tim
>
> my $date;
> my ($sec,$min,$hour,$mday,$mon,$year) = (localtime) [0,1,2,3,4,5];
> $year=$year+1900;
> $mon=$mon+1;
> $date = sprintf("%02d%02d%02d\_%02d\:%02d\:%02d",
> $year,$mon,$mday,$hour,$min,$sec);


Your list slice:

my ($sec,$min,$hour,$mday,$mon,$year) = (localtime) [0,1,2,3,4,5];

Is completely unnecessary; you can achieve the same with:

my ($sec, $min, $hour, $mday, $mon, $year) = localtime();

Use slices for what they're good for. :-)

Now, why not just check how old the file is in days? You can achieve
this in different ways, the file test operators can help you.

Simple example:

if (int(-M $file) > $x) {
# do something
}

Of course this isn't perfect, you can read more about the file test
operators with "perldoc -f -X". You should also read "perldoc -q
timestamp". I'm sure after reading these specifically the FAQ mentioned
(can be found in perlfaq5), you will be on your way to a good start.

Hope this helps! :-)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com