Solved

A question about files and dates!

Posted on 1999-01-25
2
146 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
Comment Utility
print join'-',(localtime((stat $givenfile)[9])=~/(\w+) (\d+).* (\d+)/)[1,0,2];
0
 

Accepted Solution

by:
toastgoddess earned 100 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Shell Script to find a string in server(s) log file 2 258
Input from stdin for perl 6 108
Perl string filter 5 63
create a gui in perl 3 42
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

12 Experts available now in Live!

Get 1:1 Help Now