:
This message was edited by stober at 2005-8-24 10:6:26
: :
: : The kind of computation you're looking for is very familiar to anyone who's played with astronomical computatios. The astronomer's "Julian Day Number" is the number of days that have passed from some fixed reference date. (I forget what that date is. It doesn't matter). The ideas needed to create such a program have been mentioned by previous contributors to this thread. Here is a program to compute the number of days which have passed since Jan. 1, 401. It's a lot shorter than the one written by zyb365, but it's based on the same idea. ("MJDN" stands for "modified Julian day number".)
: :
: :
: : struct date { int month; int day ; int year; };
: : #include <iostream.h>
: : #include <stdio.h>
: : long MJDN(date dt);
: :
: : main()
: : { date dt;
: :
: : cout << "Enter day of the month: ";
: : cin >> dt.day;
: : cout << "Enter month number: ";
: : cin >> dt.month;
: : cout << "Enter year: ";
: : cin >> dt.year;
: : cout << MJDN(dt) <<endl;
: : getchar();
: : }
: :
: : long MJDN(date dt)
: : // Returns number of days passed since Jan. 1, 401
: : { int k, DOY, ly;
: :
: : ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
: : if (!ly) ly += 2;
: : //ly is 1 for leap year, else 2
: : k = dt.year - 401;
: : DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
: : // DOY is "day of year".
: : return (365*k + k/4 -k/100 +k/400 +DOY -1);
: : }
: :
:
: Interesting algorithm, but I question its accuracy. According to your algorithm there are 586084 days between today (24 Aug 2005) and 1 Jan 401. But if I calculate it manually I can only come up with 585,730 days, or a difference of 354 days which is almost a full year.
:
: // number of full years since 1 Jan 401
: 2004 - 401 = 1603
: // convert years to days
: 1603 * 365 = 585,095
: // account for leap years
: 1603 / 4 = 400 leap years since 401.
: 585,095 + 400 = 585,495
: // number of days since 1 Jan 2005
: 585,495 + 235 = 575,730
:
:
:It's no surprise that you get about a full year off. The number of full years from Jan 1 401 to Jan 1 2005 is 2005 -401. Why did you write 2004 - 401? Also your number of leap years is not correct. Perhaps you're not aware of the fact that a year which is a multiple of 100 but not 400 is not a leap year, so that, for example, the years 500, 600 and 700 are not leap years, but 800 is. You seem to be counting them as leap years. The number of leap years in the years 401,402..2004 is 1604/4 -(1604)/100
+1604/400. If you make these corrections you come up with 586084.
:
If you search the web for "Julian Day Number" you'll find many versions of this program which you can run on the web or download.
By the way, an algorithm to convert from Julian day number to Gregorian date (which is our everyday civil calendar) is a bit more complex, but very interesting.
:
:
:
:
: