Algorithm Search⌘K

Proofs: Julian day number

These are the human proofs of Julian day number, at full length.

The Julian day number conversions are inverse bijections full

This proof shows: correctness.

Claim. Let G\mathcal{G} be the set of valid dates (Y,M,D)(Y, M, D) of the proleptic Gregorian calendar, with Y∈ZY \in \mathbb{Z}, 1≤M≤121 \le M \le 12 and 1≤D≤ℓ(Y,M)1 \le D \le \ell(Y, M), the length of month MM in year YY. Let succ⁡\operatorname{succ} be the map that sends each date to the next day. Then:

  1. JDN-FROM-DATE (lines L1 to L4) satisfies J(succ⁡(Y,M,D))=J(Y,M,D)+1J(\operatorname{succ}(Y, M, D)) = J(Y, M, D) + 1 for each date, and J(−4713,11,24)=0J(-4713, 11, 24) = 0. So it numbers the days consecutively, with day 00 on 24 November −4713-4713, and it is a bijection from G\mathcal{G} to Z\mathbb{Z}.
  2. DATE-FROM-JDN (lines L5 to L14) returns, for each integer JJ, the date in G\mathcal{G} whose number is JJ.

Throughout, ⌊⋅⌋\lfloor \cdot \rfloor is floor division, defined for all integers.

A floor lemma. For integers xx, aa and q≥1q \ge 1: ⌊x/q⌋≤a  ⟺  x≤qa+q−1\lfloor x/q \rfloor \le a \iff x \le qa + q - 1. Indeed ⌊x/q⌋≤a  ⟺  x/q<a+1  ⟺  x<q(a+1)  ⟺  x≤qa+q−1\lfloor x/q \rfloor \le a \iff x/q < a + 1 \iff x < q(a + 1) \iff x \le qa + q - 1. Consequently, for an integer p≥1p \ge 1 and an integer tt, the largest integer bb with ⌊pb/q⌋≤t\lfloor pb/q \rfloor \le t is ⌊(qt+q−1)/p⌋\lfloor (qt + q - 1)/p \rfloor, because ⌊pb/q⌋≤t  ⟺  pb≤qt+q−1  ⟺  b≤(qt+q−1)/p\lfloor pb/q \rfloor \le t \iff pb \le qt + q - 1 \iff b \le (qt + q - 1)/p.

March-based coordinates. For a date (Y,M,D)(Y, M, D), lines L1 to L3 compute a=⌊(14−M)/12⌋a = \lfloor (14 - M)/12 \rfloor, which is 11 for M∈{1,2}M \in \{1, 2\} and 00 for 3≤M≤123 \le M \le 12; then y=Y+4800−ay = Y + 4800 - a and m=M+12a−3m = M + 12a - 3, so m=M−3∈{0,…,9}m = M - 3 \in \{0, \ldots, 9\} for March to December and m=M+9∈{10,11}m = M + 9 \in \{10, 11\} for January and February. The map (Y,M)↦(y,m)(Y, M) \mapsto (y, m) is a bijection from Z×{1,…,12}\mathbb{Z} \times \{1, \ldots, 12\} to Z×{0,…,11}\mathbb{Z} \times \{0, \ldots, 11\}, with inverse M=m+3−12⌊m/10⌋M = m + 3 - 12\lfloor m/10 \rfloor and Y=y−4800+⌊m/10⌋Y = y - 4800 + \lfloor m/10 \rfloor, since ⌊m/10⌋=a\lfloor m/10 \rfloor = a. Call the twelve months with the same yy the March-year yy; it runs from 1 March of civil year y−4800y - 4800 to the last day of February of civil year y−4799y - 4799.

Month offsets. Let f(m)=⌊(153m+2)/5⌋f(m) = \lfloor (153m + 2)/5 \rfloor. Direct evaluation gives f(0),…,f(11)=0,31,61,92,122,153,184,214,245,275,306,337f(0), \ldots, f(11) = 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337, and f(12)=367f(12) = 367. The differences f(m+1)−f(m)f(m + 1) - f(m) for m=0,…,10m = 0, \ldots, 10 are 31,30,31,30,31,31,30,31,30,31,3131, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, the lengths of March to January. So f(m)f(m) is the number of days from 1 March to the first day of month mm of the same March-year, for 0≤m≤110 \le m \le 11.

Year lengths. The March-year yy ends with February of civil year y−4799y - 4799, so it has 366366 days if that year is a leap year and 365365 otherwise. Since 4800=12⋅4004800 = 12 \cdot 400, the civil year y−4799y - 4799 satisfies the leap rule (divisible by 44, and not by 100100 unless by 400400) if and only if y+1y + 1 does. Let χ(z)=1\chi(z) = 1 if zz satisfies the leap rule and 00 otherwise. Let L(y)=⌊y/4⌋−⌊y/100⌋+⌊y/400⌋L(y) = \lfloor y/4 \rfloor - \lfloor y/100 \rfloor + \lfloor y/400 \rfloor. For each q≥1q \ge 1, ⌊z/q⌋−⌊(z−1)/q⌋\lfloor z/q \rfloor - \lfloor (z-1)/q \rfloor is 11 if q∣zq \mid z and 00 otherwise. So L(z)−L(z−1)=[4∣z]−[100∣z]+[400∣z]=χ(z)L(z) - L(z - 1) = [4 \mid z] - [100 \mid z] + [400 \mid z] = \chi(z), by cases on the divisibility of zz. Let N(y)=365y+L(y)N(y) = 365y + L(y). Then for every integer yy,

N(y+1)−N(y)=365+χ(y+1)=the length of March-year y.N(y + 1) - N(y) = 365 + \chi(y + 1) = \text{the length of March-year } y.

So N(y)N(y) is the number of days from 1 March of March-year 00 to 1 March of March-year yy (negative for y<0y < 0), with N(0)=0N(0) = 0.

Part 1. Line L4 returns J=D−1+f(m)+N(y)−32044J = D - 1 + f(m) + N(y) - 32044. By the last two paragraphs, D−1+f(m)+N(y)D - 1 + f(m) + N(y) is the number of days from 1 March of March-year 00 (1 March −4800-4800) to the given date. Each step to the next day raises this count by one: within a month DD grows by one; from the last day of month m<11m < 11 to the first of month m+1m + 1, the term D−1+f(m)D - 1 + f(m) goes from f(m+1)−f(m)−1+f(m)f(m + 1) - f(m) - 1 + f(m) to f(m+1)f(m + 1); from the last day of month 1111 (which has N(y+1)−N(y)−f(11)N(y + 1) - N(y) - f(11) days) to 1 March of year y+1y + 1 the count goes from N(y+1)−1N(y + 1) - 1 to N(y+1)N(y + 1). So J(succ⁡(x))=J(x)+1J(\operatorname{succ}(x)) = J(x) + 1. For 24 November −4713-4713: a=0a = 0, y=87y = 87, m=8m = 8, and J=24+245+31755+21−0+0−32045=0J = 24 + 245 + 31755 + 21 - 0 + 0 - 32045 = 0. A map from G\mathcal{G} to Z\mathbb{Z} that sends one date to 00 and each successor to the next integer is a bijection, since succ⁡\operatorname{succ} is a bijection of G\mathcal{G} and every date is reached from 24 November −4713-4713 by a finite number of steps forward or backward.

Part 2. Let JJ be an integer and let (Y∗,M∗,D∗)(Y^*, M^*, D^*) be its date from Part 1, with March-based coordinates (y∗,m∗)(y^*, m^*) and e∗=D∗−1+f(m∗)e^* = D^* - 1 + f(m^*), so that a=J+32044=N(y∗)+e∗a = J + 32044 = N(y^*) + e^* by Part 1 and line L5, with 0≤e∗<N(y∗+1)−N(y∗)0 \le e^* < N(y^* + 1) - N(y^*).

Centuries (lines L6 and L7). For an integer bb, N(100b)=36500b+25b−b+⌊b/4⌋=36524b+⌊b/4⌋=⌊146097b/4⌋N(100b) = 36500b + 25b - b + \lfloor b/4 \rfloor = 36524b + \lfloor b/4 \rfloor = \lfloor 146097b/4 \rfloor. The function NN is strictly increasing, so the largest bb with N(100b)≤aN(100b) \le a is the one with 100b≤y∗<100(b+1)100b \le y^* < 100(b + 1), that is, b=⌊y∗/100⌋b = \lfloor y^*/100 \rfloor. By the floor lemma with p=146097p = 146097 and q=4q = 4, this bb is ⌊(4a+3)/146097⌋\lfloor (4a + 3)/146097 \rfloor, which is line L6. Line L7 sets c=a−N(100b)c = a - N(100b), the number of days from the start of March-year 100b100b to the date, so c≥0c \ge 0.

Years (lines L8 and L9). Let d∗=y∗−100bd^* = y^* - 100b, so 0≤d∗≤990 \le d^* \le 99. For 0≤d≤990 \le d \le 99, the March-years 100b,…,100b+d−1100b, \ldots, 100b + d - 1 have leap days exactly when χ(z)=1\chi(z) = 1 for z=100b+1,…,100b+dz = 100b + 1, \ldots, 100b + d. None of these zz is a multiple of 100100, so χ(z)=[4∣z]\chi(z) = [4 \mid z], and their number is ⌊d/4⌋\lfloor d/4 \rfloor because 100b100b is a multiple of 44. Hence N(100b+d)−N(100b)=365d+⌊d/4⌋=⌊1461d/4⌋N(100b + d) - N(100b) = 365d + \lfloor d/4 \rfloor = \lfloor 1461d/4 \rfloor for 0≤d≤990 \le d \le 99. Let g(d)=⌊1461d/4⌋g(d) = \lfloor 1461d/4 \rfloor; it is strictly increasing in dd. We have g(d∗)≤cg(d^*) \le c, and we claim c<g(d∗+1)c < g(d^* + 1). If d∗≤98d^* \le 98, then g(d∗+1)=N(y∗+1)−N(100b)>cg(d^* + 1) = N(y^* + 1) - N(100b) > c. If d∗=99d^* = 99, then c<N(100b+100)−N(100b)≤36525=g(100)c < N(100b + 100) - N(100b) \le 36525 = g(100). So d∗d^* is the largest dd with g(d)≤cg(d) \le c, and by the floor lemma with p=1461p = 1461 and q=4q = 4, it equals ⌊(4c+3)/1461⌋\lfloor (4c + 3)/1461 \rfloor, which is line L8. Line L9 gives e=c−g(d∗)=a−N(y∗)=e∗e = c - g(d^*) = a - N(y^*) = e^*.

Months (lines L10 and L11). Here 0≤e∗≤3650 \le e^* \le 365, and f(m∗)≤e∗<f(m∗+1)f(m^*) \le e^* < f(m^* + 1) for m∗≤10m^* \le 10, while for m∗=11m^* = 11, f(11)=337≤e∗≤365<367=f(12)f(11) = 337 \le e^* \le 365 < 367 = f(12). Since ff is strictly increasing, m∗m^* is the largest mm with f(m)≤e∗f(m) \le e^*. By the floor lemma with p=153p = 153, q=5q = 5 and the offset +2+2 (that is, f(m)≤e  ⟺  153m+2≤5e+4  ⟺  m≤(5e+2)/153f(m) \le e \iff 153m + 2 \le 5e + 4 \iff m \le (5e + 2)/153), it is ⌊(5e∗+2)/153⌋\lfloor (5e^* + 2)/153 \rfloor, which is line L10. Line L11 gives D=e∗−f(m∗)+1=D∗D = e^* - f(m^*) + 1 = D^*.

Back to the civil year (lines L12 and L13). With y∗=100b+d∗y^* = 100b + d^*, lines L12 and L13 apply the inverse of the March-based coordinates, so they return M∗M^* and Y∗=y∗−4800+⌊m∗/10⌋Y^* = y^* - 4800 + \lfloor m^*/10 \rfloor.

So DATE-FROM-JDN(J)=(Y∗,M∗,D∗)(J) = (Y^*, M^*, D^*) for every integer JJ, and the two procedures are inverse bijections between G\mathcal{G} and Z\mathbb{Z}.

The Julian calendar block. The same argument holds with χ(z)=[4∣z]\chi(z) = [4 \mid z], L(y)=⌊y/4⌋L(y) = \lfloor y/4 \rfloor and N(y)=365y+⌊y/4⌋=⌊1461y/4⌋N(y) = 365y + \lfloor y/4 \rfloor = \lfloor 1461y/4 \rfloor, without the century step. The constant −32083-32083 makes 1 January −4712-4712 day 00: a=1a = 1, y=87y = 87, m=10m = 10 and 1+306+31755+21−32083=01 + 306 + 31755 + 21 - 32083 = 0. In the inverse, c=J+32082c = J + 32082 is the count from 1 March −4800-4800 of the Julian calendar, and d=⌊(4c+3)/1461⌋d = \lfloor (4c + 3)/1461 \rfloor is the March-year, by the floor lemma. ■\blacksquare

Back to Julian day number