#P7075. Julian Day to Gregorian Date Conversion

    ID: 20281 Type: Default 1000ms 256MiB

Julian Day to Gregorian Date Conversion

Julian Day to Gregorian Date Conversion

Astronomers often use the Julian day to express time. The Julian day is defined as the number of days (possibly with a fractional part) elapsed since noon, January 1, 4713 BC in the proleptic Julian calendar. In this problem, you are given an integer Julian day number (which represents exactly noon of some day) and are asked to compute the corresponding Gregorian calendar date.

The modern Gregorian calendar (introduced in 1582 by Pope Gregory XIII) is defined by the following rules:

  1. For dates on or after October 15, 1582: Use the Gregorian calendar where the months have days: January (31), February (28 or 29 in a leap year), March (31), April (30), May (31), June (30), July (31), August (31), September (30), October (31), November (30), and December (31). A year is a leap year if it is divisible by 400, or if it is divisible by 4 but not by 100.
  2. Dates from October 5 to October 14, 1582 (inclusive) do not exist (they were skipped).
  3. For dates before October 4, 1582 (inclusive): Use the Julian calendar. The month lengths are the same as in the Gregorian calendar, but every year divisible by 4 is considered a leap year.
  4. Note that there is no year 0. That is, the year immediately before AD 1 is 1 BC. (Hence, years 1 BC, 5 BC, 9 BC, 13 BC, etc. are leap years under these rules.)

Given a Julian day number (with no fractional part), which always corresponds to a noon time, output the corresponding Gregorian date in the format:

year month day (separated by single spaces). For example, the Julian day corresponding to October 15, 1582 is output as 1582 10 15.

Note on BCE dates: If the computed year is less than or equal to 0, remember that there is no year 0. In such cases, adjust the year by subtracting 1 (e.g. if the algorithm computes year 0, output -1 to denote 1 BC).

The formulas used in converting a Julian day number to a calendar date differ depending on which calendar is in effect. A commonly used threshold is the Julian day 2299161, which corresponds to noon on October 15, 1582. For Julian day numbers greater than or equal to 2299161, use the Gregorian conversion algorithm; otherwise use the Julian conversion algorithm.

The formulas in LaTeX format are as follows:

For the Gregorian calendar (when JD \(\ge 2299161\)): \[ \begin{aligned} L &= JD + 68569,\\ n &= \left\lfloor \frac{4L}{146097} \right\rfloor,\\ L &= L - \left\lfloor \frac{146097 \cdot n + 3}{4} \right\rfloor,\\ i &= \left\lfloor \frac{4000(L+1)}{1461001} \right\rfloor,\\ L &= L - \left\lfloor \frac{1461\cdot i}{4} \right\rfloor + 31,\\ j &= \left\lfloor \frac{80L}{2447} \right\rfloor,\\ day &= L - \left\lfloor \frac{2447\cdot j}{80} \right\rfloor,\\ L &= \left\lfloor \frac{j}{11} \right\rfloor,\\ month &= j + 2 - 12L,\\ year &= 100(n - 49) + i + L. \end{aligned} \]

For the Julian calendar (when JD ( < 2299161)): [ \begin{aligned} j &= JD + 32082,\ d &= \left\lfloor \frac{4j+3}{1461} \right\rfloor,\ e &= j - \left\lfloor \frac{1461\cdot d}{4} \right\rfloor,\ m &= \left\lfloor \frac{5e+2}{153} \right\rfloor,\ day &= e - \left\lfloor \frac{153\cdot m+2}{5} \right\rfloor + 1,\ month &= m + 3 - 12\left\lfloor \frac{m}{10} \right\rfloor,\ year &= d - 4800 + \left\lfloor \frac{m}{10} \right\rfloor. \end{aligned} ]

inputFormat

The input consists of a single line containing one integer JD (the Julian day number, with no fractional part). This integer represents the noon time of a day.

outputFormat

Output the corresponding Gregorian calendar date in a single line as three space-separated integers: year month day. If the computed year is less than or equal to 0, adjust it by subtracting 1 because there is no year 0.

sample

2299161
1582 10 15