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

I'm downloading some files from an FTP site and obviously the modification date and time of the local file will be when the download happened.

What I'm looking to do it modify the modification time so that the minutes are set to 00. This is because the mode time is used for tracking "freshness" of the files. I've looked at alternatives - this is the only way to achieve what I want.

Say a file is downloaded at 13:34, I want to change the modification time to 13:00.

I've seen a couple of shell scripts which "touch" files to set dates and times. Is this possible in Perl? I'm using Linux as the platform so I'm guessing its possible somehow.

- Speedy

  • Comment on Modifying file access times on Unix through Perl.

Replies are listed 'Best First'.
Re: Modifying file access times on Unix through Perl.
by btrott (Parson) on Mar 21, 2000 at 22:09 UTC
    There may be other ways (for example, using touch directly), but this is the first way that I thought of.
    use Time::Local; my $file = "foo"; my($atime, $mtime) = (stat $file)[8, 9]; my($sec, $min, $hour, $mday, $mon, $year) = (localtime $mtime)[0..5]; my $on_hour = timelocal(0, 0, $hour, $mday, $mon, $year); utime $atime, $on_hour, $file;
    If you also want the last access time set to 13:00 (in your example), change the last line to
    utime $on_hour, $on_hour, $file;
Re: Modifying file access times on Unix through Perl.
by jerji (Novice) on Mar 22, 2000 at 01:28 UTC
    In the TMTOWTDI spirit, here's a shorter variation without Time::Local:
    my $file = "foo"; my ($atime, $mtime) = (stat $file)[8,9]; $mtime -= $mtime % 3600; utime $atime, $mtime, $file;
    Again, btrott's note applies re also updating the last access time.