I am trying to turn a date of birth into an age using timelocal and localtime as follows:
if( $entry =~ /^ *(\d{1,2})[ -\/\\]+(\d{1,2})[ -\/\\]+(\d{4}) *$/ ) { # $mon should be 0 indexed, so subtract 1 my $mon = $1 - 1; my $day = $2; my $year = $3; # localtime returns years since 1900 my $currentyear = (localtime( time() ))[5] + 1900; if( $mon < 0 or $mon > 11 or $day < 1 or $day > 31 or $year > $currentyear # the 130 is setting a max acceptable age or $year < $currentyear - 130 ) { return undef; } my $time = timelocal( 0, 0, 0, $day, $mon, $year ); my $age = (localtime ( time - $time ))[5]; # do some stuff with it }
When I test this, it always comes up with an age 70 years too old. Is there a discrepancy between the epoch used by localtime and timelocal? And if so, does that difference vary from system to system, ie linux to mac to windows?
In reply to getting answer 70 years off by derekstucki
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |