Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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; }

In reply to Re: Portable previous dates by vt220
in thread Portable previous dates by dtbach

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (14)
    As of 2014-04-18 21:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (472 votes), past polls