-2

Lot and lot of thanks in advance !

The following code snippet is a function that gives me Year, Month and Day for a given Julian Day..

Can you please tell me what does the constants here signify. I can find this code all over the net but nobody explains about the default values taken here. Also if anyone can explain what the function will do.

Suppose, the value i am passing for JD is 2456447..

VOID GetGregorianDate(LONG JD, PWORD Year, PWORD Month, PWORD Day)
{
    LONG j, y, d, m;
    j = JD - 1721119;            //what is this value - 1721119 (may be related to day.. but how ?)
    y = (4 * j - 1) / 146097;    //what is this value - 146097 (may be related to year.. but how ?)
    j = 4 * j - 1 - 146097 * y;
    d = j / 4;
    j = (4 * d + 3) / 1461;        // ?
    d = 4 * d + 3 - 1461 * j;
    d = (d + 4) / 4;
    m = (5 * d - 3) / 153;        // ?
    d = 5 * d - 3 - 153 * m;
    d = (d + 5) / 5;
    y = 100 * y + j;
    if (m < 10)
    {
        m = m + 3;
    }
    else 
    {
        m = m - 9;
        y = y + 1;
    }

    *Year   = (WORD) y;
    *Month  = (WORD) m;
    *Day    = (WORD) d;
}
jamylak
  • 128,818
  • 30
  • 231
  • 230
  • Take a look at the answers to [This question on Julian dates in C#](http://stackoverflow.com/questions/5248827/convert-datetime-to-julian-date-in-c-sharp-tooadate-safe), 1721119 is the Julian Day for March 2nd 1BC. – The Forest And The Trees Jun 03 '13 at 12:15
  • how the said date for value 1721119 is calculated so.. what are other values.. pls pls help.. – AjayVishwakarma_AV Jun 03 '13 at 12:25
  • You people that closed the question are not nice :( I loved the question! (I was googling around madly trying to figure the same thing, specially because the code I came across I wasn't sure it was even related to date, it was just a random jumble of constants and variables named "v1, v2, v3") – speeder Jan 07 '16 at 21:18

1 Answers1

3

They're just artifacts of the Gregorian calendar and the arbitrary date chosen as the start of the Julian epoch.

  • 1721119 is the offset from JD 0 to the start of March 1BC (since there was no 0AD); subtracting this gives the number of days since then. March is chosen as the "start" of a year so that leap days come at the "end", which makes the arithmetic simpler.
  • 146097 is the number of days in four centuries, which is the time it takes for the cycle of leap years to repeat.
  • 1461 is the number of days in four years (the short leap-year cycle).
  • 153 is the number of days in 5 consecutive months alternating between 31 and 30 days (either Mar-Jul or Aug-Dec).
  • The various additions and subtractions of 3 and 9 are to restore the "start" of the year to January.

The fiddly arithmetic puts them all together to give the correct count of days, taking into account leap years and varying month lengths.

This paper (referenced from Wikipedia) describes how the various magic numbers in the inverse calculation (Gregorian date to Julian day) arise; the numbers in your algorithm arise in a similar way.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Hey Mike, Thanks a lot. I struggled a lot for this help.. Cheers.. please keep in touch, I may need some more help... thanks again – AjayVishwakarma_AV Jun 03 '13 at 12:53
  • Hi Mike :) I have almost understood the code. Please tell me few things: 1) j = JD - 1721119; // ? => here j will be 735328 days.. whats it actually. 2) y = (4 * j - 1) / 146097; // ? => here y=20. i think its 20th century. And Specifically what is "(4 * j - 1)" 3) j = 4 * j - 1 - 146097 * y; //finally goin on here.. I read the post.. great help... thanks again.. eagerly waiting 4 ur reply.. – AjayVishwakarma_AV Jun 03 '13 at 15:29
  • @AjayVishwakarma_AV You're moving the goalposts. You asked about the magic numbers, you got your answer, you should accept it. SO is a QA site, not a site where you recruit helpers who should "keep in touch" - please don't try to get whomever's attention you managed to grab to walk you through the rest of what you're doing. If you're facing another problem, do your own research, then post a new specific question, instead of an unreadable comment with a vague "what's going on here". – millimoose Jun 03 '13 at 15:34
  • @AjayVishwakarma_AV: As I said, `j` is initially the count of days since March, 1BC. The rest is a bit fiddly, but quite simple to follow through on paper; as you say, `y` is initially 20 for 20 centuries; later on, it's multiplied by 100, and the remaining years are added (accounting for leap days); and `m` and `d` are calculated based on two repeating sets of 5 months followed by Jan and Feb, then adjusted to make the year start in Jan. – Mike Seymour Jun 03 '13 at 17:21