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

sub show_when_last { my $file = shift @_; my ($atime, $mtime) = (stat ($file) )[8,9]; my $whenstring = "File: $file\n"; my %filestats = (Accessed => $atime, Modified => $mtime); my ( $modsecs, $days, $hours, $mins, $secs ); print "TIME " , localtime($atime) , "\n"; while( my ($term, $ftime) = each %filestats ) { $modsecs = (time - $ftime); # mod time in epoch secs. $days = $modsecs / 86400; # secs/day $modsecs %= 86400; # days remainder $hours = $modsecs / 3600; # secs/hour $modsecs %= 3600; # hours remainder $mins = $modsecs / 60; # secs/min $secs = $modsecs % 60; # remaining secs $whenstring .= sprintf ("$term: %02d days, %02d hours, %02d minu +tes, and %02d seconds ago.\n", $days, $hours, $mins, $secs ); } print "$whenstring\n"; }
OUTPUT:
TIME 5025151021092680 File: /home/finload/load/data//GMTKFUTPDV0120090309.TXT Accessed: 00 days, 02 hours, 44 minutes, and 37 seconds ago. Modified: 00 days, 02 hours, 44 minutes, and 37 seconds ago.
How can I get the atime format as <code> Mar 10 15:25 <code> Any idea?

Replies are listed 'Best First'.
Re: getting time value from any datetime modules
by Corion (Patriarch) on Mar 10, 2009 at 09:49 UTC

    The most convenient way to produce human readable timestamps is through POSIX::strftime:

    my @atime = localtime($atime); print strftime "%b %d %H:%M\n", @atime;

    See, for example, this strftime manpage for the placeholders.

Re: getting time value from any datetime modules
by Marshall (Canon) on Mar 10, 2009 at 11:50 UTC
    I suggest the following....
    I just showed the year as a reminder to add 1900 if you need that. Anyway once you have epoch time, atime or mtime conversion is the same to the type of string you want. DayofWeek would be similar to @Month_text...
    #!usr/bin/perl -w use strict; my @Month_text = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my $source_path = "./testfile"; (my $m_time) = (stat($source_path))[9]; my ($i_month, $day, $year, $hour, $minutes) = (localtime($m_time))[4,3 +,5,2,1]; $year += 1900; printf "%s %2d %4d %d:%d\n",$Month_text[$i_month],$day,$year,$hour,$mi +nutes,"\n"; __END__ prints: Mar 10 2009 2:58