Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How do I incorporate a timestamp into a file name?

( #93258=categorized question: print w/ replies, xml ) Need Help??
Contributed by peschkaj on Jul 02, 2001 at 22:09 UTC
Q&A  > dates and times


Answer: How do I incorporate a timestamp into a file name?
contributed by particle

i prefer

use POSIX; use constant DATETIME => strftime("%Y-%m-%d_%H-%M-%S", localtime);
this returns something like "2001-07-02_15-07-32"

i use this to name temp directories, like

my $tmpdir = $ENV{TMP} . '/' . basename $0 . '.' . DATETIME . '.' . $$
which gives me a uniquely named dir. then my named temp files are more human readable, because i don't need the date/time string in their names.

~Particle

Answer: How do I incorporate a timestamp into a file name?
contributed by mikeB

# grab the current time my @now = localtime(); # rearrange the following to suit your stamping needs. # it currently generates YYYYMMDDhhmmss my $timeStamp = sprintf("%04d%02d%02d%02d%02d%02d", $now[5]+1900, $now[4]+1, $now[3], $now[2], $now[1], $now[0]); # insert stamp into constant portion of file name. # the constant portion of the name could be included # in the sprintf() above. my $fileName = "File$timeStamp.log";
Answer: How do I incorporate a timestamp into a file name?
contributed by bikeNomad

The easiest way to do it (assuming you don't need to have humans interpret it) is to just use the numeric return value from time():

my $filename = $directory . '/' . time() . '.ext';
Otherwise, you can put together the various numbers from localtime:

my @lt = localtime(time); my $filename = sprintf "%s/%04d%02d%02d%02d%02d%02d", $directory, $lt[ +5]+1900, $lt[4]+1, @lt[3,2,1,0];
Answer: How do I incorporate a timestamp into a file name?
contributed by DeadPoet

For a standard implementation that may be reused throughout the program:

Example

use Time::localtime; sub timestamp { my $t = localtime; return sprintf( "%04d-%02d-%02d_%02d-%02d-%02d", $t->year + 1900, $t->mon + 1, $t->mday, $t->hour, $t->min, $t->sec ); } print '[' . timestamp() . ']: my message'. "\n";

Output:

[2011-01-08_11-48-29]: my message

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (7)
    As of 2015-07-04 21:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (60 votes), past polls