Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Date and time for log files

by ibaboom (Initiate)
on Oct 04, 2007 at 15:43 UTC ( #642684=perlquestion: print w/replies, xml ) Need Help??

ibaboom has asked for the wisdom of the Perl Monks concerning the following question:

Good day I would like to use the date and time for the name of a log file. I have tried using a number of the date functions and have not gotten it to work. What I would like the output to be is, YYYYMMDDHHMMSS.log How can I get the date and concatenate it with the .log Thanks AB

Replies are listed 'Best First'.
Re: Date and time for log files
by roboticus (Chancellor) on Oct 04, 2007 at 15:48 UTC
    ibaboom:

    Howzabout:

    #!/usr/bin/perl -w use strict; use warnings; my @gm = localtime(time); printf "%04u%02u%02u%02u%02u\n", $gm[5]+1900, $gm[4]+1, $gm[3], $gm[2], $gm[1], $gm[0];

    ...roboticus

      /me = dork
        No you're not. If you hadn't asked the question I wouldn't have seen roboticus's nifty little snippet which is now filed away in my snippet toolbox. So, thanks for asking the question :-)
Re: Date and time for log files
by moritz (Cardinal) on Oct 04, 2007 at 15:46 UTC
Re: Date and time for log files
by graff (Chancellor) on Oct 04, 2007 at 22:56 UTC
    use POSIX; my $filename = strftime( "%Y%m%d%H%M%S.log", localtime ); print "lets open a file called $filename ...\n";
    Personally, I like to throw in a little punctuation, because it's easier to look at, and it can be easier to grep for a given day or a given hour:
    strftime( "%Y%m%d_%H%M%S.log", localtime );
Re: Date and time for log files
by Akoya (Scribe) on Oct 04, 2007 at 17:27 UTC

    I have a function that gives me a formatted timestamp string when I need it. It will format a value I pass, or if I don't pass anything to it, it uses the current time.

    sub _timestamp { my $t = shift; # allow me to pass in a time $t = time unless $t; # use the current time if none specified my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me($t); my $timestamp = sprintf("%04u-%02u-%02u %02u:%02u:%02u", $year + 1900, $mon + 1, $mday, $hour, $min, $sec); return $timestamp; }

    All you have to do is change the format to however you like. You can then use the returned string as part of a filename, or as I use it, to prefix entries in my log files.

    Cheers,
    Akoya

Re: Date and time for log files
by nimdokk (Vicar) on Oct 04, 2007 at 17:21 UTC
    Yet another way to do it might be as follows:
    #!/usr/bin/perl use Time::localtime; $log_name=sprintf("my_log.%04d%02d%02d%02d%02d%02d.log", localtime->year()+1900, localtime->mon()+1,localtime->mday(), localtime->hour(), localtime->min(),localtime->sec()); print $log_name . "\n";
    Would give you:

    my_log.20071004122204.log
    update: removed period between date and time to better reflect string from OP.
Re: Date and time for log files
by sago (Scribe) on Oct 05, 2007 at 04:43 UTC

    my @t = localtime(time);
    my ($rf, $td);
    $t4++;
    $td = (1900 + $t5) . ((length($t4)==1)?(0 . $t4):$t4) . ((length($t3)>1)?$t3:(0 . $t3)) . ((length($t2)==1)?(0 . $t2):$t2) . ((length($t1)>1)?$t1:(0 . $t1)) . ((length($t[0])>1)?$t[0]:(0 . $t[0]));

    open(PLOG, ">>/usr/perl_scripts/$td.log");
    print PLOG "Hello World";
    close(PLOG);
    The above sample script will create the log file with date and time.
      Please put <code> (or <c>) at the beginning of any program code or data, and put </code> (or </c>) at the end of your code or data, when you post.

      Read Writeup Formatting Tips.

      Learn from what other people have posted.

      And please, please, PLEASE use <code> tags when you post code or data (abbreviate them to just "c" if you like, but use these tags).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://642684]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-05-28 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (99 votes). Check out past polls.

    Notices?