SPUG: File dates

Lou Hevly lou at visca.com
Thu Dec 30 13:04:50 CST 1999


At 08:26 30/12/99 -0800, Daniel V. Ebert wrote:
>
>I have used the stats FILEHANDLE command to extract the "last modified" time
 
>of a file.
>
>($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime,  
>$ctime, $blksize, $blocks) = stat FILEHANDLE;
>
>This returns the time in a numrical format: 946568470
>I'm guessing that this is the number of seconds since the epoch

No need to guess:
perldoc -f stat
...
9 mtime    last modify time since the epoch

> ... is there  
>an easy way to translate that into a calendar date?  Or is there a different
 
>command that would return the modify time in a different format?

Here's one way:
#!/usr/bin/perl -w
use strict;

my $file = '/path/to/file';
open F, $file or die $!;
my $mtime = (stat F)[9];
close F or warn $!;
my $last_mod = localtime($mtime);
print "$last_mod\n";

If you want to play with the formating, you can use the 'strftime' function
from the POSIX module:

#!/usr/bin/perl -w
use strict;
use POSIX;
...(same as above)...
my $last_mod = strftime "%A, %B %d, %Y", localtime($mtime);
print "$last_mod\n";

Lou Hevly
lou at visca.com
http://www.visca.com

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list