Learning Perl

Learning PerlSearch this book
Previous: 13.6 Modifying OwnershipChapter 13
File and Directory Manipulation
Next: 13.8 Exercises
 

13.7 Modifying Timestamps

Associated with each file is a set of three timestamps. These timestamps were discussed briefly when we talked about getting information about a file: the last access time, the last modification time, and the last inode-change time. The first two timestamps can be set to arbitrary values by the utime function (which corresponds directly to the same-named UNIX system call). Setting these two values automatically sets the third value to the current time, so there's no point in having a way to set the third value.

The values are measured in internal time, namely an integer number of seconds past midnight GMT, January 1, 1970 - a figure that had reached 800-million-something when this book was being written. (Internally, it's represented as a 32-bit unsigned number, and if we haven't all upgraded to 64-bit machines (or beyond), will overflow sometime well into the next century. We have much more to worry about in the year 2000.[3])

[3] Perl's localtime and gmtime functions work just like C's: they return the year with 1,900 subtracted. In 2003, localtime will give the year as 103.

The utime function works like chmod and unlink. It takes a list of filenames and returns the number of files affected. Here's how to make the fred and barney files look as though they were modified sometime in the recent past:

$atime = $mtime = 700_000_000; # a while ago
utime($atime,$mtime,"fred","barney");

There's no "reasonableness" value for the timestamps: you can make a file look arbitrarily old or as though it were modified at some time in the distant future (useful if you are writing science fiction stories). For example, using the time function (which returns the current time as a UNIX timestamp), here's how to make the file max_headroom look like it was updated 20 minutes into the future:

$when = time( ) + 20*60; # 20 minutes from now
utime($when,$when,"max_headroom");


Previous: 13.6 Modifying OwnershipLearning PerlNext: 13.8 Exercises
13.6 Modifying OwnershipBook Index13.8 Exercises