Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-23 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found