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

Hey everybody, I wanted some help on these few lins of code. @f=(localtime)[3..5]; # grabs day/month/year values $SystemDate= sprintf "%d%02d%02d", $f[2] +1900, $f[1] +1, $f[0]; Right now $SystemDate has the value of yyyymmdd, so for eg it will pri +nt out today's date like below 20031217 I need it to print out the date as well as the timestamp so for eg it +should print out below 20031217150506 so that will be today's date plus the time 15:05:06 (but without the c +olons) can someone help me modify the code pasted below so that it prints out + what I want, Thanks for the input

Replies are listed 'Best First'.
Re: Date Extraction
by davido (Cardinal) on Dec 17, 2003 at 22:42 UTC
    Well, localtime also gives you the hours, minutes, and seconds, but through the power of slices, you've skipped over them in your assignment to the @f array.

    use strict; use warnings; my @f = ( localtime )[0..5]; my $system_date = sprintf "%d" . "%02d" x 5, $f[5] + 1900, $f[4] + 1, @f[ 3, 2, 1, 0 ]; print $system_date, "\n";

    Check the POD for localtime. Also, you may find perldata interesting as it discusses the use of slices. Oh, and I used the 'x' operator (see perlop) and the '.' concatenation operator (also found in perlop) to reduce the sprintf template to something more understandable.


    Dave

Re: Date Extraction
by CombatSquirrel (Hermit) on Dec 17, 2003 at 22:43 UTC
    perldoc -f localtime should help you; here is your code:
    my @time = localtime; my $time = sprintf "%04d" . "%02d" x 5, $time[5] + 1900, # year $time[4] + 1, # month @time[3, 2, 1, 0]; # day, hour, minute, second print $time;

    Hope this helped.
    CombatSquirrel.

    Update: Consider using one of the following modules:

    1.) Time::Format. Example:
    use Time::Format qw/%time/; my $time = $time{'yyyymmddhhmmss'}; print $time;
    2.) Date::Format. Example:
    use Date::Format; my $time = strftime "%Y%m%d%H%M%S", @{[localtime]}; print $time;
    Cheers.
    Entropy is the tendency of everything going to hell.
      When dealing with localtime and similar functions, I'd like to name the elements that it returns explicitly:
      my ($sec, $min, $hour, $day, $mon, $year) = (localtime)[0..5]; my $time = sprintf "%04d" . "%02d" x 5, $year + 1900, $mon + 1, $day, $hour, $min, $sec; print $time;
      And presto! Self-documenting code :-)

      Arjen

        Obligatory mention of Time::Piece:
        use Time::Piece; my $t = localtime; printf "%.4d"."%.2d"x5, $t->year, $t->mon, $t->mday, $t->hour, $t->min +, $t->sec;
Re: Date Extraction
by Zaxo (Archbishop) on Dec 17, 2003 at 22:48 UTC
    use POSIX 'strftime'; print strftime '%Y%m%d%H%M%S', localtime;

    &POSIX::strftime is the answer to a whole lot of questions concerning time. The format strings are documented in man strftime, not in the perl docs.

    After Compline,
    Zaxo

Re: Date Extraction
by Anonymous Monk on Dec 17, 2003 at 22:41 UTC
    Hi I meant the code pasted above .. not below ..sorry about that mistake .. THanks