Igor
Igor

Reputation: 27268

How can I get a file's modification date in DDMMYY format in Perl?

What's the best way to get a file's modification date in DDMMYY format in Perl?

(Originally the question was "How to do it without the DateTime module". I removed this restriction in order to make the question more general.)

Upvotes: 3

Views: 41832

Answers (5)

Беров
Беров

Reputation: 1393

See Time::Piece for object oriented syntax. It also comes with Perl by default in versions 5.10 and next. Usually at some later point you need more functionality. Using Time::Piece does not import anything beside localtime and gmtime. And you can ask it not to import anything too by just saying use Time::Piece ();.

use Time::Piece;
localtime((stat $filename)[9])->ymd;                # 2000-02-29
localtime((stat $filename)[9])->ymd('');                 # 20000229
localtime((stat $filename)[9])->dmy("");#is what you wanted



$perl -MTime::Piece -e 'print localtime(time)->dmy("")'

Upvotes: 12

brian d foy
brian d foy

Reputation: 132920

If this is the only date formatting I need to do and don't need any date features for anything else, I just use POSIX (which comes with Perl):

use POSIX;

my $file = "/etc/passwd";

my $date = POSIX::strftime( 
             "%d%m%y", 
             localtime( 
                 ( stat $file )[9]
                 )
             );

Forget about -M if you aren't using relative time. Go directly to stat to get the epoch time.

Upvotes: 18

Ed Guiness
Ed Guiness

Reputation: 35287

UPDATE: Note that this answer was to the original question, which specifically excluded use of DateTime modules.

To get modified time

my $mtime = (stat $filename)[9];

This returns the last modify time in seconds since the epoch.

Then, to translate to date components, use localtime -

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

Note that localtime returns 0 for Jan, 1 for Feb, etc, and $year is the number of years since 1900, so 109 -> 2009.

Then to output in the format DDMMYY -

print substr('0'.$mday,-2), substr('0'.($mon+1),-2), substr($year+1900,-2);

So it's easier - and less error-prone - to simply use Date::Format if you can.

Upvotes: 4

Telemachus
Telemachus

Reputation: 19725

Here's a reasonably straightforward method using POSIX (which is a core module) and localtime to avoid twiddling with the details of what stat returns (see Edg's answer for why that's a pain):

use POSIX qw/strftime/;

my @records;

opendir my $dh, '.' or die "Can't open current directory for reading: $!";

while (my $item = readdir $dh) {
    next unless -f $item && $item !~ m/^\./;
    push @records, [$item, (stat $item)[9]];
}

for my $record (@records) {
    print "File: $record->[0]\n";
    print "Mtime: " . strftime("%d%m%Y", localtime $record->[1]), "\n";
}

Upvotes: 4

Igor
Igor

Reputation: 27268

use Date::Format;
print time2str("%d%m%y", (stat $filename)[9]);

Upvotes: 1

Related Questions