Collected algorithms from Mapping Time The algorithms below are taken from “Mapping Time” by E.G.Richards They have been fully corrected for typos etc. and retested. Please note that the '<=' symbols in the book have been replaced by '=' and that several lines terminate with a number which is the equation number in “Mapping Time” (e.g. 24.1) Only integer arithmetic is used throughout. i.e. All the numbers are whole numbers. Note the following: A*B represents the result of multiplying A by B A/B represents the result of dividing A by B and discarding any remainder MOD(A,B) represents the remainder obtained when A is divided by B. All the algorithms have been tested by A Checking for reversibility by: 1. Converting a range of Julian day numbers, J, spanning at least 400 years (>146100 days) to dates in each calendar, D/M/Y. 2. Converting each date, D/M/Y back to a Julian day number, JN. 3. Checking that J = JN for each day. B Checking for correctness 1. Converting a range of Julian day numbers, J, spanning at least 400 years (>146100 days) to dates in each calendar, D/M/Y. 2. Converting the date, D/M/Y, back to a Julian day number, JC using other algorithms believed to give the correct Julian day number. 3. Checking that J = JC for each day. Last alteration made April 1, 2009 ------------------------------------------------------------------------------- Algorithm A To calculate the day of the week number, W, (Sunday = 1 etc.) for a Julian date D/M/Y N.B. This algorithm is valid only for Dates in the Christian era (A.D.) 1 M' = MOD(9 + M,12) 24.2 2 Q = M'/10 3 Z = (13*M' + 2)/5 24.3 4 T = 28*M' + Z + D - 365*Q + 59 24.4 5 C = 1 + MOD(T - 1,7) 24.5 6 Y' = Y - Q 24.17 7 V = Y/4 - Y'/4 24.15 8 P = Y + Y/4 + 4 - V 24.12 9 N = 7 - MOD(P,7) 24.14 10 W = 1 + MOD(7 + C - N,7) 25.18 ------------------------------------------------------------------------------- Algorithm B To calculate the day of the week number, W, (Sunday = 1 etc.) for a Gregorian date D/M/Y N.B. This algorithm is valid only for Dates in the Christian Era (A.D.) 1 M' = MOD(9 + M,12) 24.1 2 Q = M'/10 3 Z = (13*M' + 2)/5 24.3 4 T = 28*M' + Z + D - 365*Q + 59 24.4 5 C = 1 + MOD(T - 1,7) 24.5 6 Y' = Y - Q 24.17 7 V = (Y/4 - Y'/4) - (Y/100 - Y'/100) + (Y/400 - Y'/400) 24.16 8 P = Y + Y/4 - Y/100 + Y/400 - 1 - V 24.13 9 N = 7 - MOD(P,7) 24.14 10 W = 1 + MOD(7 + C - N,7) 25.18 ------------------------------------------------------------------------------- Algorithm C To calculate the day of the week number, W, (Sunday = 1 etc.) for a Julian date D/M/Y 1 M' = MOD(9 + M,12) 2 Y' = Y - M'/10 3 W = 1 + MOD(D + (13*M' + 2)/5 + Y' + Y'/4,7) N.B. This algorithm is valid all dates before the Christian era (B.C.) provided that the year, Y, of a date X B.C. is entered as Y = 1 - X. In this case it is necessary to insert after step 2: 2a If Y' < 0 Then Y' = Y' + 28*(1 - Y'/28) ------------------------------------------------------------------------------- Algorithm D To calculate the day of the week number, W, (Sunday = 1 etc.) for a Gregorian date D/M/Y 1 M' = MOD(9 + M,12) 24.2 2 Y' = Y - M'/10 3 W = 1 + MOD(2 + D + (13*M' + 2)/5 + Y' + Y'/4 - Y'/100 + Y'/400,7) N.B. This algorithm is valid all dates before the Christian era (B.C.) provided that the year, Y, of a date X B.C. is entered as Y = 1 - X, In this case it is necessary to insert after step 2: 2a If Y' < 0 Then Y' = Y' + 400*(1 - Y'/400) ------------------------------------------------------------------------------- Algorithm E To convert a date D/M/Y to a Julian day number, J 1 Y' = Y + y - (n + m - 1 - M)/n 25.1 2 M' = MOD(M - m + n,n) 25.2 3 D' = D - 1 25.3 4 c = (p*Y' + q)/r 25.8 5 d = (s*M' + t)/u 25.9 6 J = c + d + D' - j 25.7 For Gregorian type calendars replace step 6 by: 6a g = (3*((Y' + A)/100))/4 + G 5.26 6b J = c + d + D' - j - g 25.22 For the Saka calendar insert before step 5: 4a Z = M'/6 4b s = 31 - Z 4c t = 5*Z ------------------------------------------------------------------------------- Algorithm F To convert a Julian day number, J, to a date D/M/Y in the Julian calendar 1 J' = J + j 25.10 2 Y' = (r*J' + v)/p 25.11 3 T' = MOD(r*J' + v,p)/r 25.12 4 M' = (u*T' + w)/s 25.13 5 D' = MOD(u*T' + w,s)/u 25.14 6 D = D' + 1 25.4 7 M = MOD(M' + m - 1,n) + 1 25.6 For Gregorian type calendars replace step 1 by: 1a g = (3*((4*J + B)/146097))/4 + G 25.34 1b J' = J + j + g 25.23 For the Saka calendar, insert before step 4: 3a X = T'/365 3b Z = T'/185 - X 3c s = 31 - Z 3d w = - 5*Z and replace step 5 by: 5a D' = (6*X + MOD(u*T' + w,s))/u 25.37 -------------------------------------------------------------------------------- Algorithm G To calculate the Julian day number, J, of the 1st day of Tishri in the Jewish year Y. 1 t = 31524 + 765433*((235*Y - 234)/19) 26.4 2 d = t/25920 26.5 3 t' = MOD(t,25920) 26.6 4 w = 1 + MOD(d,7) 26.7 5 E = MOD(7*Y + 13,19)/12 26.8 6 E' = MOD(7*Y + 6,19)/12 26.9 7 If t'>=19940 26.10 or t'=>9924 and w=3 and E=0 or t'=>16788 and w=2 and E=0 and E'=1 then d = d + 1 8 J = d + MOD(MOD(d+5,7),2) + 347997 26.12 & 26.13 It is very likely that the value of t calculated in step 1 will exceed the capacity of a 32-bit computer word. If this is not acceptable, steps 1, 2 and 3 should be replaced by: a m = (235*Y - 234)/19 b tc = 204 + 793*m c th = 5 + 12*m + tc/1080 d d = 1 + 29*m + th/24 e t' = MOD(tc,1080) + 1080*MOD(th,24) -------------------------------------------------------------------------------- Algorithm H To calculate the Jewish year, Y, in which Julian day number J falls 1 M = (25920*(J - 347996))/765433 26.14 2 Y = 19*(M/235) + (19*MOD(M,235) - 2)/235 + 1 26.15 3 Calculate the day number of Tishri 1st, J', for year Y = using algorithm G 4 If (J' > J) then Y = Y - 1 It is very likely that the value of k(J-347996) calculated in step 1 will exceed the capacity of a 32-bit computer word. If this is not acceptable, step 1 should be replaced by: 1a M = [(J - 347996)*0.03386318] + 1 Here [...] indicates that the integral part only of the expression should be retained. Make sure you use all 8 places after the decimal point. -------------------------------------------------------------------------------- Algorithm I To calculate the date in the Jewish calendar, D/M/Y which corresponds to Julian day number J. 1 Calculate the Jewish year, Y, in which J falls using algorithm H 2 Calculate J', day number of Tishri 1st in year Y (1/1/Y) using algorithm G and the corresponding value of K using algorithm J 3 d = J - J' + 1 26.18 4 Find from row K in Table 26.2, the highest value of M such that d > A(K,M) 5 D = d - A(K,M) 26.19 Programmers may care to replace step 4 by the following fragment of (FORTRAN) code: DO 100 I=1,14 IF (A(I,K).GE.d) THEN M=I-1 GOTO 110 END IF 100 CONTINUE 110 CONTINUE If they do, Table 26.2 should be extended to give dummy values corresponding to a nominal month 14. The values of A(13,K) for K = 1,2,3 and A(14,K) for all K should be set to a high value, 999 ------------------------------------------------------------------------------- Algorithm J To calculate the Julian day number, J, which corresponds to a Jewish date D/M/Y 1 Calculate the Julian day number, J, of the first day of Tishri for year Y using algorithm G 2 Calculate the Julian day number, J', of the first day of Tishri for year Y+1 using algorithm G 3 K = J' - J - 352 - 27*(MOD(7*Y + 13,19)/12) 26.17 4 Look up the value of A(K,M) from Table 26.2 5 J = J + A(K,M) + D - 1 26.20 -------------------------------------------------------------------------------- Algorithm K To calculate a Mayan calendar round date (specified by A,B,C,D,Y) corresponding to a Julian day number, J. 1 L = J - 584285 27.30 2 Y = L/18980 27.31 3 R = MOD(L,18980) 27.32 4 A = 1 + MOD(R + 3,13) 27.33 5 B = 1 + MOD(R + 19,20) 27.34 6 N = MOD(R + 348,365) 27.35 7 C = MOD(N,20) 27.16 8 D = N/20 27.17 ------------------------------------------------------------------------------- Algorithm L To calculate a Julian day number, J, corresponding to a Mayan calendar round date specified by A,B,C,D,Y (Y is the cycle number) 1 M = 13*MOD(60 + 3*(A-B),20) + A - 1 27.13 2 M = MOD(M + 101,260) 27.39 3 N = 20*D + C 27.18 4 N = MOD(N + 17,365) 27.38 5 R = 365*MOD(364 + M - N,52) + N 27.29 6 J = 584285 + 18980*Y + R 27.36 -------------------------------------------------------------------------------- Algorithm X An extra algorithm to calculate a Julian day number corresponding to a Mayan long count date specified by D,U,T,K,B,P J = 584285 + ((((P*20 + B)*20 + K)*20 + T)*18 + U)*20 + D -------------------------------------------------------------------------------- Algorithm Y An extra algorithm to calculate a Mayan long count date (specified by P,B,K,T,D) corresponding to a Julian day number, J. 1 X = J - 584285 2 D = MOD(X,20) !kins 3 X = X/20 4 U = MOD(X,18) !uinals 5 X = X/18 6 T = MOD(X,20) !tuns 7 X = X/20 8 K = MOD(X,20) !katuns 9 X = X/20 10 B = MOD(X,20) !baktuns 11 X = X/20 12 P = MOD(X,20) !pictuns Programmers may care to use in place of steps 2 to 12, the following ragments of (FORTRAN) code: DATA JFACT/20,18,20,20,20,20/ DO 100 I=1,6 JF=JFACT(I) LCNT(I) = MOD(X,JF) X = K/JF 100 CONTINUE where the array LCNT will receive the values of D,U,T,K,B,T and the array JFACT contains 20,18,20,20,20,20 ------------------------------------------------------------------------------- Algorithm M To calculate the 'day of March' of Easter Sunday for year Y according to the Dionysian canon (long form) 1 P = Y + Y/4 + 4 24.12 2 N = 7 - MOD(P,7) 24.14 3 G = 1 + MOD(Y,19) 29.1 4 E = MOD(11*G - 3,30) 29.2 5 R = 22 + MOD(53 - E,30) 29.3 6 C = 1 + MOD(R + 2,7) 29.4 7 S = R + MOD(7 + N - C,7) 29.6 ------------------------------------------------------------------------------ Algorithm N To calculate the 'day of March' of Easter Sunday for year Y according to the Dionysian canon (short form) 1 A = MOD(Y,19) 2 B = 22 + MOD(225 - 11*A,30) 3 S = B + MOD(56 + 6*Y - Y/4 - B,7) ------------------------------------------------------------------------------ Algorithm O To calculate the 'day of March' of Easter Sunday for year Y according to the Gregorian canon (long form) 1 P = Y + Y/4 - Y/100 + Y/400 - 1 24.13 2 N = 7 - MOD(P,7) 24.14 3 H = Y/100 4 Q = H - H/4 29.8 5 G = 1 + MOD(Y,19) 29.1 6 E = MOD(57 + 11*G - Q + (H - (H - 17)/25)/3,30) 9.12 7 U = MOD(53 - E,30) 29.14 8 V' = (G - 1 + 11*U)/319 29.15 9 R = 22 + U - V' 10 C = 1 + MOD(R + 2,7) 29.4 11 S = R + MOD(7 + N - C,7) 29.6 ------------------------------------------------------------------------------ Algorithm P To calculate the 'day of March' of Easter Sunday for year Y according to the Gregorian canon (short form) 1 A = Y/100 2 B = A - A/4 3 C = MOD(Y,19) 4 D = MOD(15 + 19*C + B - (A - (A - 17)/25)/3,30) 5 E = D - (C+11*D)/319 6 S = 22 + E + MOD(140004 - Y - Y/4 + B - E,7) ------------------------------------------------------------------------------ Algorithm Q To calculate day, D, and month, M (March=3, April=4) of Easter Sunday iven the 'Day of March', S. 1 M = 3 + S/32 2 D = 1 + MOD(S-1,31) ------------------------------------------------------------------------------- Table 25.1 for use with Algorithms E and F # y j m n r p q v u s t w --------------------------------------------------------------- 1 3968, 47, 1, 13, 1, 365, 0, 0, 1, 30, 0, 0, 2 5268, 317, 1, 13, 1, 365, 0, 0, 1, 30, 0, 0, 3 5348, 317, 1, 13, 1, 365, 0, 0, 1, 30, 0, 0, 4 5348, 77, 10, 13, 1, 365, 0, 0, 1, 30, 0, 0, 5 4720, 124, 1, 13, 4, 1461, 0, 3, 1, 30, 0, 0, 6 4996, 124, 1, 13, 4, 1461, 0, 3, 1, 30, 0, 0, 7 6504, 111, 1, 13, 4, 1461, 0, 3, 1, 30, 0, 0, 8 4405, 1401, 7, 12, 4, 1461, 0, 3, 5, 153, 2, 2, 9 4405, 1401, 6, 12, 4, 1461, 0, 3, 5, 153, 2, 2, A 4716, 1401, 3, 12, 4, 1461, 0, 3, 5, 153, 2, 2, B 4716, 1401, 3, 12, 4, 1461, 0, 3, 5, 153, 2, 2, C 5519, 7665, 1, 12, 30, 10631, 14, 15,100, 2951, 51, 10, D 5519, 7664, 1, 12, 30, 10631, 14, 15,100, 2951, 51, 10, E 6560, 1412, 20, 20, 4, 1461, 0, 3, 1, 19, 0, 0, F 4794, 1348, 2, 12, 4, 1461, 0, 3, 1, 31, 0, 0 -------------------------------------------------------------------------------- Table 25.4 (part of) for use with Algorithms E and F # A B G ------------------------- 1 0, 0, 0, 2 0, 0, 0, 3 0, 0, 0, 4 0, 0, 0, 5 0, 0, 0, 6 0, 0, 0, 7 396, 578797, -51, 8 0, 0, 0, 9 0, 0, 0, A 0, 0, 0, B 184, 274277, -38, C 0, 0, 0, D 0, 0, 0, E 184, 274273, -50, F 184, 274073, -36 ------------------------------------------------------------------------------- Table 26.2 for use with algorithms I and J K M=1 2 3 4 5 6 7 8 9 10 11 12 13 ---------------------------------------------------- 1 0,30,59,88,117,147,176,206,235,265,294,324, -, 2 0,30,59,89,118,148,177,207,236,266,295,325, -, 3 0,30,60,90,119,149,178,208,237,267,296,326, -, 4 0,30,59,88,117,147,177,206,236,265,295,324,354, 5 0,30,59,89,118,148,178,207,237,266,296,325,355, 6 0,30,60,90,119,149,179,208,238,267,297,326,356 ------------------------------------------------------------------------------