+// return the integral part of the JDN for the midnight of the given date (to
+// get the real JDN you need to add 0.5, this is, in fact, JDN of the noon of
+// the previous day)
+static long GetTruncatedJDN(wxDateTime::wxDateTime_t day,
+ wxDateTime::Month mon,
+ int year)
+{
+ // CREDIT: the algorithm was taken from Peter Baum's home page
+
+ // the algorithm assumes Jan == 1
+ int month = mon + 1;
+
+ // we want the leap day (Feb 29) be at the end of the year, so we count
+ // March as the first month
+ if ( month < wxDateTime::Mar + 1 )
+ {
+ month += MONTHS_IN_YEAR;
+ year--;
+ }
+
+ // this table contains the number of the days before the 1st of the each
+ // month (in a non leap year) with the third value corresponding to March
+ // (and the last one to February)
+ static const int monthOffsets[14] =
+ {
+ 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306
+ };
+
+ // and now add contributions of all terms together to get the result (you'd
+ // better see the Web page for the description if you want to understand
+ // why it works (if it does :-))
+ return day +
+ // linear approximation for months
+ monthOffsets[month - (wxDateTime::Mar + 1)] +
+ // the year contribution
+ 365*year + year/4 - year/100 + year/400 +
+ // 1721119.5 is the JDN of the midnight of Mar 1, year 0
+ 1721118;
+}
+