Archive forJanuary, 2007

How to get a date or timestamp of a file in perl.

Once i was trying to get the date when the file is written in perl. I found the following solutions all of them are giving the complete timestamp of the file but i needed only the date.
You can get the timestamp of a file in following ways in perl :

First Method :

$write_secs = (stat($file))[9];
print “file $file updated at “, scalar(localtime($file)), “\n”;

Second Method :
Using standard module File::stat and Time::locatime(bundled with all 5.x perl installations)

use File::stat;
use Time::localtime;
$date_string = ctime(stat($file)->mtime);
print “file $file updated at $date_string\n”;

To get only the data of file I used this solution :

$tm = localtime(stat($path)->mtime);
#get only the data from time object
$date_string = $tm->mday.”-”.(($tm->mon)+1).”-”.(($tm->year)+1900);
print “file $file updated at $date_string\n”;

Comments

Test

Comments

"Quote of the Day"