G'day derekstucki,

Your main problem is that "my $age = (localtime ( time - $time ))[5];" is not doing what you presumable imagine it does. Consider the following which assumes an age of 25:

$ perl -Mstrict -Mwarnings -E ' my $x = time; say $x; my $y = int($x - (25*365.24225*24*60*60)); say $y; my $z = (localtime($x-$y))[5]; say $z ' 1389999563 601076303 95

That's out by 70. Using ages of 5 and 45 gives 75 and 115 respectively: i.e. out by 70 also. You added the comment "# localtime returns years since 1900": you took this into consideration with your first localtime() (i.e. ... + 1900) but ignored it with the second. The epoch started in 1970: that's 70 years since 1900. Had you added 1900 for $age, then substracted 1970, you would have got the right answer: obviously that's ridiculously complicated. Here's a much easier way to do it:

#!/usr/bin/env perl -l use strict; use warnings; use Time::Piece; use Time::Seconds; my @YMDs = ([2004, 1, 17], [2004, 1, 18], [2004, 1, 19]); my $now = localtime; print 'Now: ', $now->strftime('%Y-%m-%d'); for (@YMDs) { my $dob = Time::Piece->strptime(join('-' => @$_), '%Y-%m-%d'); print 'DOB: ', $dob->strftime('%Y-%m-%d'); print 'Age: ', int(($now->epoch - $dob->epoch) / ONE_YEAR); }

Output:

Now: 2014-01-18 DOB: 2004-01-17 Age: 10 DOB: 2004-01-18 Age: 10 DOB: 2004-01-19 Age: 9

You also have problems in your regex:

You also have problems in your validation (if( $mon < 0 or ...); for instance, 31st February would be considered valid!

-- Ken


In reply to Re: getting answer 70 years off by kcott
in thread getting answer 70 years off by derekstucki

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.