XP is just a number PerlMonks

### Comment on

 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!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• 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
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-02-22 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (288 votes). Check out past polls.

Notices?