Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last modification date formatting

Status
Not open for further replies.

Perlu

Programmer
Feb 2, 2005
3
CH
Hi everybody,

I don't know all the perl functionalities. I have one line perl script that reads the last modification date of a file then inserts it into a variable:

$fileDate = ctime(stat($temp_filename)->mtime);

This is the result:

Mon Dec 20 07:49:51 2004

How could we format this date as:

20.12.2004 07:49:51 (i.e. DD.MM.YYYY.....)

Thanks for your help.



 
stat(...)->mtime (an extended module functionality equivalent to the perl built-in function (stat(...))[9]) returns an epoch-timestamp - that is, a time in seconds since the start of 1970. ctime() is another extended module functionality which probably already comes with the ability to specify a format, but it would depend entirely on what module you have installed as to how to specify that format. If you look up what module you have acquired ctime() from, this would undoubtedly be the simplest method to code for.

Without using any additional modules, the perl built-in function localtime(...) returns by default the string format that you're currently getting, but can also return its result as an array of values - ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) to be precise. You could use this array to output the data in any way you choose - for instance:
Code:
@_ = localtime((stat($temp_filename))[9]);
# The month value comes in the range 0..11 for historical reasons
$_[5] += 1;
# The year value has a zero-point of 1900
$_[6] += 1900;
# Insert possible leading zeroes if desired
foreach $_ (0,1,2,4,5) {
  $_[$_] = substr("0$_[$_]", -2);
}
# Create a formatted output string
$fileDate = "$_[4].$_[5].$_[6] $_[2]:$_[1]:$_[0]";
 
Dear MORAC,

Thanks for your clarification. In fact, I am using ActivePerl 5.8.6 Build 811 on Windows XP dev machine. I don't know which modules are added to perl by ActiveState.
I have tried the perl built-in function localtime as you mentioned and these are the results:

localtime((stat($temp_filename))[9])
------------------------------------

Time::tm=ARRAY(0X1824394)

"$_[4].$_[5].$_[6] $_[2]:$_[1]:$_[0]"
-------------------------------------

0.01.1900 0:0:c>

So, I don't know why localtime bulit-in function doesn't work. Do you have any idea?

Thanks once again.
 
Regarding knowing what modules are installed on your system, I have just submitted a FAQ to explain how to do this, which is taken from a thread on Libraries (thread219-994818), to which I posted the other day.

The code is essentially:

Code:
#!/usr/bin/perl -w
use strict;

use File::Find;

my %modlist;
find(\&print_package, @INC);

sub print_package {                                   
    if ($File::Find::name =~ /\.pm$/) {
        return unless open(PM, $File::Find::name);
        foreach (<PM>) {
            if (/^ *package +(\S+);/) {
                $modlist{$1}=1;
            }
        }
        close(PM);
    }
}

print "$_\n"    for(sort keys %modlist);

Barbie
Leader of Birmingham Perl Mongers
 
Perlu,

localtime is a built in function, can you post the relevant code you tried

@var=localtime(); will return an array
$var=localtime will return the scalar value you had in your earlier post

Regards
--Paul
 
Looking up ActivePerl on a simple search engine, I find that localtime too has been overloaded by the default libraries in the same way as stat has, and returns objects with named functions for retrieving the original array elements in the same way. The code could therefore modify to something like:
Code:
$fileTime = stat($temp_filename)->mtime;
$fileDate = substr('0' . localtime($fileTime)->mday . '.', -3);
$fileDate .= substr('0' . (localtime($fileTime)->mon + 1) . '.', -3);
$fileDate .= (localtime($fileTime)->year + 1900) . ' ';
$fileDate = substr('0' . localtime($fileTime)->hour . ':', -3);
$fileDate = substr('0' . localtime($fileTime)->min . ':', -3);
$fileDate = substr('0' . localtime($fileTime)->sec, -2);
I haven't tested this version as I don't use ActivePerl, but the theory is straightforward enough.
 
Dear all ,

MOrac, you are right. The localtime is override by this statement:
use Time::localtime;

This is the script that I have develped according to your previous notes:
sub format_file_date
{
my $tmp = stat($temp_filename)->mtime;
chomp($tmp);

$_[3]= localtime($tmp)->mday;
$_[4]= localtime($tmp)->mon;
$_[5]= localtime($tmp)->year;
$_[2]= localtime($tmp)->hour;
$_[1]= localtime($tmp)->min;
$_[0]= localtime($tmp)->sec;

# The month value comes in the range 0..11
$_[4] += 1;
# The year value has a zero-point of 1900 for windows xp
$_[5] += 1900;
# Insert leading zeroes
foreach $_ (0,1,2,4)
{
$_[$_] = substr("0$_[$_]", -2);
}
# Create a formatted output string
$tmp = "$_[3].$_[4].$_[5] $_[2]:$_[1]:$_[0]";

return $tmp;
}

So thanks to all of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top