Solved

A question about files and dates!

Posted on 1999-01-25
2
147 Views
Last Modified: 2010-03-05
I am currently writing a Perl CGI Script that will display the date of the files that have been uploaded to our website.  Basically I want it to display the date in dd-mmm-yyyy format (eg 25-Jan-1999).  

How can I write a piece of perl code that will get the date of a given file and then display the date in that format ?

I would be grateful for any advice offered regarding this.

Thanks in advance.

John Clarke
0
Comment
Question by:johnclarke
2 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1210305
print join'-',(localtime((stat $givenfile)[9])=~/(\w+) (\d+).* (\d+)/)[1,0,2];
0
 

Accepted Solution

by:
toastgoddess earned 100 total points
ID: 1210306
I'm assuming you're running under Unix, though the following code will run under either Unix or Win32.

"There is no way under standard Unix to find a file's creation time" (direct from the _Perl Cookbook_!) but since this is an upload directory presumably either the last modification time or the inode change time will do.

The subroutine can be invoked within a script as

last_modified($file)

where $file holds the name of the file you want to check.

sub last_modified {
#
#  get the filename that was passed as an argument
#
    my $file = shift;
#
#  stat returns a list of file attributes.  last modified
#  time, or mtime, is index 9 in the list.  To use the
#  inode change time instead, substitute 10 for 9 in the
#  next line.
#
    my $last_write = (stat($file))[9];
#
#  The time stat returns is in seconds since the epoch.
#  localtime converts epoch time into a list of seconds,
#  minutes, hours, and so forth.  We'll pull out the
#  values we're interested in:
#
    my ($day, $month, $year) = (localtime($last_write))[3,4,5];

#
#  Almost there - the $month variable is a number, so we'll
#  create an array of month names:
#
    my @monthnames = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct
                        Nov Dec);
#
#  Now printf prints a nicely-formatted DD-MMM-YYYY date
#  for us.  We use the $day value returned by stat, use
#  $month as an index into @monthnames, and add 1900 to
#  the $year value returned by stat.  Don't panic at the
#  sight of the 1900 - years returned by stat are not
#  necessarily two digits long, and this code will continue
#  to work after the year 2000.
#
    printf ("%02d-%3s-%04d\n", $day, $monthnames[$month], $year+1900);

}

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl program to obtain a machine's memory usage 6 22
Which version of C should I use when Perl is too slow? 13 173
Extract data from span tag 1 91
Perl modules on linux ec2 3 96
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now