/* Calendar (Rosetta code) in Picat. """ Create a routine that will generate a text calendar for any year. Test the calendar by generating a calendar for the year 1969, on a device of the time. Choose one of the following devices: A line printer with a width of 132 characters. An IBM 3278 model 4 terminal (80×43 display with accented characters). Target formatting the months of the year to fit nicely across the 80 character width screen. Restrict number of lines in test output to 43. (Ideally, the program will generate well-formatted calendars for any page width from 20 characters up.) Kudos (κῦδος) for routines that also correctly transition from Julian to Gregorian calendar in September 1752. This task is inspired by Real Programmers Don't Use PASCAL by Ed Post, Datamation, volume 29 number 7, July 1983. THE REAL PROGRAMMER'S NATURAL HABITAT "Taped to the wall is a line-printer Snoopy calender for the year 1969." For further Kudos see task CALENDAR, where all code is to be in UPPERCASE. For economy of size, do not actually include Snoopy generation in either the code or the output, instead just output a place-holder. """ Note: This program just print the months in one column. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go => Names = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"], Year = 2019, foreach(Month in 1..12) println(center(to_fstring("%w %d\n", Names[Month], Year),28)), println(calendar(Year,Month)), if Month mod 3 == 0 then nl end, nl end, nl. go2 => Names = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"], YearFrom = 2010, YearTo = 2020, foreach(Year in YearFrom..YearTo) foreach(Month in 1..12) println(center(to_fstring("%w %d\n", Names[Month], Year),28)), println(calendar(Year,Month)), if Month mod 3 == 0 then nl end, nl end end, nl. calendar(Year,Month) = Cal => Cal = " Sun Mon Tue Wed Thu Fri Sat\n", DOW = dow(Year,Month, 1), Cal := Cal ++ [" " : _ in 1..DOW].join(''), foreach(Day in 1..max_days_in_month(Year,Month)) Cal := Cal ++ to_fstring("%4w", Day), if (DOW + Day ) mod 7 == 0 then Cal := Cal ++ "\n" end end. % % Day of week, Sakamoto's method % http://en.wikipedia.org/wiki/Weekday_determination#Sakamoto.27s_Method % dow(Y, M, D) = R => T = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4], if M < 3 then Y := Y - 1 end, R = (Y + Y // 4 - Y // 100 + Y // 400 + T[M] + D) mod 7. leap_year(Year) => (Year mod 4 == 0, Year mod 100 != 0) ; Year mod 400 == 0. % % http://en.wikipedia.org/wiki/Julian_day % gregorian2julian % g2j(Year,Month,Day) = JD => A = floor((14-Month) / 12), % 1 for Jan or Feb, 0 for other months Y = Year + 4800 - A, M = Month + 12*A - 3, % 0 for Mars, 11 for Feb JD = Day + floor( (153*M + 2) / 5) + 365*Y + floor(Y/4) - floor(Y / 100) + floor(Y / 400) - 32045. % % julian2gregorian % j2g(JD) = Date => Y=4716, V=3, J=1401, U=5, M=2, S=153, N=12, W=2, R=4, B=274277, P=1461, C= -38, F = JD + J + (((4 * JD + B) div 146097) * 3) div 4 + C, E = R * F + V, G = mod(E, P) div R, H = U * G + W, Day = (mod(H, S)) div U + 1, Month = mod(H div S + M, N) + 1, Year = (E div P) - Y + (N + M - Month) div N, Date = [Year,Month,Day]. % for the alternative approach max_days_in_month(Year,Month) = Days => if member(Month, [1,3,5,7,8,10,12]) then Days = 31 elseif member(Month,[4,6,9,11]) then Days = 30 else if leap_year(Year) then Days = 29 else Days = 28 end end. % * string.center % center(s, width[, fillchar]) -> string % Return a center version of s, in a field of the specified % width. padded with spaces as needed. The string is never % truncated. If specified the fillchar is used instead of spaces. center(S,Width) = Center => Len = S.length, if Len > Width then Center = S else Diff = (Width - Len), W = Diff div 2, SpaceLeft = rep_string(' ',W), SpaceRight = cond(Diff mod 2 = 1, SpaceLeft ++ [' '],SpaceLeft), Center = SpaceLeft ++ S ++ SpaceRight end. rep_string(S,N) = [S : _ in 1..N].