Solved

A question about files and dates!

Posted on 1999-01-25
2
148 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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