### Thread: Confusion in Julian day to Gregorian Date conversion code. Pls Explain the logic

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
1
Rep Power
0

#### Confusion in Julian day to Gregorian Date conversion code. Pls Explain the logic

VOID GetGregorianDate(LONG JD, PWORD Year, PWORD Month, PWORD Day)
{
LONG j, y, d, m;

//while calculating we consider the following :
//for Years -> 4 centuries (146067 days)
//for Months -> 4 years and 5 consecutive months
//for days -> 5 consecutive months

j = JD - 1721119; //1721119 is the number of days since JD 0 to the start of March 2 1BC. //hence j are the no of days from March 2 1BC
y = (4 * j - 1) / 146097; //146097 is the number of days in four centuries, time it takes for the cycle of leap years to repeat.
j = 4 * j - 1 - 146097 * y; //4 is used to denote leap year
d = j / 4;
j = (4 * d + 3) / 1461; //number of days in four years.
d = 4 * d + 3 - 1461 * j;
d = (d + 4) / 4;
m = (5 * d - 3) / 153; //number of days in 5 consecutive months alternating between 31 and 30 days (either Mar-Jul or Aug-Dec).
d = 5 * d - 3 - 153 * m;
d = (d + 5) / 5;
y = 100 * y + j;
if (m < 10)
{
m = m + 3; //additions and subtractions of 3 and 9 are to restore the "start" of the year to January.
}
else
{
m = m - 9; //additions and subtractions of 3 and 9 are to restore the "start" of the year to January.
y = y + 1;
}

*Year = (WORD) y;
*Month = (WORD) m;
*Day = (WORD) d;
}
2. What is it that you don't understand? If you were to work through the derivation then it should make sense, especially given all the comments (which I've never seen offered before). Is it that you don't understand what Julian Date is? Or the leap-year rules of the Gregorian Calendar?

But first, you must tell us what these typedef's mean:
VOID
LONG
WORD
PWORD

Simply providing the actual typedef statements should be sufficient.
3. If you do not understand what is meant by "Julian day", then that could confuse you.

One possible meaning is what the US military and other agencies and organizations use. What that is is the "ordinal date", the day-of-year of a given calendar date within one calendar year. So 04 January would be 4 (or 004) and 28 Feb would be 059. For calendar dates after that, it would depend on whether or not it's a leap year.

That is not what this function deals with.

Another possibility would be the Julian Calendar established by Julius Caesar in 46 BCE. It was the Julian Calendar that, after some reforms, established the twelve months and their lengths as we use now, as well as the basic leap-year rule that it's every fourth year. That leap-year rule introduced an error of adding three extra days every 400 years; the Gregorian Calendar was meant to remedy that error (not a leap year if divisible by 100, except if divisible by 400).

That is not what this function deals with.

In 1583, historian Joseph Scaliger devised the "Julian Period" which is 7980 years long and started 4713 BCE. Its purpose was to allow him to synchronize different cultures' histories, each using different year-numbering systems (many even being based on how many years into the current king's reign). In Scaliger's usage, a Julian Date was the number of years since 4713 BCE, but in 1849 Herschel changed it to count the days since the start of the Julian Period, since knowing the number of days that have passed from one date to another is very useful in astronomical calculations. For that reason, it is still in use by astronomers, though it's common to use variants of the system, such as the Modified Julian Date (MJD), which is the number of days since midnight on 1858 November 17, when the Julian Date was at a nice round value of 2400000.5. When I worked on a VAX-11 minicomputer, its system clock was the number of hundreds of nanoseconds since midnight 1858 November 17, the start of the MJD system.

This is what that function is dealing with! More specifically, it deals with the standard Julian Date which started in 4713 BCE, though it modifies it to start at JD 1721119, which it gives as 1 BCE March 02. And, needless to say, in order to use that function you would need to know the Julian Date for the day whose Gregorian Date you are trying to find.

Read the Wikipedia article, Julian Day, for more information. At the bottom of that article it describes methods for converting from one system to another.
Last edited by dwise1_aol; June 6th, 2013 at 02:41 PM.