in reply to Re: Directory creation with current Date
in thread Directory creation with current Date

Hello,

Usually I want to avoid the load of the POSIX module so here is what i do :

$formatted_date = now_date() ; sub now_date { my @now = localtime ; my $now_day = $now[3] ; my $now_year = $now[5] + 1900 ; my $now_month = $now[4] + 1 ; if ($now_month < 10) { $now_month = "0".$now_month } if ($now_day < 10) { $now_day = "0".$now_day } my $now_scal = join "-", ( $now_year, $now_month, $now_day ) ; return $now_scal ; }
Update: I guess it's better with sprintf, like mkirank does :
sub now_date { my @now = localtime ; my $now_day = $now[3] ; my $now_year = $now[5] + 1900 ; my $now_month = $now[4] + 1 ; $now_scal = sprintf "%04d-%02d-%2d", $now_year , $now_month ,$now_ +day ; return $now_scal }
More Update: As demerphq says it could in the end very well be much better to use POSIX qw(strftime)
Coding practice evolve !

Replies are listed 'Best First'.
Re^3: Directory creation with current Date
by demerphq (Chancellor) on Jan 19, 2005 at 15:28 UTC

    Usually I want to avoid the load of the POSIX module so here is what i do

    Why do you want to avoid POSIX?

    ---
    demerphq

      Why do you want to avoid POSIX?

      I know it's in the standard distribution and I'm sure loading it will not be significant at all but ... if it's not *really* needed, well, it's just one less module to load :)

        Well, not that you probably care but I certainly consider that a form of premature optimization. If you are sure that POSIX will cause you issues then I could understand it, absent such evidence its just bad practice. For one, the code you write is less likely to be correct than the code in POSIX, for two if you use a nontrivial selection of modules the likely that POSIX gets loaded anyway is pretty high. So you increase code bloat, probably reduce your run times (I admit I havent benchmarked), increase your chance for error, all for a gain that may not even occur at all. Doesnt sound like good practice to me.

        ---
        demerphq