[thelist] Date returning 20100 - second try

Steven Champeon schampeo at hesketh.com
Thu Jun 29 20:27:41 CDT 2000


on Thu, Jun 29, 2000 at 05:51:13PM -0500, CDitty wrote:
> Haven't seen this come through yet.  Please forgive if this is a dup.
> 
> Can someone tell me why this routine is returning 20100 instead of 2000?
> 
> @date = localtime(time);     #puts the current time/date into an array
> $day = $date[3] + 1;         #have to add 1 since it returns days starting at 0
> $month = $date[4] + 1;       #same with the month
> $yr = "20" . $date[5];       #only returns a 2 digit year, change 19 to 20 
> after y2k
> print "$yr\n";

Because that's how localtime works? Try reading the manual page. The
struct returned by localtime() contains the following member:

       tm_year
              The number of years since 1900.

Note that "number of years since 1900" is not, as the comment above
claims, the two-digit string representing the year. Granted, it's the
manual page for the localtime C function, not for the Perl localtime,
However, the perlfunc manpage contains the following bit of useful
information:

       localtime EXPR
               Converts a time as returned by the time function
               to a 9-element array with the time analyzed for
               the local time zone.  Typically used as follows:

                   #  0    1    2     3     4    5     6     7     8
                   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                               localtime(time);

               All array elements are numeric, and come straight
               out of a struct tm.  In particular this means that
               $mon has the range 0..11 and $wday has the range
               0..6 with sunday as day 0.  Also, $year is the
               number of years since 1900, that is, $year is 123
               in year 2023, and not simply the last two digits
               of the year.  If you assume it is, then you create
               non-Y2K-compliant programs--and you wouldn't want
               to do that, would you?

Seems straightforward to me.

Steve

-- 
http://a.jaundicedeye.com/weblog/
because it just annoys people when I talk to myself...




More information about the thelist mailing list