linux-l: Perl Datum Formatieren

Daniel Winkelmann winkelma at chemie.fu-berlin.de
Mo Apr 9 18:47:40 CEST 2001



On Mon, 9 Apr 2001, Guido Roock wrote:

> Hallo,
> 
> ich benötige ein Datum im Format "DD.MM.YYYY HH:MM:SS".
> Dafür hab ich mir etwas in Perl geschrieben. Jetzt frage
> ich mich, ob man das noch verkürzen kann. Mein Source funktioniert
> zwar, ist aber sehr lang.
> 
> Ja, es muß Perl sein. Es ist nur ein Teil von einem großen Perl-Script.
> 
> Hier ist mein Source. Wo kann man kürzen?
> 

Ueberall ... wieso das Rad neu erfinden ? Loesung siehe unten.

> #!/usr/bin/perl -w
> 
> $now = time;
> $timelocal = localtime($now);
> &Monat;
> @date = split(/ /,$timelocal);
> 
> $tag = sprintf("%02d",$date[3]);
> $month = sprintf("%02d",$date[1]);
> $year = sprintf("%04d",$date[5]);
> print "$tag.$month.$year $date[4]\n";
> 
> sub Monat
> {
>         $timelocal =~ s/Jan/1/;
>         $timelocal =~ s/Feb/2/;
>         $timelocal =~ s/Mar/3/;
>         $timelocal =~ s/Apr/4/;
>         $timelocal =~ s/May/5/;
>         $timelocal =~ s/Jun/6/;
>         $timelocal =~ s/Jul/7/;
>         $timelocal =~ s/Aug/8/;
>         $timelocal =~ s/Sep/9/;
>         $timelocal =~ s/Oct/10/;
>         $timelocal =~ s/Nov/11/;
>         $timelocal =~ s/Dec/12/;
> }
> 

use POSIX qw(strftime) ;

my $time = strftime "%d.$m.%Y %T" , localtime time ;

> Gruß Guido
> 

Ciao,
	Daniel Winkelmann





Mehr Informationen über die Mailingliste linux-l