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

As my title implies, I am trying to come up with a method within perl that creates the same output as the above unix date command. I have the (very cludgy) following:

#!/usr/bin/perl -w use strict; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year = $year+1900; $mon = $mon+1; if ( $mon < 10 ) { $mon = "0$mon"; } my $yyyymmdd = "$year$mon$mday"; print "$yyyymmdd\n";

Although this works, I can barely believe that there is not a better way. I read a few posts about Time::Local but it didnt seem to provide method with that much savings in typing. Was I overlooking the benefits?

humbly -c

Edit ar0n -- more title descriptive

Replies are listed 'Best First'.
Re: date +%Y%m%d
by chipmunk (Parson) on Aug 21, 2001 at 17:22 UTC
    Try the strftime function in the POSIX module.
    use POSIX; print strftime("%Y%m%d", localtime);
Re: date +%Y%m%d
by clemburg (Curate) on Aug 21, 2001 at 17:21 UTC

    How about this:

    > perl -MDate::Manip -e 'Date_Init("TZ=CET", "DateFormat=non-Us"); pri +nt UnixDate(ParseDate(shift), "%Y%m%d")' 20.03.2001 20010320

    (Note conversion of German date input style to ISO style)

    You might also want to have a look at create file with current date for name.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: date +%Y%m%d
by Hofmator (Curate) on Aug 21, 2001 at 17:27 UTC

    It can also be done in one line, although that does not necessarily improve the readability ;-) printf "%04d%02d%02d", sub{$_[0]+1900,$_[1]+1,$_[2]}->((localtime)[5,4,3]);

    -- Hofmator

Re: date +%Y%m%d
by arturo (Vicar) on Aug 21, 2001 at 17:29 UTC

    There are a few ways to do this in perl. It's a good day to learn about array slices and s/printf.

    # this takes the 4th through sixth elements of the array # returned by localtime and sets @times to them my @times = (localtime)[3..5]; my $date = sprintf("%4d%02d%02d", $times[2]+1900, $times[1]+1, $times[ +0]);

    The "%02d" formatting string (roughly: give me two digits, add enough '0's to the front to make the field two wide) is highly useful for 0-padding things to a specified length (in this case, 2).

    Overall, it's more typing than the external call to date? Sure, but it's more portable and (although I haven't benchmarked it), more efficient (doesn't spawn a subshell).

    HTH.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: date +%Y%m%d
by snafu (Chaplain) on Aug 21, 2001 at 17:33 UTC
    Yes, POSIX is your friend! Using POSIX::strftime is how I would do it too. There is no need for me to give an example because Chipmunk did a fine job of that himself.

    ----------
    - Jim

Re: date +%Y%m%d
by JP Sama (Hermit) on Aug 21, 2001 at 17:25 UTC
    probably because you dont need ANOTHER variable to do that.. just print the $year$mon$mday; and you'll get what you want... right?

    I'm not sure I got your question right.. (sorry if i didn't)

    - jpsama