Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to find a file's age with respect to current time 1

Status
Not open for further replies.

gftech

Programmer
Aug 22, 2007
5
US
Hi,

I've used the -M operator to find a file's age in days. However, -M returns the age with respect to the program start time. Now that I plan to run my program as a daemon, I need to find a file's age wrt the *current* time. Any body out there knows how I can do that?

Thanks & regards,
 
-M returns the files agein days. It has nothing to do with the start time of your program.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Well :) I guess I was thinking the file time that created the file.. yeah that's it :D

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Well screwed up that joke to.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
the script start time is generally so close to the current time it does not matter. But in a long running program you want to use stat().

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yeah I think he was saying he was going to daemonize it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
May I contribute my little bit of confusion? Both -M and stats return a time interval since the file was last modified - not created. According to the Perl Cookbook p.347
There is no way under standard Unix to find a file's creation time.
If you really want that date you're apparently stuck. I wonder if the same restriction applies to Win systems?

It is also my understanding that -M returns a value measured in days, whilst stats return a value measured in seconds.

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Thank you all for your inputs. I went stat() and solved my problem:

# get cur time
`touch curtime`
my $curtime = (stat("curtime"))[9];
unlink "curtime";
# find file age
if (($curtime - (stat($thefile))[9]) > AGE) {
# process $thefile
}
 
You don't need to touch a file to find the current time.
the function time returns the number of seconds since the epoch.

Code:
if( time - stat($thefile)[9] > AGE ) {
  # process the file
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top