For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > August 2004 > Need Script Help - file/time diff in seconds









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 Need Script Help - file/time diff in seconds
Willie Nelson

2004-08-19, 9:11 pm

I have code that returns the most recent modified file in a directory
(Win2K/Apache/PERL). I want to compare the modified time on that filename
that is returned ($newestFile) to the local time on the webserver and spawn
a failure response if the difference in time is more than 600 seconds (10
minutes). I've looked everywhere for examples are am totally by
the whole epoch time, time conversion etc. Please help. Thanks - joe

use POSIX;
use File::Find;
use File::Stat;
use Date::Manip;
$folder = 'D:\wxc\data\radar\national\nat1km';
@files = grep {-f} glob "$folder/*";
$fNT{$_} = sprintf "%010d", (stat $_)[9] for @files;#create hash with file
and file last modified date
$newestFile = ( sort {$fNT{$b} <=> $fNT{$a}} keys %fNT )[0];#the first
element of the sorted hash
print $newestFile;
$file = $newestFile;
$mtime = (stat $file)[9];
print strftime("%A %B %d, %Y %H:%M %p", localtime($mtime));
print strftime("%A %B %d, %Y %H:%M %p", localtime(time));



Gunnar Hjalmarsson

2004-08-19, 9:11 pm

Willie Nelson wrote:
> I have code that returns the most recent modified file in a
> directory (Win2K/Apache/PERL). I want to compare the modified time
> on that filename that is returned ($newestFile) to the local time
> on the webserver and spawn a failure response if the difference in
> time is more than 600 seconds (10 minutes). I've looked everywhere
> for examples are am totally by the whole epoch time, time
> conversion etc.


Everywhere? Did your research include the Perl documentation, for
instance the description of the stat() and time() functions?

perldoc -f stat
perldoc -f time

It's obvious that you didn't wrote the script yourself, but you
probably just picked it up somewhere and now you are asking this group
to modify it for you. It's not advisable to use random pieces of code
without understanding how it works, so I'd suggest that you start there.

As regards your question, you should be able to write the condition
using the $mtime value (as given from the script) and the return value
from time().

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Willie Nelson

2004-08-19, 9:11 pm

Thanks for the response. FYI, this is one of those learn as you go
projects. Now that I'm seeing the flexibility of PERL, I plan on using it
for more things.

I have checked the PERL docs, but this whole epoch time thing is killing me.
I've created the following at the end of the script:

$seconds_diff = int($time - $mtime);

but all it does is return the same result: -1092926507. I know that I'm
close here, but just can't get over the hump. Any suggestions?

Thanks,
joe


Willie Nelson

2004-08-19, 9:11 pm

Ok. This is the string that I need. The value changed when my newest file
was updated. Now the question is turning that large number into a value for
seconds or minutes. Any suggestions?

Thanks,
joe

"Willie Nelson" <willie@buymyrecords.com> wrote in message
news:u%2Vc.95$Ae.73@newsread1.dllstx09.us.to.verio.net...
> Thanks for the response. FYI, this is one of those learn as you go
> projects. Now that I'm seeing the flexibility of PERL, I plan on using it
> for more things.
>
> I have checked the PERL docs, but this whole epoch time thing is killing

me.
> I've created the following at the end of the script:
>
> $seconds_diff = int($time - $mtime);
>
> but all it does is return the same result: -1092926507. I know that I'm
> close here, but just can't get over the hump. Any suggestions?
>
> Thanks,
> joe
>
>



Gunnar Hjalmarsson

2004-08-19, 9:11 pm

Willie Nelson wrote:
> Thanks for the response. FYI, this is one of those learn as you go
> projects. Now that I'm seeing the flexibility of PERL, I plan on
> using it for more things.
>
> I have checked the PERL docs, but this whole epoch time thing is
> killing me. I've created the following at the end of the script:
>
> $seconds_diff = int($time - $mtime);

----------------------^

You didn't populate any $time variable, did you? You need to use the
time() *function*. Try removing that '$' character, and you don't need
the int() function either:

$seconds_diff = time - $mtime;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Willie Nelson

2004-08-19, 9:11 pm

Thanks! That did the trick. I knew it was a matter of syntax, just wasn't
sure where..

-joe

"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:2ojv97Fbkc7kU1@uni-berlin.de...
> Willie Nelson wrote:
> ----------------------^
>
> You didn't populate any $time variable, did you? You need to use the
> time() *function*. Try removing that '$' character, and you don't need
> the int() function either:
>
> $seconds_diff = time - $mtime;
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl



Gunnar Hjalmarsson

2004-08-19, 9:11 pm

Willie Nelson wrote:
> I knew it was a matter of syntax, just wasn't sure where..


Yes, it was, and if you are going to learn Perl, please make it a
habit from the start to include

use strict;
use warnings;

in the beginning of every program. If that had been the case with the
script you now have modified, Perl would have helped you detect the
problem.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Joe Smith

2004-08-19, 9:11 pm

Willie Nelson wrote:

> I have code that returns the most recent modified file in a directory
> (Win2K/Apache/PERL). I want to compare the modified time on that filename
> that is returned ($newestFile) to the local time on the webserver


$_ = -M "myfile";
printf "Age is %.2f days; %.2f hours; %.2f minutes\n",$_,$_*60,$_*60*60;

Age is 0.47 days; 27.96 hours; 1677.67 minutes

-Joe
Willie Nelson

2004-08-19, 9:11 pm

Thanks Joe. That will help too..
-joe
"Joe Smith" <Joe.Smith@inwap.com> wrote in message
news:e86Vc.39159$mD.3798@attbi_s02...
> Willie Nelson wrote:
>
filename[color=darkred]
>
> $_ = -M "myfile";
> printf "Age is %.2f days; %.2f hours; %.2f minutes\n",$_,$_*60,$_*60*60;
>
> Age is 0.47 days; 27.96 hours; 1677.67 minutes
>
> -Joe



Sponsored Links







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

Copyright 2008 codecomments.com