in reply to subroutine for calculating day since 1900 needed

sub days_since_1900 { use integer; my ($d, $m, $y) = @_; # $d: 1-based Day of the month (1..31) # $m: 0-based Month (Jan=0..Dec=11) # $y: 2 or 4 digit year $y += 1900 if $y < 1000; my $i = ($m + 10) % 12; my $j = $y - $i/10; return $d + 365*$j + $j/4 - $j/100 + $j/400 + ($i*306 + 5)/10 - 693 +902; }

Based on Time::Local's internals.

Update: Fixed a misnamed variable. Thanks liverpole.
Update: Added missing - 693902. Thanks rodion. Where was my head when I posted this?!

Replies are listed 'Best First'.
Re^2: subroutine for calculating day since 1900 needed
by rodion (Chaplain) on May 12, 2006 at 20:13 UTC
    Oops, you need to subtract 693902 in the return line to make it return days since Jan 1 1900. In case it's of interest, the formula is valid back to 1752 for most parts of the world, and back to 1582 for most places outside of British control. Differences have to do with when people switched to the gregorian calendar. (Some still haven't.)
Re^2: subroutine for calculating day since 1900 needed
by Anonymous Monk on Jul 16, 2016 at 23:02 UTC
    Awesome, best routine I have found on this subject. Works perfectly! THANKS!