costas has asked for the wisdom of the Perl Monks concerning the following question:

I am writing some code which scans through a directory looking at all files within and checking for the modification date of each file. I want to know the age of each file so i am comparing the epoch mod time to the present epoch time. This shouldwork fine but i am getting mod file times which are older than todays epoch time (???)

the following code was executed at Mar 5 12:18:27 2002

To find the epoch time for now i used the code:
#split date into seperate vars my($day, $month, $mday, $time, $year) = split(" ", $datetime); my($timeh, $timem, $times) = split(':', $time); #this function calculates the number of seconds that have passed since + Midnight, January 1, 1970 my $time = timegm($times,$timem,$timeh,$mday,$month,$year);
$time gave the present epoch time of 1010233107

to find the mod epoch time of files i used the code
find (\&checkfile, "/home/costas"); sub checkfile { my @stats = stat $_; $fileage = $time-$stats[9]; my @filetime = localtime($stats[9]); my $year = $filetime[5]+1900; print "$File::Find::name &nbsp; Epoch mod : $stats[9], LT mo +d $filetime[2]:$filetime[1]:$filetime[0] $filetime[4]/$year, Age o +f + file : $fileage <br>"; }
Note that oin the above code i have printed the localtime mod time as well as the epoch mod time.

THe result i get for a particular file is: /home/costas/index.html Epoch mod : 1015330245, LT mod 12:10:45 2/2002, Age of file : -5097138

In order to find the a of age of this file in epoch time i simply subtracted the epoch mod time from the current mod time. The problem as you can see is that the age of time is NEGATIVE which would mean that the file is OLDER than the current time despite the fact that the localtime of this file shows this to be untrue.

Can anybody see why this is so.

Thanks in advance

Costas

PS i posted this about two hours ago but for some reason I cannot ssee the responses that have been posted!!! Also my questions do not appear on the monastery gates.

Replies are listed 'Best First'.
Re: Epoch mod time for files
by steves (Curate) on Mar 05, 2002 at 15:38 UTC

    You're comparing GM time to your local time for one thing. Why not just do this?

    $time = time();

    It is possible to set a file's modification time ahead of the current time on most systems (see touch on UNIX for example). But in practice things will probably appear as expected if both times you use are relative to the same time zone.

Re: Epoch mod time for files
by Rhose (Priest) on Mar 05, 2002 at 15:38 UTC
    Here is some code I have used to find out how many seconds old a specified file(s) is.

    #!/usr/bin/perl -w use strict; #-- Use modules use File::stat; use Time::Local; use Time::localtime; #-- Define local constants #-- Define local variables my $lNow; my $lStat; #-- Initialize variables $lNow=localtime(); $lNow=timelocal($lNow->sec,$lNow->min,$lNow->hour,$lNow->mday,$lNow->m +on,$lNow->year); #-- Process all files foreach (@ARGV) { #-- Get the file's status $lStat=stat($_) || die 'Could not stat file ['.$_.']...'; #-- Print the time print $_,"\t",($lNow-$lStat->mtime()),' seconds old',"\n"; } #-- End of Script

    I hope this helps.

Re: Epoch mod time for files
by mpeppler (Vicar) on Mar 05, 2002 at 15:39 UTC
    You're using a very convoluted way of getting the $time value.

    Use this instead:

    $time = time();

    Michael

Re: Epoch mod time for files
by webadept (Pilgrim) on Mar 05, 2002 at 17:13 UTC
    I threw Rhose's cool little example above into a while loop to read all files in a given directory, and print out age. Hope this helps.
    #!/usr/bin/perl -w use strict; #-- Use modules use File::stat; use Time::Local; use Time::localtime; #-- Define local constants #-- Define local variables my $lNow; my $lStat; my $file; my $dirname; if($ARGV[0]){$dirname = $ARGV[0]}else{$dirname = "./";} opendir(DIR,$dirname) || die "Horrible Death"; while(defined($file=readdir(DIR))) { #-- Initialize variables $lNow=localtime(); $lNow=timelocal($lNow->sec,$lNow->min,$lNow->hour,$lNow->mday,$lNow->m +on,$lNow->year); #-- Process all files #-- Get the file's status $lStat=stat($file) || die 'Could not stat file ['.$file.']...'; #-- Print the time print $file,"\t",($lNow-$lStat->mtime()),' seconds old',"\n"; } closedir(DIR); #-- End of Script


    Glenn Hefley
    webadept.net
Re: Epoch mod time for files
by Anonymous Monk on Mar 05, 2002 at 16:56 UTC
    Where is $datetime coming from?
Re: Epoch mod time for files
by costas (Scribe) on Mar 06, 2002 at 09:49 UTC
    Datetime comes from the following code:
    my $datetime = gmtime(time);