[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Real World Perl



OK, it's not a shorter DeCSS implementation, but... I seeded FWP with some of
these "way back when" and here's one I wanted to share.  A Real World Code
Example.  It's called "GetToday" and it wants to... return the date in
YYYYMMDD format. Ready?

sub GetToday {

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdist);
    my($string1,$cc,$ccyear);

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdist) = localtime(time);

    if ( $year < 100 ) {
        $cc = 19;
      }
    else {
       $cc = 20;
       $year = $year - 100;
       $year = "0" . $year if length($year) < 2;
      }

    $mon++;

    $ccyear = $cc . $year;

    $string1 = $mon . "/" . $mday . "/" . $ccyear;

    $today = &NormalizeDate($string1);

    return $today;
}

(NormalizeDate, (which I leave as an exercies to the reader; it is also "Real
World Code" :-), takes a date in any of several /-delimited formats(with or
without leading 0s, 2- or 4- digit year) and returns YYYYMMDD).

I was amused by the fact that the sub author had done sufficient research to
type in all the possible return values from localtime... yet missed the
explanation of what it means for $year to be represented as the current year
- 1900.


So, can it be done in one line, (without calling localtime more than once)?

sub GetToday2 {
    my (undef,undef,undef,$mday,$mon,$year) = localtime(time);

    sprintf("%4d%02d%02d", $year + 1900, $mon+1, $mday);

}

(if I check in changes, I will put the "return" in there; I don't want to be
"that cute" in production code :)

- Vicki
-- 
--
Vicki Brown            ZZZ                      mailto:vlb@cfcl.com
Journeyman Sourceror:     zz  |\     _,,,---,,_       P.O. Box 1269
  Scripts & Philtres       zz /,`.-'`'    -.  ;-;;,_  San Bruno, CA
  (Perl, Unix)               |,4-  ) )-,_. ,\ ( `'-'  94066     USA
http://www.cfcl.com/~vlb    '---''(_/--'  `-'\_)    +1-650-873-7842

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe