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

I have to install this date in the format 2000.09.11. The 09 being september. I have the following code that gives me the date in the correct sequence. I am not able to get the month to two digits or the day 1st thru 9th to two digits. This is probably a baby question. Code to follow.

my($year, $month, $day) = (localtime)[5 ,4 ,3];<br> $year1 = $year+1900;<br> $month1 = $month+1;<br>

Replies are listed 'Best First'.
Re: Date format question
by BastardOperator (Monk) on Sep 12, 2000 at 04:23 UTC
    Here's my 2 cents:
    use POSIX qw(strftime); print strftime("%Y.%m.%d", localtime()), "\n";

    small tip, use the system for dates, don't try to add and whatnot, what happens when it's the twelfth month? get 13.
      I always forget about the POSIX stuff, of course I also wish Perl had a matrix operator so that I could say something like:
      printf '%d,%02d,%02d', (1900,1,0) plus reverse +(localtime)[3..5];
      Er... I guess really that's just a vector operator... I know there are modules to do that, but I think it should be part of the Perl core... otherwise what's the point? I could just as easily write it:
      sub add_vectors { my @left = @{ shift }; my @right = @{ shift }; my @result = (); my $index = $#left > $#right ? @left : @right; while( --$index >= $[ ) { $left[$index] = 0 if not defined $left[$index]; $right[$index] = 0 if not defined $right[$index]; $result[$index] = $left[$index] + $right[$index]; } return \@result; }
      A sloppy first pass, but you get the idea.
      If we're talking Perl, you won't get 13 if it is the twelfth month, because localtime delivers months from 0 to 11.

      Besides that I like your answer.

Re: Date format question
by athomason (Curate) on Sep 12, 2000 at 03:16 UTC
    printf and more generally sprintf allow you to specify how wide you want fields to print. printf just takes the string returned by sprintf and actually prints it, but the format specifiers are documented with sprintf. Try this:

    printf("%4d.%02d.%02d\n", $year, $month, $day);


    As BlaisePascal points out, this snippet assumes that the variables already contain what you want to print out; you may need to coerce return values from time functions to get them into the appropriate form.

      That is, if you don't want today's date to be printed as 100.09.11

      You might want to use sprintf if you are trying to get it in a string.

        Actually, you'll also want to use $month+1...

RE: Date format question
by Adam (Vicar) on Sep 12, 2000 at 03:48 UTC
    @_ = (0,1,1900); $_ = sprintf '%4d.%02d.%02d', map {$_+pop @_} reverse +(localtime)[3.. +5]; # $_ = 2000.09.11 on Sept 11th, 2000. # Another way that is more readable is: my( $day, $month, $year ) = (localtime)[3..5]; $_ = sprintf '%4d.%02d.%02d', 1900+$year, ++$month, $day;
RE: Date format question
by OzzyOsbourne (Chaplain) on Sep 12, 2000 at 20:34 UTC
    I did it this way:
    @xtime=localtime(time); $day=$xtime[3]+1; $month=$xtime[4]+1; $year=$xtime[5]+1900; $hours=$xtime[2]; $mins=$xtime[1]; $secs=$xtime[0]; $logname='//server/share/'."ftp$month$day$year$hours$mins$secs".'.log' +;
    Hope that helps... -ozzyosbourne