Gossamer Forum
Home : General : Perl Programming :

How do i get a timestamp of a file ?

Quote Reply
How do i get a timestamp of a file ?
Lets say i have this output:

-rwxr-xr-x 1 mtorres support 826 Nov 12 12:47 notify

How would i grab the '12:47' part (and if possible remove the ":" to make it 1247)
I need to assign it to a variable because i will be comparing it later in the script.
The above output is the result of an 'ls -la $filename' command.

i've tried:
$timestamp = system("ls -ltr $BDIR/$host|awk '{print $8}'|awk -F: '{print $1$2}'");


but that doesnt work at all. it grabs the whole ls line for some reason
Please help, its driving me crazy!#%
Quote Reply
Re: [mtorres] How do i get a timestamp of a file ? In reply to
Hi,

I don't know if below could meet your requirement but that is more easier

my @stats = stat("$BDIR/$host");

my @d = localtime($stats[9]);

my $timestamp = $d[3] . ':' . $d[4];

Cheers,

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] How do i get a timestamp of a file ? In reply to
thanks! worked
Quote Reply
Re: [tandat] How do i get a timestamp of a file ? In reply to
Hi

Could somebody explain me as to how the above code for timestamp works?

Thank you.

Vishal
Novice Perlian
Quote Reply
Re: [vishal19178] How do i get a timestamp of a file ? In reply to
Hi Vishal,

The stat function gets all file's properties and push them into the returned array. The 9th element is the last modified time in second. The localtime will return as date time in array and its 4th adn 5th elements are hour and minutes.

For further information, you can reference its manual by perl -f stat and perl -f localtime

Cheers,

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] How do i get a timestamp of a file ? In reply to
Hi tandat,

Thank you for the information on timestamp.

Vishal

Newbie