Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Portable previous dates

by vt220 (Scribe)
on Jul 09, 2011 at 00:31 UTC ( #913450=note: print w/ replies, xml ) Need Help??


in reply to Portable previous dates

A long while back, after finding these:

http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
http://www.smart.net/~mmontes/ushols.html

I put together the following subs. Please note that I add or subtract 3 in the conversion routines. This allows a modulo function to return the day of the week with 0 equaling Sunday. As in:

$DoW = $gdate % 7;

I humbly submit the following for review...

#=================================================================== # d2g() Convert year, month, and day to date factor G. sub d2g { my($y, $m, $d) = @_; my $g; $m = ($m + 9) % 12; $y = $y - int($m/10); $g = 365*$y + int($y/4) - int($y/100) + int($y/400) + int(($m*306 ++ 5)/10) + ($d - 1); #------- $g += 3; return $g; } #==================================================================== # g2d() Convert date factor G to year, month, and day. sub g2d { my($g) = @_; my($y, $m, $d, $M, $D); #----------------------------------- $g -= 3; #------- $y = int((10000*$g + 14780)/3652425); $D = $g - (365*$y + int($y/4) - int($y/100) + int($y/400)); if ( $D < 0 ) { $y = $y - 1; $D = $g - (365*$y + int($y/4) - int($y/100) + int($y/400)); } $M = int((100*$D + 52)/3060); $m = ($M + 2)%12 + 1; $y = $y + int(($M + 2)/12); $d = $D - int(($M*306 + 5)/10) + 1; return ($y, $m, $d); } #==================================================================== # ValidYMD() Returns true if given year, month, and day is valid. sub ValidYMD { my($y, $m, $d) = @_; my($Y, $M, $D) = g2d( d2g($y, $m, $d) ); return $y == $Y && $m == $M && $d == $D; }


Comment on Re: Portable previous dates
Select or Download Code
Re^2: Portable previous dates
by Anonymous Monk on Jul 09, 2011 at 13:29 UTC
    Thanks for the response vt220, will review this code and see what I can learn from it. Darryl

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://913450]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2015-07-04 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls