The fixed-date conversions of the Gregorian calendar are inverse bijections full
This proof shows: correctness.
Claim. Let G be the set of valid proleptic Gregorian dates (y,m,d) with y∈Z. FIXED-FROM-GREGORIAN numbers the days of G consecutively with 1 on 1 January of year 1, so it is a bijection from G to Z. GREGORIAN-FROM-FIXED is its inverse, and DAY-OF-WEEK(r) is 0 for Sunday through 6 for Saturday.
Throughout, ⌊⋅⌋ is floor division and xmodq=x−q⌊x/q⌋∈{0,…,q−1}, defined for all integers x.
Notation. Let χ(z)=1 if 4∣z and (100∤z or 400∣z), and χ(z)=0 otherwise; this is LEAP (line L1). Year z has 365+χ(z) days. Let L(x)=⌊x/4⌋−⌊x/100⌋+⌊x/400⌋ and F(x)=365x+L(x). For q≥1, ⌊z/q⌋−⌊(z−1)/q⌋=[q∣z], so L(z)−L(z−1)=[4∣z]−[100∣z]+[400∣z]=χ(z) by cases, and
F(z)−F(z−1)=365+χ(z). With F(0)=0, F(x) is the number of days in the years 1 to x for x≥0, and minus the number of days in the years x+1 to 0 for x<0. In both cases, the days before 1 January of year y, counted from 1 January of year 1, number F(y−1).
Month offsets. Let g(m)=⌊(367m−362)/12⌋. Direct evaluation gives g(1),…,g(13)=0,31,61,92,122,153,183,214,245,275,306,336,367. The differences g(m+1)−g(m) for m=1,…,12 are 31,30,31,30,31,30,31,31,30,31,30,31: the true month lengths, except 30 for February. For year y, let cy(m)=0 for m≤2 and cy(m)=−2+χ(y) for m≥3 (line L2). Then hy(m)=g(m)+cy(m) is the number of days in year y before month m, because February has 28+χ(y) days, which is 30−(2−χ(y)).
Forward (lines L2 and L3). Line L3 returns F(y−1)+hy(m)+d, which is the number of days from 1 January of year 1 to the date, plus 1. So two consecutive days get consecutive numbers, and 1 January of year 1 gets F(0)+0+1=1. Every date is reached from 1 January of year 1 by finitely many steps forward or backward, so the map is a bijection from G to Z.
Mixed-radix form of F. Every integer x has a unique form x=400a+100b+4c+e with a∈Z, 0≤b≤3, 0≤c≤24 and 0≤e≤3. Then ⌊x/4⌋=100a+25b+c, ⌊x/100⌋=4a+b and ⌊x/400⌋=a, because e≤3, 4c+e≤99 and 100b+4c+e≤399. So L(x)=97a+24b+c and
F(x)=146097a+36524b+1461c+365e. Also x+1 is a leap year exactly when e=3 and (c=24 or b=3), since x+1=400a+100b+4(c+1) is then a multiple of 4, of 100 exactly when c=24, and of 400 exactly when c=24 and b=3.
Backward, the year (lines L4 to L10). Let r be an integer with date (y∗,m∗,d∗). By the forward part, r=F(y∗−1)+hy∗(m∗)+d∗. Let x∗=y∗−1=400a+100b+4c+e in the form above, and t=hy∗(m∗)+d∗−1, the day of the year counted from 0, so 0≤t≤364+χ(y∗). Line L4 gives d0=r−1=F(x∗)+t=146097a+36524b+1461c+365e+t.
- Line L5. The part after 146097a is at most 36524⋅3+1461⋅24+365⋅3+365=146096, so n400=a and d1=36524b+1461c+365e+t.
- Line L6. The part 1461c+365e+t is at most 35064+1095+365=36524, with equality only if c=24, e=3 and t=365. Then y∗ is a leap year, so b=3. Case A: not all of these hold. Then n100=b and d2=1461c+365e+t. Case B: b=3, c=24, e=3, t=365. Then d1=146096, n100=4 and d2=0, so lines L7 and L8 give n4=n1=0, line L9 gives y=400a+400=x∗+1=y∗, and line L10 keeps it.
- Line L7, Case A. The part 365e+t is at most 1095+365=1460<1461, so n4=c and d3=365e+t.
- Line L8, Case A. If t≤364, then n1=e≤3, line L9 gives y=x∗, and line L10 adds one: y=y∗. If t=365, then y∗ is a leap year, e=3, d3=1460 and n1=4, so line L9 gives y=x∗+1=y∗ and line L10 keeps it.
In every case the year after line L10 is y∗.
Backward, the month and the day (lines L11 to L14). Line L11 gives p=r−(F(y∗−1)+1)=t. The test of line L12 holds exactly when m∗≤2, so c=−cy∗(m∗) and p+c=g(m∗)+d∗−1. Since 0≤d∗−1 and d∗ is at most the true length of month m∗, which is at most g(m∗+1)−g(m∗), we get g(m∗)≤p+c<g(m∗+1). The function g is strictly increasing, so m∗ is the largest m with g(m)≤p+c. For integers, g(m)≤T⟺367m−362≤12T+11⟺m≤(12T+373)/367, so this largest m is ⌊(12(p+c)+373)/367⌋, which is line L13. Line L14 returns r−(F(y∗−1)+hy∗(m∗)+1)+1=d∗.
Day of the week (line L15). R.D. 1 is a Monday, and consecutive days have consecutive numbers, so R.D. r is r−1 days after a Monday and its weekday is (1+(r−1))mod7=rmod7 with 0 for Sunday, for every integer r. ■