Displaying update time of files

Top Japanese page




Overview

Display update time of a file.

Flow

  1. Get update time of a file
  2. Convert the epoch time to date information
  3. Display the date information

A sample code

 my $mtime = (stat("$file"))[9];
 my ($min,$hour,$mday,$mon,$year) = (localtime($mtime))[1 .. 5];
 printf("%s/%s/%s %s:%s",$mon+1,$mday,$year+1900,$hour,$min)

Description of the code

 my $mtime = (stat("$file"))[9];

$file is the name of the file I want to get time information. By using state function, attributes are returned as an array. The tenth element(element number nine) of the array contains the update time and put it into $mttime. The other values are discarded. The value is epoch and 0 means January 1st, 1970, 0:0:0.

The other return value of the stat functoin is

 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,
     $mtime,$ctime,$blksize,$blocks)=stat("$file");

The access time is $atime, inode update time is $ctime. $size indicates file size.

 my ($min,$hour,$mday,$mon,$year) = (localtime($mtime))[1 .. 5];

Convert the update time into date information. The localtime function returns array. The second through sixth elements indicates minute, hour, day, month and year. The other return values are discarded in this example.

If you want to retrieve all return values,

 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($mtime);

you can do as above.

 printf("%s/%s/%s %s:%s",$mon+1,$mday,$year+1900,$hour,$min)

Display the date information as your favorite format. $year use 0 as 1900. So add 1900. And month indicate 0 in Janurary. So add 1.