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

I'm a complete novice to perl and have been given the project of fixing another persons code, and to that end I have a question... using the date statement
# get filename from yesterday ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time()-24*3600); $mon++; if(length($mon)==1) { $mon="0$mon"; } if(length($mday)==1) { $mday="0$mday"; } $yesterday = "\_$year$mon$mday.zip"; print "Need to get file $yesterday\n";
I get a year of 10 can someone please help, I'm desperate. Thank you in advance

Replies are listed 'Best First'.
RE: Date/Time
by vroom (His Eminence) on Jan 05, 2000 at 22:54 UTC
    I'm not quite sure why that works like it does however if you do something like:
    use Time::localtime; $year=1900+localtime(time()-24*3600)->year; print $year,"\n";
    It seems to work. If you want to look into this more you could probably look at Time::localtime and localtime
Two Quick Optimizations
by chromatic (Archbishop) on Jan 23, 2000 at 05:22 UTC
    You might want to use just plain localtime instead of the time() - 24*3600 stuff up there.

    Also, get rid of the length calls and just use sprintf to do the leading zero format padding, if necessary.

    (And don't forget, the year bit of localtime() returns the number of years SINCE 1900. That's by design (libc, in this case). So don't prepend 19 to it.

    Bad:

    my $year = (localtime())[5]; print "The year is 19$year\n";

    Good:

    my $year = (localtime())[5]; print "The year is " . 1900+ $year . "\n";
RE: Date/Time
by xeh007 (Sexton) on Jan 08, 2000 at 05:38 UTC
    It's not returning a year of 10, it's returning a year of 100. Just add 1900 to the year ( or subtract 100 if you want 2-digit years ) and it should work.