Calendar: Difference between revisions

6,924 bytes added ,  1 month ago
no edit summary
No edit summary
 
(40 intermediate revisions by 14 users not shown)
Line 1:
[[Category:Scala examples needing attention]]
{{task|Date and time}}
Create a routine that will generate a text calendar for any year.
Line 23 ⟶ 24:
:*   [[Five weekends]]
<br><br>
 
=={{header|360 Assembly}}==
{{trans|Free Basic}}
This program uses no external functions but two ASSIST macros (XDECO, XPRNT) to keep the code as short as possible.
<langsyntaxhighlight lang="360asm">* calendar 08/06/2016
CALENDAR CSECT
USING CALENDAR,R13 base register
Line 206:
DA DS 12CL144
YREG
END CALENDAR</langsyntaxhighlight>
{{out}}
<pre>
Line 243:
31
</pre>
 
=={{header|Ada}}==
 
Line 250 ⟶ 249:
the [[Calendar_-_for_"real"_programmers#Ada]] task.
 
<langsyntaxhighlight Adalang="ada">with Ada.Calendar.Formatting;
 
package Printable_Calendar is
Line 297 ⟶ 296:
end record;
 
end Printable_Calendar;</langsyntaxhighlight>
 
We continue with the implementation (printable_calendar.ads):
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
package body Printable_Calendar is
Line 476 ⟶ 475:
end Init_132;
 
end Printable_Calendar;</langsyntaxhighlight>
 
Now, the main program is really simple:
 
<langsyntaxhighlight Adalang="ada">with Printable_Calendar;
 
procedure Cal is
Line 490 ⟶ 489:
C.New_Line;
C.Print(1969, "Nineteen-Sixty-Nine");
end Cal;</langsyntaxhighlight>
 
Here is the output:
Line 535 ⟶ 534:
 
To get a 132-character-wide output, you just have to replace "Init_80" by "Init_132" in the main program.
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<langsyntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
 
PROC print calendar = (INT year, page width)VOID: (
Line 647 ⟶ 645:
line printer width = 80; # as at 1969! #
print calendar(mankind stepped on the moon, line printer width)
)</langsyntaxhighlight>
Output:
<pre>
Line 686 ⟶ 684:
30
</pre>
 
=={{header|ALGOL W}}==
{{trans|Simula}} which is a {{trans|C}}
Line 696 ⟶ 693:
<br>In Algol W, the condition of a "while" loop can be a block and the boolean "and" operator short circuits, which allow us to avoid the goto statements.
<br>Also, the Algol W string type is fixed length, unlike the Simula text type.
<langsyntaxhighlight lang="algolw">BEGIN
INTEGER WIDTH, YEAR;
INTEGER COLS, LEAD, GAP;
Line 837 ⟶ 834:
PRINT_YEAR
END
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 879 ⟶ 876:
 
 
</pre>
 
=={{header|Amazing Hopper}}==
<p>Hopper tiene una función que genera calendarios en español, y con un formato diferente al pedido.</p>
<p>El calendario generado en un array 2D como el que se muestra a continuación:</p>
<p>Línea de código:</p>
<pre>
Let ( calendario := Calendar(1,año,12) )
[ 1:8, 1:7 ] Cget 'calendario', Print this table
</pre>
<pre>
ENE, , , , , ,1969
Dom,Lun,Mar,Mie,Jue,Vie,Sab
, , , 1, 2, 3, 4
5, 6, 7, 8, 9, 10, 11
12, 13, 14, 15, 16, 17, 18
19, 20, 21, 22, 23, 24, 25
26, 27, 28, 29, 30, 31,
, , , , , ,
</pre>
<p>Son generados tantos calendarios como se pida, y de diferentes formas (lineales o radiales). El código a continuación debe reformatear el calendario antes de imprimirlo.</p>
<syntaxhighlight lang="txt">
#include <jambo.h>
 
Main
Set stack 15
año=0
Get arg numeric '2', Move to 'año'
Set '4,1,1,1' Init 'fila, columna, contador columna, contador mes)
meses={}
Let list ( meses := "Enero","Febrero","Marzo","Abril","Mayo",\
"Junio","Julio","Agosto","Septiembre","Octubre",\
"Noviembre","Diciembre" )
calendario=0
Let ( calendario := Calendar(1,año,12) )
Cls
Link gosub 'Cambia lenguaje de los meses, Imprime año'
Tok sep ("")
Gosub while ( i=1, Less(i,97), Dibuja calendario )
End
 
Subrutines
 
Define 'Dibuja calendario'
Locate (fila, columna) Just center (23, [ contador mes++ ] Cget 'meses')
Print it
++fila
Loc row (fila--) [ {i}Plus(1):{i}Plus(7), 1:7 ] Cget 'calendario'
Print this table
++contador columna
columna += 25
When( Equals (contador columna, 4) ) {
Set '1', Copy to 'contador columna',
Move to 'columna'
fila+=9
}
i+=8
Return
 
Define 'Imprime año'
Locate (2,35), Print (año)
Return
 
Define 'Cambia lenguaje de los meses'
Let ( calendario := Tran (" Do","Dom",calendario) )
Let ( calendario := Tran (" Lu","Lun",calendario) )
Let ( calendario := Tran (" Ma","Mar",calendario) )
Let ( calendario := Tran (" Mi","Mie",calendario) )
Let ( calendario := Tran (" Ju","Jue",calendario) )
Let ( calendario := Tran (" Vi","Vie",calendario) )
Let ( calendario := Tran (" Sa","Sab",calendario) )
Return
</syntaxhighlight>
{{out}}
<pre>
$ hopper jm/calendar.jambo 1969
 
1969
 
Enero Febrero Marzo
Do Lu Ma Mi Ju Vi Sa Do Lu Ma Mi Ju Vi Sa Do Lu Ma Mi Ju Vi Sa
1 2 3 4 1 1
5 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 29
30 31
 
Abril Mayo Junio
Do Lu Ma Mi Ju Vi Sa Do Lu Ma Mi Ju Vi Sa Do Lu Ma Mi Ju Vi Sa
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30
 
Julio Agosto Septiembre
Do Lu Ma Mi Ju Vi Sa Do Lu Ma Mi Ju Vi Sa Do Lu Ma Mi Ju Vi Sa
1 2 3 4 5 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
31
 
Octubre Noviembre Diciembre
Do Lu Ma Mi Ju Vi Sa Do Lu Ma Mi Ju Vi Sa Do Lu Ma Mi Ju Vi Sa
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Calendar(Yr){
LastDay := [], Day := []
Titles =
Line 919 ⟶ 1,033:
Res:=RegExReplace(Res,"`am)(^|\s)\K0", " ")
return res
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">Gui, font,s8, COURIER
Gui, add, edit, vYr w40 r1 Limit4 Number, 1969
Gui, add, edit, vEdit2 w580 r38
Line 934 ⟶ 1,048:
GuiClose:
ExitApp
return</langsyntaxhighlight>
Outputs:<pre> 1969
 
Line 972 ⟶ 1,086:
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30 </pre>
 
=={{header|AutoIt}}==
<syntaxhighlight lang="autoit">
<lang AutoIt>
#include <Date.au3>
 
Line 1,077 ⟶ 1,190:
ConsoleWrite($sLeft & $_sLine & $_sLF & @LF)
EndFunc ;==>_PrintLine
</syntaxhighlight>
</lang>
Output
<pre>
Line 1,120 ⟶ 1,233:
</pre>
--[[User:BugFix|BugFix]] ([[User talk:BugFix|talk]]) 00:40, 15 November 2013 (UTC)
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
 
Works with Gnu awk version 3.1.5 and with BusyBox v1.20.0.git awk
Line 1,223 ⟶ 1,335:
 
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,262 ⟶ 1,374:
 
</pre>
=={{header|BASIC}}==
 
==={{header|BaCon}}===
Choosing 132 character output.
<langsyntaxhighlight lang="freebasic">DECLARE month$[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }
DECLARE month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
year$ = "1969"
Line 1,297 ⟶ 1,409:
PRINT day;
NEXT
NEXT</langsyntaxhighlight>
 
{{out}}
Line 1,321 ⟶ 1,433:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">::Calender Task from Rosetta Code Wiki
::Batch File Implementation
 
Line 1,413 ⟶ 1,525:
echo.
pause
endlocal</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,458 ⟶ 1,570:
 
Press any key to continue . . .</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
The day and month names are in the language for which the PC is configured.
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"DATELIB"
VDU 23,22,640;570;8,15,16,128
Line 1,497 ⟶ 1,608:
PRINT
NEXT
</syntaxhighlight>
</lang>
Output:
<pre> 1969
Line 1,535 ⟶ 1,646:
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30</pre>
 
=={{header|Befunge}}==
This is quite closely based on the [[Calendar#C|C]] sample, although the centering calculation has been adjusted to fix an off-by-one error, and the months have been made a constant height, regardless of how much space they require, to try and produce a more balanced layout.
Line 1,541 ⟶ 1,651:
The year is read from stdin, and the width is specified by the first value on the stack (set to 80 - <tt>"P"</tt> - in the current implementation).
 
<langsyntaxhighlight lang="befunge">"P"00p&>:::4%!\"d"%*\45*:*%!+!!65*+31p:1-:::"I"5**\4/+\"d"/-\45*:*/+1+7%:0v
J!F?M!A M!J J!A!S O!N D!SaFrThWeTuMoSuvp01:_1#!-#%:#\>#+6<v-2g1+1g01p1p01:<
January February March April >:45**00g\-\1-:v:<<6>+7%:10g2+:38*\`|
Line 1,551 ⟶ 1,661:
> > $$55+,6>>40p:10g30g>:#,1#*-#8\#4_$>\:2*:1+1g2-50p1g640g-7*1+\-7v v@,<6
2:+g01$_55+,^ > > > > #^>#g>#0>#2_v v*2!!\%+55:\/+55:**`0\!`g05:::\< >2-^^
->:> >#^>#<>#<^#!:-1g04$$ < < < < < >4+8*+\:!!2*4+8*+,,48*,1+\1-:>#^_$$1+\1</langsyntaxhighlight>
 
=={{header|C}}==
With arbitrary display width (>= 20 though) and auto spacing.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,679 ⟶ 1,788:
bail: fprintf(stderr, "bad args\nUsage: %s year [-w width (>= 20)]\n", v[0]);
exit(1);
}</langsyntaxhighlight>
 
{{libheader|Gadget}}
{{trans|Amazing Hopper}}
<syntaxhighlight lang="c">
 
#include <gadget/gadget.h>
LIB_GADGET_START
 
void draw_calendar( RDS(char*,calendario), int year );
void print_calendar( RDS(char*,calendario) );
 
Main
Assert( Arg_count ==2, fail_arg );
Get_arg_int( year, 1 );
 
ACTUAL_LANG_DATE = EN; /* text in english */
New array calendario as string;
/* get full year:
112 = code for months after initial month.
1 = initial month
year = well... */
calendario = Calendar(calendario, 112, 1, year);
draw_calendar( SDS(calendario), year );
Free str array calendario;
Exception( fail_arg ){
Msg_yellow("Modo de uso:\n ./calendar <nYear>");
}
 
End
 
void draw_calendar( RDS( char*, calendario), int year )
{
int fila=4, columna=1, cnt_columna=1, cnt_mes=0, i=0;
Cls;
At 2,35; Print "%d", year;
 
while ( cnt_mes < 12 )
{
String month_name;
Stack {
Store ( month_name, Pad_c( Capital( Get_monthname(cnt_mes++) ),' ',23) );
} Stack_off;
At fila, columna; Print "%s", month_name;
 
Atrow ++fila;
Range for calendario [ i+1: 1: i+8, 0:1: Cols(calendario) ];
print_calendar( SDS(calendario) );
--fila;
++cnt_columna;
columna += 25;
When( cnt_columna == 4 ) {
cnt_columna = columna = 1;
fila+=9;
}
i+=8;
Free secure month_name;
}
Prnl;
}
 
 
void print_calendar( RDS(char*,calendario) )
{
int i,j;
int row = SCREEN_ROW;
 
Iterup( row, calendario, i)
{
Iterup( col, calendario, j)
{
Print "%*s", 3, $calendario[i,j];
}
Atrow ++row;
}
}
 
</syntaxhighlight>
{{out}}
<pre>
1969
 
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1
5 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 29
30 31
 
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30
 
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
31
 
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
 
</pre>
 
=={{header|C sharp|C#}}==
An attempt to abuse the DateTime class for all static information. In the event that the number of days and months changes, so long as the DateTime class is updated accordingly, this should still print properly. It also abuses iterators to allow for a concise month printing method, but with the ability to still print x months per line.
 
<langsyntaxhighlight lang="csharp">
 
using System;
Line 1,777 ⟶ 2,013:
}
 
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
Line 1,937 ⟶ 2,172:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,982 ⟶ 2,217:
+----------------------+----------------------+----------------------+
</pre>
 
=={{header|Clojure}}==
{{trans|Common Lisp}}Java interop version.<br>
Line 1,989 ⟶ 2,223:
Written by Kyuvi.<br>
 
<langsyntaxhighlight lang="clojure">(require '[clojure.string :only [join] :refer [join]])
 
(def day-row "Su Mo Tu We Th Fr Sa")
Line 1,996 ⟶ 2,230:
 
(defn month-to-word
"Translate a month from 0 to 11 into its word representation."
[month]
((vec (.getMonths (new java.text.DateFormatSymbols))) month))
Line 2,010 ⟶ 2,244:
(defn normal-date-string
"Returns a formttedformatted list of strings of the days of the month."
[date]
(map #(join " " %)
Line 2,023 ⟶ 2,257:
(defn oct-1582-string
" Returns a formatedformatted list of strings of the days of the month of octOctober 1582."
[date]
(map #(join " " %)
Line 2,040 ⟶ 2,274:
 
(defn center-string
" ReturnReturns a string that is WIDTH long with STRING centered in it ."
[string width]
(let [pad (- width (count string))
Line 2,053 ⟶ 2,287:
 
(defn calc-columns
"CalcutateCalculates the number of columns given the width in CHARACTERS and the
MARGIN SIZE."
[characters margin-size]
(loop [cols 0 excess characters ]
Line 2,062 ⟶ 2,296:
 
(defn month-vector
"Returns a vector with the month name, day-row and days
formatted for printing."
[date]
(vec (concat
Line 2,074 ⟶ 2,308:
 
(defn year-vector [date]
"RetunsReturns a 2d vector of all the months in the year of DATE."
(loop [m [] c (month date)]
(if (= c 11 )
Line 2,083 ⟶ 2,317:
 
(defn print-months
"printsPrints the months to standard output with NCOLS and MARGIN ."
[ v ncols margin]
(doseq [r (range (Math/ceil (/ 12 ncols)))]
Line 2,096 ⟶ 2,330:
(defn print-cal
"(print-cal [year [width [margin]]])
PrintPrints out the calendar for a given YEAR with WIDTH characters wide and
with MARGIN spaces between months."
([]
(print-cal 1969 80 2))
Line 2,105 ⟶ 2,339:
(print-cal year width 2))
([year width margin]
(assert (>= width (count day-row)) "widthWidth should be more than 20.")
(assert (> margin 0) "marginMargin needs to be more than 0.")
(let [date (new java.util.GregorianCalendar year 0 1)
column-count (calc-columns width margin)
Line 2,114 ⟶ 2,348:
(println (center-string (str year) total-size))
(println)
(print-months (year-vector date) column-count margin))))</langsyntaxhighlight>
 
 
Line 2,159 ⟶ 2,393:
 
nil</pre>
 
=={{header|COBOL}}==
the program calls subroutine DATE2DOW to convert any YYYY-MM-DD to Day of Week (1=Sunday). the group names WS-CFGN
and WS-CFGW may be moved to WS-CFG to use narrow or wide print line size respectively.
<syntaxhighlight lang="cobol">
<lang COBOL>
IDENTIFICATION DIVISION.
PROGRAM-ID. CALEND.
Line 2,390 ⟶ 2,623:
.
END PROGRAM DATE2DOW.
</syntaxhighlight>
</lang>
Output (based on 80 character wide display)
<pre>
Line 2,429 ⟶ 2,662:
30
</pre>
 
=={{header|Common Lisp}}==
Depends on quicklisp.
<langsyntaxhighlight lang="lisp">(ql:quickload '(date-calc))
 
(defparameter *day-row* "Su Mo Tu We Th Fr Sa")
Line 2,506 ⟶ 2,738:
(lambda (&rest heads)
(format t format-string heads))
row))))</langsyntaxhighlight>
{{out}}
<pre>CL-USER> (print-calendar 1969)
Line 2,545 ⟶ 2,777:
30
NIL</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.datetime, std.string, std.conv;
 
void printCalendar(in uint year, in uint nCols)
Line 2,589 ⟶ 2,820:
void main() {
printCalendar(1969, 3);
}</langsyntaxhighlight>
 
<pre> [Snoopy Picture]
Line 2,633 ⟶ 2,864:
{{libheader| System.DateUtils}}
{{Trans|D}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Calendar;
 
Line 2,722 ⟶ 2,953:
printCalendar(1969, 4);
readln;
end.</langsyntaxhighlight>
{{out}}
<pre> [Snoopy Picture]
Line 2,753 ⟶ 2,984:
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30</pre>
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
year = 1969
#
wkdays$ = "Su Mo Tu We Th Fr Sa"
pagewide = 80
blank$ = ""
month$[] = [ " January " " February " " March " " April " " May " " June " " July " " August " " September" " October " " November " " December " ]
days[] = [ 31 28 31 30 31 30 31 31 30 31 30 31 ]
#
func$ center txt$ .
h$ = substr blank$ 1 ((pagewide - len txt$) / 2)
return h$ & txt$ & h$
.
func$ makewk fst lst day .
for i to day - 1
wstr$ &= " "
.
for i = fst to lst
i$ = i
if i <= 9
i$ = " " & i
.
wstr$ &= i$ & " "
.
return substr wstr$ & blank$ 1 20
.
proc dow y . ndow leap .
leap = 0
if y mod 4 = 0
leap = 1
.
if y mod 100 = 0
leap = 0
.
if y mod 400 = 0
leap = 1
.
ndow = y * 365 + y div 4 - y div 100 + y div 400 + 1
ndow = (ndow - leap) mod1 7
.
len lin$[] 8
proc prmonth nmonth newdow monsize . .
lin$[1] &= " " & month$[nmonth] & " "
lin$[2] &= wkdays$ & " "
lin$[3] &= makewk 1 (8 - newdow) newdow & " "
for i = 4 to 7
lin$[i] &= makewk (9 + h - newdow) lower monsize (15 + h - newdow) 1 & " "
h += 7
.
lin$[8] &= makewk (37 - newdow) monsize 1 & " "
if len lin$[3] + 22 > pagewide
for i to 8
print center lin$[i]
lin$[i] = ""
.
.
.
for i to pagewide
blank$ &= " "
.
dow year newdow leap
print center "[ picture of Snoopy goes here ]"
print center year
for i = 1 to 12
monsize = days[i]
if i = 2 and leap = 1
monsize = 29
.
prmonth i newdow monsize
newdow = (monsize + newdow) mod1 7
.
</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let getCalendar year =
let day_of_week month year =
let t = [|0; 3; 2; 5; 0; 3; 5; 1; 4; 6; 2; 4|]
Line 2,825 ⟶ 3,130:
sb.ToString()
 
let printCalendar year = getCalendar year</langsyntaxhighlight>
 
{{out}}
Line 2,871 ⟶ 3,176:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays calendar.format grouping io.streams.string kernel
math.ranges prettyprint sequences sequences.interleaved ;
IN: rosetta-code.calendar
Line 2,882 ⟶ 3,187:
: calendar-demo ( -- ) 1969 calendar ;
 
MAIN: calendar-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 2,921 ⟶ 3,226:
30
</pre>
=={{header|Forth}}==
<syntaxhighlight lang="forth">
: weekday ( d m y -- u )
over 3 < if swap 12 + swap 1- then
dup 4 / over 100 / - over 400 / + + swap 1+ 13 * 5 / + + 2 - 7 mod ;
: mdays ( m y -- msize mday )
over 12 = if 31 1 2swap weekday negate exit then
2>r 1 2r@ weekday 1 2r> swap 1+ swap weekday over -
7 + 7 mod 28 + swap negate ;
: .week ( msize mday -- msize mday' )
7 0 do dup 0< if 1+ 3 spaces else
2dup > if 1+ dup 2 .r space else 3 spaces then then loop ;
: .3months ( y m -- )
3 0 do ." Mo Tu We Th Fr Sa Su " loop cr
3 over + swap do i over mdays rot loop drop
6 0 do 2rot .week 2 spaces 2rot .week 2 spaces 2rot .week cr loop
2drop 2drop 2drop ;
 
: cal ( y -- )
30 spaces ." [Snoopy]" cr
32 spaces dup . cr
." January February March" cr
dup 1 .3months
." April May June" cr
dup 4 .3months
." July August September" cr
dup 7 .3months
." October November December" cr
10 .3months ;
 
1969 cal
</syntaxhighlight>
{{out}}
<pre>
[Snoopy]
1969
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
=={{header|Fortran}}==
Already having a routine to produce a calendar simplified matters. However, it added to each row of days a row of annotations for those days (Xmas, etc. and also +2hh and -2hh for the days with two half-hour changes in length due to daylight saving: NZ also had daylight saving with changes of one half-hour) which meant that the field allowance was always four. With the annotations abandoned, this could be reduced to three, and, the first day column on a line does not need a leading space. Since the method employed variables for the layout, it could easily be twiddled to have three months per line (thus fitting into a line length of 80) or six (using most of a line length of 132) and so it became a matter of pulling together the needed routines from various places.
<syntaxhighlight lang="fortran">
<lang Fortran>
MODULE DATEGNASH !Assorted vexations. Time and calendar games, with local flavourings added.
 
Line 3,208 ⟶ 3,583:
END DO
END
</syntaxhighlight>
</lang>
Selected output, lacking alas the outbursts from Snoopy: three months /line
<pre>
Line 3,264 ⟶ 3,639:
31 30
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 17-02-2016
' compile with: fbc -s console
 
Line 3,390 ⟶ 3,764:
'Print : Print "hit any key to end program
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 1969
Line 3,409 ⟶ 3,783:
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30 27 28 29 30 31 24 25 26 27 28 29 30 29 30 31 </pre>
 
=={{header|FutureBasic}}==
Legacy version:
<lang futurebasic>
<syntaxhighlight lang="futurebasic">window 1, @"Calendar", (0, 0, 520, 520 )
include "ConsoleWindow"
 
dim as Str255 a
 
open "UNIX", 1,"cal 1969"
do
line input #1, a
print a
until eof(1)
close 1
</lang>
 
HandleEvents</syntaxhighlight>
Output:
 
Modern version:
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
 
local fn RunCommand( command as CFStringRef ) as CFStringRef
TaskRef tsk = fn TaskInit
TaskSetExecutableURL( tsk, fn URLFileURLWithPath( @"/bin/sh" ) )
TaskSetArguments( tsk, @[@"-c",command] )
PipeRef pip = fn PipeInit
TaskSetStandardOutput( tsk, pip )
FileHandleRef fh = fn PipeFileHandleForReading( pip )
fn TaskLaunch( tsk, NULL )
CFDataRef dta = fn FileHandleReadDataToEndOfFile( fh, NULL )
CFStringRef outputStr = fn StringWithData( dta, NSUTF8StringEncoding )
end fn = outputStr
 
NSLog( @"%@", fn RunCommand( @"cal 1969" ) )
 
HandleEvents</syntaxhighlight>
 
Output of either version:
<pre>
1969
Line 3,461 ⟶ 3,854:
30
</pre>
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Sub Main()
 
Shell "cal 1969"
 
End</langsyntaxhighlight>
Output:
<pre>
Line 3,507 ⟶ 3,899:
30
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight Golang="go">package main
 
import (
Line 3,579 ⟶ 3,970:
fmt.Println()
}
}</langsyntaxhighlight>
Output:
<pre> [SNOOPY]
Line 3,621 ⟶ 4,012:
30
</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import qualified Data.Text as T
import Data.Time
import Data.Time.Calendar
Line 3,712 ⟶ 4,102:
calcol' = calColFromCol columns
 
columns' = colFromCalCol calcol'</langsyntaxhighlight>
<pre>*Main> printCalendar 1969 80
[Maybe Snoopy]
Line 3,749 ⟶ 4,139:
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30</pre>
 
=={{header|Huginn}}==
<langsyntaxhighlight lang="huginn">import DateTime as dt;
import Algorithms as algo;
import Text as text;
Line 3,811 ⟶ 4,200:
cols
);
}</langsyntaxhighlight>
 
Output: <pre> 1969
Line 3,847 ⟶ 4,236:
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
The procedures ''printCalendar'' handles formatting of large components and uses co-expressions to keep the formatting of week elements in each column synchronized. The procedure ''CalendarFormatWeek'' is a generator that returns heading elements, alignment spacing, and individual days.
<langsyntaxhighlight Iconlang="icon">procedure main(A)
printCalendar(\A[1]|1969)
end
Line 3,894 ⟶ 4,282:
if m = 2 & IsLeapYear(year) then suspend (d +:= 1, 29) # LY adjustment
every d to (6*7) do suspend "" # trailer alignment
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 3,934 ⟶ 4,322:
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30</pre>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">require 'dates format' NB. J6.x
require 'dates general/misc/format' NB. J7.x
calBody=: (1 1 }. _1 _1 }. ":)@(-@(<.@%&22)@[ ]\ calendar@])
calTitle=: (<: - 22&|)@[ center '[Insert Snoopy here]' , '' ,:~ ":@]
formatCalendar=: calTitle , calBody</langsyntaxhighlight>
'''Example use:'''
<langsyntaxhighlight lang="j"> 80 formatCalendar 1969
[Insert Snoopy here]
1969
Line 3,981 ⟶ 4,368:
19 20 21 22 23 24 25│ 16 17 18 19 20 21 22│ 21 22 23 24 25 26 27
26 27 28 29 30 31 │ 23 24 25 26 27 28 29│ 28 29 30 31
│ 30 │ </langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|D}}
<langsyntaxhighlight lang="java">import java.text.*;
import java.util.*;
 
Line 4,041 ⟶ 4,427:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,083 ⟶ 4,469:
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30 </pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">/**
* Given a width, return a function that takes a string, and
* pads it at both ends to the given width
Line 4,206 ⟶ 4,591:
 
cal(1969, 'en-US', 3);
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,249 ⟶ 4,634:
30
</pre>
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
'''Also works with fq, a Go implementation of a large subset of jq'''
 
'''Utility Functions'''
<syntaxhighlight lang=jq>
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def center(width):
tostring
| length as $l
| ((width - $l)/2 | floor) as $k
| (" " * $k) + . + (" " * (width - ($k+$l)));
</syntaxhighlight>
'''Calendrical Functions'''
<syntaxhighlight lang=jq>
def weekdaynames: ["Su", "Mo","Tu", "We", "Th", "Fr", "Sa"];
 
# q.v. weekday
# Output the integer index of weekdaynames, i.e. Sunday is 0
def dayofweek($year; $month; $day):
"\($year)-\($month)-\($day)" | strptime("%Y-%m-%d") | .[-2];
 
def isLeapYear(y):
y%4 == 0 and ((y%100 != 0) or (y%400 == 0));
 
# January is 1
def monthLength($y; $m):
def __diy: [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
def __diy2: [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366];
 
if isLeapYear($y)
then __diy2[$m] - __diy2[$m-1]
else __diy[$m] - __diy[$m-1]
end;
 
def calendar(year):
def snoopy: "🐶";
def months: [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
(weekdaynames | join(" ")) as $days
# $chunk is a list of three months in group number $c
| def printRow($chunk; $c):
# The line of month names:
(" " + ($chunk | map(center(20)) | join(" "))),
# The line of weekday names
(" " + ([range(0;3)| $days] | join(" "))),
# The body of the calendar
( [ dayofweek(year; $c*3 + 1; 1),
dayofweek(year; $c*3 + 2; 1),
dayofweek(year; $c*3 + 3; 1)] as $first
| [ monthLength(year; $c*3 + 1),
monthLength(year; $c*3 + 2),
monthLength(year; $c*3 + 3) ] as $mlen
# Print up to 6 lines
| range(0;6) as $i
| reduce range(0;3) as $j ("";
(1 + (7 * $i) - $first[$j]) as $start
| (reduce range($start; $start+7) as $k (.;
if ($k >= 1 and $k <= $mlen[$j])
then . + ($k|lpad(3))
else . + " "
end ) + " " ) )
),
"";
 
(snoopy, "--- \(year) ---" | center(72)),
( [months|nwise(3)] as $chunks
| range(0;3) | printRow( $chunks[.]; .) );
 
calendar(1969)
</syntaxhighlight>
{{output}}
As for [[#Wren|Wren]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
using Dates
Line 4,316 ⟶ 4,783:
lineprintcalendar(1969)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">import java.io.PrintStream
import java.text.DateFormatSymbols
import java.text.MessageFormat
Line 4,374 ⟶ 4,841:
fun main(args: Array<String>) {
System.out.printCalendar(1969, 3, Locale.US)
}</langsyntaxhighlight>
{{out}}
See D output.
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
rem Adapted from LB examples included with software
[start]
Line 4,439 ⟶ 4,905:
monthname$=word$("January February March April May June July August September October November December",month)
end function
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,502 ⟶ 4,968:
 
</pre>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">----------------------------------------
-- @desc Class "Calendar"
-- @file parent script "Calendar"
Line 4,560 ⟶ 5,025:
on _write (me, str, x, y)
put str into char x to x+str.length-1 of line y of _calStr
end</langsyntaxhighlight>
 
Usage:
<langsyntaxhighlight lang="lingo">calObj = script("Calendar").new()
calStr = calObj.make(1969)
put calStr</langsyntaxhighlight>
 
{{out}}
Line 4,606 ⟶ 5,071:
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function print_cal(year)
local months={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE",
"JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"}
Line 4,670 ⟶ 5,134:
end
 
print_cal(1969)</langsyntaxhighlight>
{{out}}
<pre>
Line 4,712 ⟶ 5,176:
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
 
=={{header|M2000 Interpreter}}==
Set console to 80 character by 43 lines. Produce calendar line by line, three columns of months.
Line 4,720 ⟶ 5,183:
Module Calendar get the Year and the Language Id (1033 for English, 1032 for Greek and any other which support the Window OS).
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Calendar (Year, LocaleId) {
Function GetMax(Year, Month) {
Line 4,788 ⟶ 5,251:
k=Key$ ' wait key
Calendar 2018, 1032 ' Greek
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
 
Line 4,813 ⟶ 5,275:
Therefore, I've chosen to implement "raw" textual grids from scratch with a function called DataGrid. There are two versions of DataGrid. The first version allows you to specify exactly the height and width of each row and column in terms of number of string characters. The second version allows you to specify just the "macro" grid dimensions, and the actual size of each cell will be determined by calculating the width among cells in the same column and max height among cells in the same row. With those max values, it calls the first GridData function. The cell dimensions do not account for spacing and borders, which are added after the cell is scaled to the correct character dimensions. A variety of decoration options can also be supplied. The effect of these will be demonstrated in the examples below. Each version acts on a list of cells that are already rectangular arrays of data (though their dimensions obviously don't already need to be identical--that's part of the job of DataGrid, and it's why DataGrid can be composed/nested). Also, the list of cells is "flat", and its length does not need to conform to an exact rectangle (again, this is a job of DataGrid).
 
<langsyntaxhighlight Mathematicalang="mathematica">DataGrid[
rowHeights:{__Integer},
colWidths:{__Integer},
Line 4,861 ⟶ 5,323:
 
(*While the grid functions make no assumptions about the format of the data, we will be using them with string/character data, and we will eventually want to output a calendar as a single large string. AsString gives us a standard method for transforming a matrix of characters into a string with rows delimited by newlines.*)
AsString[matrix_List?MatrixQ]:=StringRiffle[matrix,"\n",""];</langsyntaxhighlight>
 
To illustrate how DataGrid works, I'll create some sample data-grids and assemble them together into a larger data-grid, and then I'll use DataGrid itself to present all of these results. Notice the options for alignment, borders, spacings, etc.
 
<langsyntaxhighlight Mathematicalang="mathematica">DataA={{"A"}};
GridA = DataGrid[{2},{5},{0,0},{{1,1},{1,1}},<|"border"->"*","alignment"->{1,-1}|>,{DataA}];
DataB = {{"B"},{"B"}};
Line 4,873 ⟶ 5,335:
DataGrid[{2,3},{2,3},{{0,1},{2,3}},<|"border"->"@","alignment"->{0,0},"dividers"->{"/","/","/"}|>,{GridA,GridB,GridA,GridB,GridA}];
 
DataGrid[{2,3},{2,12},{{0,0},{0,0}},<||>,{{{"A"}},{{"B"}},{{"C"}},GridA,GridB,GridC}]//AsString</langsyntaxhighlight>
 
{{out}}
Line 4,903 ⟶ 5,365:
HeaderGrid is a helper that defers the "gridding" of the individual header cells and data cells to functions that get passed in. There are some assumptions about presentation that feed into this design, but if your requirements differ, it's fairly easy to follow this same basic pattern of accepting functions that do the lower level work--you would just need to organize that work differently.
 
<langsyntaxhighlight Mathematicalang="mathematica">HeadedGrid[
finalSpacings:{_Integer,_Integer},
finalBorderWidths:{{_Integer,_Integer},{_Integer,_Integer}},
Line 4,925 ⟶ 5,387:
{"Su","Mo","Tu","We","Th","Fr","Sa"},
DataGrid[{3},{4},{0,0},{{0,0},{0,0}},<|"alignment"->{-1,1}|>,#]&,
ToString/@Range[31]]//AsString</langsyntaxhighlight>
 
{{out}}
Line 4,956 ⟶ 5,418:
MonthGrid will use HeadedGrid for the main part of the month, and it will take another gridding function to handle the title (month name). I could have simplified the method signature by having the caller pass in a HeadedGrid function directly, but I liked exposing more control with this function. MonthGrid offers the convenience of specifying a leading offset for the month (month and week boundaries don't typically align, so this allows for starting on the correct day of the week without requiring the caller to munge the data).
 
<langsyntaxhighlight Mathematicalang="mathematica">MonthGrid[
finalSpacings:{_Integer,_Integer},
finalBorderWidths:{{_Integer,_Integer},{_Integer,_Integer}},
Line 4,981 ⟶ 5,443:
{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"},
DataGrid[{1}, {2}, {0, 0}, {{0, 0}, {0, 0}}, <|"alignment" -> {-1, 1}|>, #] &,
2, ToString /@ Range[31]]//AsString</langsyntaxhighlight>
 
{{out}}
Line 4,998 ⟶ 5,460:
FractalChars takes a string and rasterizes each character and then replaces each black pixel with the original character. The output is a matrix of characters suitable as a datum in the argument to DataGrid.
 
<langsyntaxhighlight Mathematicalang="mathematica">FractalChars[rasterSize_,font_,char_String/;1==StringLength[char]]:=
ReplaceAll[ImageData[ImageCrop[Binarize[Rasterize[Style[char,FontFamily->font],RasterSize->rasterSize]]]],{1->" ",0->char}];
FractalChars[rasterSize_,font_,word_String]:=FractalChars[rasterSize,font,#]&/@Characters[word];
Line 5,009 ⟶ 5,471:
 
(*An example banner.*)
BannerGrid[{0,5},{{1,1},{0,0}},<|"border"->"X"|>,DataGrid[{1,1},{0,0},{{1,1},{0,0}},<||>,#]&,20,"1969"]//AsString</langsyntaxhighlight>
 
{{out}}
Line 5,042 ⟶ 5,504:
Fortunately, Mathematica has many powerful calendar-related functions. Unfortunately, the semantics of calendars is sufficiently complicated that there is no practical way to create just a few standard data generators that will cover all of the implied cases. For example, the seemingly simple concept of "first day of the year" is ambiguous. The Jewish year uses one start day for religious holidays and another day for civil/secular purposes. The task definition referenced the switch-over from the Julian calendar to the Gregorian calendar, but this did not happen simultaneously around the globe, and many regions adopted the Gregorian calendar surprisingly recently. Assigning a number to a year is not standardized across calendars (e.g. consider the Chinese and Jewish calendars). The Cotsworth Calendar (effectively the International Fixed Calendar) has days that aren't assigned to any month or week--they're just dangling special days. So, we can provide some simple, general helper functions, but we assume that the calendar maker will be responsible for dealing with special cases.
 
<langsyntaxhighlight Mathematicalang="mathematica">(*Mathematica makes it easy to get month names and day names for several standard calendars.*)
MonthNames[]:=MonthNames["Gregorian"];
MonthNames[calType_]:=Lookup[CalendarData[calType,"PropertyAssociation"],"MonthNames"];
Line 5,050 ⟶ 5,512:
 
(*Since month boundaries don't align with week boundaries on most calendars, we need to pad month data with empty cells. I was tempted to create a function that would generate offsets for a given year on a given calendar, but even that required too many decisions on the part of the calendar maker to be feasible. So, I removed all of the calendar semantics. If you provide the fixed small group length (week length), the initial offset, and the list of the large group lengths (month lengths), this function will give you the offsets you need for each large group (month).*)
Offsets[groupLength_,firstOffset_,lengths_List]:=FoldPairList[{#1,Mod[#1+#2,groupLength]}&,firstOffset,lengths];</langsyntaxhighlight>
 
For the year 1969 on the Gregorian calendar, Mathematica can give us all of the data we need.
 
<langsyntaxhighlight Mathematicalang="mathematica">Data1969=GroupBy[Most[DateRange[DateObject[{1969,1,1}],DateObject[{1+1969,1,1}]]],DateValue[#,"MonthName"]&];
InitialOffset1969=QuantityMagnitude[DateDifference[PreviousDate[DateObject[{1969,1,1}],Sunday],DateObject[{1969,1,1}]]];
MonthLengths1969=Length/@Values[Data1969];
Line 5,076 ⟶ 5,538:
MonthsGrid1969=DataGrid[{3,4},{1,4},{{0,0},{0,0}},<||>,MonthGrids1969];
 
DataGrid[{2,1},{2,0},{{0,0},{0,0}},<||>,{YearBanner1969,MonthsGrid1969}]//AsString</langsyntaxhighlight>
 
{{out}}
Line 5,134 ⟶ 5,596:
It's a bit more involved to create the data for a calendar that displays the switch from Julian to Gregorian. A few major European Catholic countries made the switch in 1582, so I'll generate data for that scenario. The switch happened in October, with Thursday Oct 4 being followed by Friday Oct 15. Mathematica provides tools to both fetch the calendar data and then fiddle with the structures so that I can highlight the relevant month and dates.
 
<langsyntaxhighlight Mathematicalang="mathematica">Dates1582France=
Join[
DateRange[DateObject[{1582,1,1},CalendarType->"Julian"],DateObject[{1582,10,4},CalendarType->"Julian"],CalendarType->"Julian"],
Line 5,163 ⟶ 5,625:
{monthNameGridder[{{Characters[MonthNames1582France[[10]]]}}],
DataGrid[{1+Ceiling[Length@octoberCells/7],7},{0,1},{{0,0},{0,0}},<|"alignment"->{0,0}|>,octoberCells]}]},
DataGrid[{2,1},{2,0},{{0,0},{0,0}},<||>,{yearBanner,DataGrid[{3,4},{2,4},{{0,0},{0,0}},<|"alignment"->{-1,0}|>,ReplacePart[monthCells,10->octoberGrid]]}]]]]]//AsString</langsyntaxhighlight>
 
{{out}}
Line 5,226 ⟶ 5,688:
WidestFitDimensions will output data about the best fit it found, including the dimensions (which could be used in DataGrid) and the width of the best fit grid. There is no guarantee that a strict fit can be found, so it also provides the actual width of the best fit, and the user can determine what to do with the results. One could create another helper gridding function that uses WidestFitDimensions to automatically generate the grid, but I've already demonstrated such helper functions, so I'll just focus on computing the grid dimensions here.
 
<langsyntaxhighlight Mathematicalang="mathematica">WidestFitDimensions[
targetWidth_Integer,
columnSpacings_Integer,
Line 5,243 ⟶ 5,705:
{widths},isTooLarge[#]&,1,-1+Length@widths]},
<|"dimensions"->Dimensions@bestFitGrid,"width"->fullWidthOfGrid@bestFitGrid|>]]]];
</syntaxhighlight>
</lang>
 
Checking best fit for our 1969 calendar for a few widths.
<langsyntaxhighlight Mathematicalang="mathematica">(*Choose a best fit for our 1969 calendar data on 80 character wide display.*)
WidestFitDimensions[80,4,{0,0},MonthGrids1969]</langsyntaxhighlight>
{{out}}
<pre><|dimensions->{4,3},width->68|></pre>
<langsyntaxhighlight Mathematicalang="mathematica">(*Choose a best fit for our 1969 calendar data on 132 character wide display.*)
WidestFitDimensions[132,4,{0,0},MonthGrids1969]</langsyntaxhighlight>
{{out}}
<pre><|dimensions->{3,5},width->116|></pre>
<langsyntaxhighlight Mathematicalang="mathematica">(*Can we fit into a 20-character wide display?*)
WidestFitDimensions[20,4,{0,0},MonthGrids1969]</langsyntaxhighlight>
{{out}}
<pre><|dimensions->{12,1},width->20|></pre>
 
=={{header|Nim}}==
{{trans|D}}
<langsyntaxhighlight lang="nim">import times
import strformat
 
Line 5,298 ⟶ 5,759:
echo ""
 
printCalendar(1969, 3)</langsyntaxhighlight>
{{out}}
<pre>
Line 5,342 ⟶ 5,803:
 
</pre>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">#load "unix.cma"
 
let lang = "en" (* language: English *)
Line 5,540 ⟶ 6,000:
| _ ->
usage ()</langsyntaxhighlight>
 
{{out}}
Line 5,581 ⟶ 6,041:
</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl -l
 
use strict; # https://rosettacode.org/wiki/Calendar
Line 5,615 ⟶ 6,074:
my ($string, $w) = @_;
sprintf "%${w}s", $string . ' ' x ($w - length($string) >> 1);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 5,676 ⟶ 6,135:
31 30
</pre>
 
=={{header|Phix}}==
Gregorian calender only.
<!--<lang Phix>(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
with javascript_semantics
<span style="color: #008080;">constant</span> <span style="color: #000000;">year</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1969</span>
constant year = 1969
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
include builtins\timedate.e
<span style="color: #008080;">function</span> <span style="color: #000000;">centre</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">)</span>
function centre(string s, integer width)
<span style="color: #004080;">integer</span> <span style="color: #000000;">gap</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span>
integer gap = width-length(s),
<span style="color: #000000;">left</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">gap</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
left = floor(gap/2),
<span style="color: #000000;">right</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">gap</span><span style="color: #0000FF;">-</span><span style="color: #000000;">left</span>
right = gap-left
<span style="color: #008080;">return</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">left</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">&</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">right</span><span style="color: #0000FF;">)</span>
return repeat(' ',left) & s & repeat(' ',right)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">one_month</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">year</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">month</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">sun_to_sat</span><span style="color: #0000FF;">)</span>
function one_month(integer year, integer month, bool sun_to_sat)
<span style="color: #004080;">string</span> <span style="color: #000000;">weekdays</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sun_to_sat</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"Su Mo Tu We Th Fr Sa"</span>
string weekdays = iff(sun_to_sat?"Su Mo Tu We Th Fr Sa"
<span style="color: #0000FF;">:</span><span style="color: #008000;">"Mo Tu We Th Fr Sa Su"</span><span style="color: #0000FF;">),</span>
:"Mo Tu We Th Fr Sa Su"),
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
line = repeat(' ',20)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ldm</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">adjust_timedate</span><span style="color: #0000FF;">(</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">month</span><span style="color: #0000FF;">=</span><span style="color: #000000;">12</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">year</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
sequence ldm = adjust_timedate(iff(month=12?{year+1,1,1,0,0,0,0,0}
<span style="color: #0000FF;">:{</span><span style="color: #000000;">year</span><span style="color: #0000FF;">,</span><span style="color: #000000;">month</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}),</span>
:{year,month+1,1,0,0,0,0,0}),
<span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">days</span><span style="color: #0000FF;">:=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)),</span>
timedelta(days:=-1)),
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">centre</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ldm</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Mmmm"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">20</span><span style="color: #0000FF;">),</span><span style="color: #000000;">weekdays</span><span style="color: #0000FF;">}</span>
res = {centre(format_timedate(ldm,"Mmmm"),20),weekdays}
<span style="color: #004080;">integer</span> <span style="color: #000000;">dow</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">day_of_week</span><span style="color: #0000FF;">(</span><span style="color: #000000;">year</span><span style="color: #0000FF;">,</span><span style="color: #000000;">month</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
integer dow = day_of_week(year,month,1)
<span style="color: #008080;">if</span> <span style="color: #000000;">sun_to_sat</span> <span style="color: #008080;">then</span> <span style="color: #000000;">dow</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dow</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if sun_to_sat then dow = remainder(dow,7)+1 end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">lastday</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ldm</span><span style="color: #0000FF;">[</span><span style="color: #004600;">DT_DAY</span><span style="color: #0000FF;">],</span>
integer lastday = ldm[DT_DAY],
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dow</span><span style="color: #0000FF;">*</span><span style="color: #000000;">3</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span>
p = dow*3-2
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lastday</span> <span style="color: #008080;">do</span>
for d=1 to lastday do
<span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">..</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
line[p..p+1] = sprintf("%2d",d)
<span style="color: #000000;">p</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">3</span>
p += 3
<span style="color: #008080;">if</span> <span style="color: #000000;">dow</span><span style="color: #0000FF;">=</span><span style="color: #000000;">7</span> <span style="color: #008080;">or</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">lastday</span> <span style="color: #008080;">then</span>
if dow=7 or d=lastday then
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
res = append(res,line)
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
line = repeat(' ',20)
<span style="color: #000000;">dow</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
dow = 1
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style p ="color: #008080;">else</span>1
else
<span style="color: #000000;">dow</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
dow += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
<span style="color: #008080;">procedure</span> <span style="color: #000000;">print_calendar</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">year</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">sun_to_sat</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
procedure print_calendar(integer year, width, bool sun_to_sat=false)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">months</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">)</span>
sequence months = repeat(0,12)
<span style="color: #004080;">integer</span> <span style="color: #000000;">wide</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">width</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">22</span><span style="color: #0000FF;">)</span>
integer wide = floor((width+2)/22)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">centre</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"[Spot Reserved For Snoopy]"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">width</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
printf(1,centre("[Spot Reserved For Snoopy]",width)&"\n")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">centre</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">year</span><span style="color: #0000FF;">),</span><span style="color: #000000;">width</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
printf(1,centre(sprintf("%d",year),width)&"\n")
<span style="color: #008080;">for</span> <span style="color: #000000;">month</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
for month=1 to 12 do
<span style="color: #000000;">months</span><span style="color: #0000FF;">[</span><span style="color: #000000;">month</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">one_month</span><span style="color: #0000FF;">(</span><span style="color: #000000;">year</span><span style="color: #0000FF;">,</span><span style="color: #000000;">month</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sun_to_sat</span><span style="color: #0000FF;">)</span>
months[month] = one_month(year,month,sun_to_sat)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">month</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">by</span> <span style="color: #000000;">wide</span> <span style="color: #008080;">do</span>
for month=1 to 12 by wide do
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (more than enough)</span>
for k=1 to 9 do -- (more than enough)
<span style="color: #004080;">integer</span> <span style="color: #000000;">any</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
integer any = 0
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
string line = ""
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">wide</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
for j=0 to wide-1 do
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if length(line) then
<span style="color: #000000;">line</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" "</span>
<span style="color: #008080;">end</span> <spanline style&= "color: #008080; ">if</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">months</span><span style="color: #0000FF;">[</span><span style="color: #000000;">month</span><span style="color: #0000FF;">+</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">then</span>
if k>length(months[month+j]) then
<span style="color: #000000;">line</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
<span style line &="color: #008080;">else</span>repeat(' ',20)
else
<span style="color: #000000;">line</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">months</span><span style="color: #0000FF;">[</span><span style="color: #000000;">month</span><span style="color: #0000FF;">+</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
line &= months[month+j][k]
<span style="color: #000000;">any</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <spanany style="color: #008080;">if</span>1
<span style="color: #008080;"> end</span> <span style="color: #008080;">for</span>if
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">any</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if any=0 then exit end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">centre</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">,</span><span style="color: #000000;">width</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
printf(1,centre(line,width)&"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
end procedure
<span style="color: #000000;">print_calendar</span><span style="color: #0000FF;">(</span><span style="color: #000000;">year</span><span style="color: #0000FF;">,</span><span style="color: #000000;">80</span><span style="color: #0000FF;">)</span>
print_calendar(year,80)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1234567890"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
printf(1,join(repeat("1234567890",8),"")&"\n")
<span style="color: #000000;">print_calendar</span><span style="color: #0000FF;">(</span><span style="color: #000000;">year</span><span style="color: #0000FF;">,</span><span style="color: #000000;">132</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
print_calendar(year,132,true)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1234567890"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"12\n"</span><span style="color: #0000FF;">)</span>
printf(1,join(repeat("1234567890",13),"")&"12\n")
<!--</lang>-->
</syntaxhighlight>
{{out}}
<pre style="font-size:75%">
<pre>
[Spot Reserved For Snoopy]
1969
Line 5,811 ⟶ 6,270:
=={{header|Phixmonti}}==
{{trans|Lua}}
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
 
32 var space
Line 5,930 ⟶ 6,389:
enddef
 
2020 print_cal</langsyntaxhighlight>
{{out}}
<pre>
Line 5,999 ⟶ 6,458:
 
=== Press any key to exit ===</pre>
 
=={{header|PicoLisp}}==
This "calendar" is nicely formatted, and fits into 20 columns ;-)
<langsyntaxhighlight PicoLisplang="picolisp">(de cal (Year)
(prinl "====== " Year " ======")
(for Dat (range (date Year 1 1) (date Year 12 31))
Line 6,014 ⟶ 6,472:
(pack "Week " (week Dat)) ) ) ) ) )
 
(cal 1969)</langsyntaxhighlight>
Output:
<pre>====== 1969 ======
Line 6,040 ⟶ 6,498:
30 Tue
31 Wed</pre>
 
=={{header|Pike}}==
 
Line 6,048 ⟶ 6,505:
this script also highlights holidays by region. regions may be chosen by 2-letter country name, as well as some special 'regions': christianity, orthodox, bahai, islamic, among others.
 
<langsyntaxhighlight Pikelang="pike">#!/bin/env pike
 
int main(int argc, array(string) argv)
Line 6,167 ⟶ 6,624:
return dayname;
}
</syntaxhighlight>
</lang>
Output: (holidays lost in copy-paste)
<pre>
Line 6,205 ⟶ 6,662:
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
calendar: procedure (year) options (main);
declare year character (4) varying;
Line 6,259 ⟶ 6,715:
 
end calendar;
</syntaxhighlight>
</lang>
Output:
<pre>
Line 6,311 ⟶ 6,767:
28 29 30 31 25 26 27 28 25 26 27 28 29 30 31
</pre>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
Param([int]$Year = 1969)
Begin {
Line 6,350 ⟶ 6,805:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Call the write_calendar(Year) predicate to print a calendar for a year.
Works with swi-prolog and requires the day_of_the_week library call.
 
<langsyntaxhighlight lang="prolog">% Write out the calender, because format can actually span multiple lines, it is easier
% to write out the static parts in place and insert the generated parts into that format.
write_calendar(Year) :-
Line 6,449 ⟶ 6,903:
is_leap_year(Year) :-
0 is Year mod 100 -> 0 is Year mod 400
; 0 is Year mod 4.</langsyntaxhighlight>
 
=={{header|Python}}==
The Python [https://docs.python.org/3/library/calendar.html calendar].prcal function prints calendars with the following formatting options: optional parameters w, l, and c are for date column width, lines per week, and number of spaces between month columns, respectively.
 
<langsyntaxhighlight lang="python">>>> import calendar
>>> help(calendar.prcal)
Help on method pryear in module calendar:
Line 6,496 ⟶ 6,949:
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31</langsyntaxhighlight>
 
=={{header|R}}==
<syntaxhighlight lang="R">
library(lubridate)
library(stringi)
 
# Helper function padding
pad_d <- function(gr) gr %>% stri_pad_left(2) %>% stri_c(collapse = " ")
pad_l <- function(gr) gr %>% pad_d() %>% stri_pad_left(20)
pad_r <- function(gr) gr %>% pad_d() %>% stri_pad_right(20)
pad_20 <- " " %s*% 20
 
# 1st week mapping
idx_week <- list("1"=1,"7"=2,"6"=3,"5"=4,"4"=5,"3"=6,"2"=7)
 
# Generate a single month
gen_cal <- function(date_str) {
str_l <- list()
# Pick up month name
month_name <- month(ymd(date_str),label = T,abbr = F) %>%
as.character() %>%
stri_pad_both(20)
# Add to list with day header
str_l[length(str_l)+1] <- month_name
str_l[length(str_l)+1] <- "Mo Tu We Th Fr Sa Su"
# Day list for the month
cc <- 1:days_in_month(as.Date(date_str))
# Staring week
wd <- wday(ymd(date_str))
st <- idx_week[as.character(wd)][[1]]
 
# Add 1st week
str_l[length(str_l)+1] <- pad_l(head(cc,st))
# Middle weeks
cc <- tail(cc,-st)
while (length(cc) > 7) {
str_l[length(str_l)+1] <- pad_l(head(cc,7))
cc <- tail(cc,-7)
}
# Last week
str_l[length(str_l)+1] <- pad_r(cc)
 
# Pad for empty week
if (length(str_l)==7)
str_l[length(str_l)+1] <- pad_20
 
str_l
}
 
# Print calendar
print_calendar <- function(target_year) {
cat("\n",stri_pad_both(target_year,64),"\n\n")
for (j in seq.int(1,12,3)) {
cal <- sapply(j:(j+2),\(x) gen_cal(paste0(target_year,"/",x,"/1")))
xres <- paste(cal[,1],cal[,2],cal[,3],sep = " ")
for (i in xres) cat(i,"\n")
}
}
 
#
# Main
#
 
print_calendar("1969")
</syntaxhighlight>
 
{{out}}
<pre>
1969
 
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require racket/date net/base64 file/gunzip)
(define (calendar yr)
Line 6,530 ⟶ 7,091:
(λ(s) (equal? "" s)))))
 
(calendar 1969)</langsyntaxhighlight>
 
{{out}}
Line 6,582 ⟶ 7,143:
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my $months-per-row = 3;
my @weekday-names = <Mo Tu We Th Fr Sa Su>;
my @month-names = <Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec>;
Line 6,613 ⟶ 7,173:
((' ' xx $date.day-of-week - 1), (1..$date.days-in-month)».fmt('%2d')).flat.rotor(7, :partial).join("\n") ~
(' ' if $_ < 7) ~ (' ' xx 7-$_).join(' ') given Date.new($year, $month, $date.days-in-month).day-of-week;
}</langsyntaxhighlight>
 
=={{header|Rebol}}==
<langsyntaxhighlight lang="rebol">
Rebol []
do [if "" = y: ask "Year (ENTER for current):^/^/" [prin y: now/year]
foreach m system/locale/months [
Line 6,629 ⟶ 7,189:
]
] ask "^/^/Press [ENTER] to Continue..."]
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
Line 6,672 ⟶ 7,232:
<br>specified. &nbsp; The choice could've been performed programmatically, but I never believe that a program
<br>could best choose over what the user wants, as it depends on esthetics and looks (fashion).
<langsyntaxhighlight lang="rexx">/*REXX program to show any year's (monthly) calendar (with/without grid)*/
 
@abc='abcdefghijklmnopqrstuvwxyz'; @abcU=@abc; upper @abcU
Line 6,818 ⟶ 7,378:
put:_=arg(1);_=translate(_,,'_'chk);if \grid then _=ungrid(_);if lowerCase then _=lower(_);if upperCase then upper _;if shortest&_=' ' then return;call tell _;return
tell:say arg(1);return
ungrid:return translate(arg(1),,"│║─═┤┐└┴┬├┼┘┌╔╗╚╝╟╢╞╡╫╪╤╧╥╨╠╣")</langsyntaxhighlight>
'''output''' when using the input of: <tt> 1/1/1969 (noGrid smallest narrowest) </tt>
<pre>
Line 6,913 ⟶ 7,473:
| |
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Calendar
 
Line 7,072 ⟶ 7,631:
next
next
</syntaxhighlight>
</lang>
Output image:
 
[https://www.dropbox.com/s/sj37yypiq45o5cd/CalmoSoftCalendar.jpg?dl=0 Calendar]
=={{header|RPL}}==
Screens of calculators running RPL have a maximum of 7 lines and 22 columns, so we can only display one month at a time.
 
<code>WKDAY</code> is defined at [[Last Friday of each month#RPL|Last Friday of each month]]
[[File:RPL Calendar.png|thumb|Screenshot of a HP-48 emulator]]
{{works with|HP|48}}
« FP DUP 2 TRNC 1 → month line
« CLLCD "Mo Tu We Th Fr Sa Su" 1 DISP
1 + DUP <span style="color:blue">WKDAY</span> 1 - NEG DATE+
'''DO''' ""
1 7 '''START'''
OVER FP 2 TRNC month ==
" " 4 PICK IP →STR + DUP SIZE DUP 1 -
SWAP SUB " " IFTE + " " +
SWAP 1 DATE+ SWAP
'''NEXT'''
'line' INCR DISP
'''UNTIL''' DUP FP 2 TRNC month ≠ '''END'''
7 FREEZE
» » ‘<span style="color:blue">CAL</span>’ STO
 
1.072023 <span style="color:blue">CAL</span>
 
=={{header|Ruby}}==
<code>Date</code> class, from the standard library, knows how many days in a month, and which day is Sunday, for both Julian and Gregorian calendars. This program uses <code>Date</code> class, plus its own assumptions, to create the calendar. This program assumes that every year has 12 months and starts with January 1, and every month fits in 6 weeks starting with Sunday.
 
<langsyntaxhighlight lang="ruby">require 'date'
 
# Creates a calendar of _year_. Returns this calendar as a multi-line
Line 7,149 ⟶ 7,730:
80; end; end; end
 
puts cal(Integer(ARGV[0]), columns)</langsyntaxhighlight>
 
Here is 1969 in 132 columns.
Line 7,211 ⟶ 7,792:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// Assume your binary name is 'calendar'.
// Command line:
// >>$ calendar 2019 150
Line 7,309 ⟶ 7,890:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
{{libheader|Scala}}
Line 7,316 ⟶ 7,896:
=== Scala Version 1 ===
 
<syntaxhighlight lang="scala">import java.util.{ Calendar, GregorianCalendar }
[[Category:Scala examples needing attention]]
<lang Scala>import java.util.{ Calendar, GregorianCalendar }
import language.postfixOps
import collection.mutable.ListBuffer
Line 7,469 ⟶ 8,048:
printCalendar(getGregCal(1969), List("[Snoopy Picture]", "1969"))
printCalendar(getGregCal(1582), List("[Snoopy Picture]", "1582"), printerWidth = 132)
}</langsyntaxhighlight>
 
{{out}}
Line 7,539 ⟶ 8,118:
{{works with|Scala|2.10.1}}
 
<syntaxhighlight lang="scala">/**
<lang Scala>/**
* Loosely based on the Ruby implementation.
*
Line 7,588 ⟶ 8,167:
}
 
}</langsyntaxhighlight>
 
<syntaxhighlight lang="scala">/**
<lang Scala>/**
* This provides extra classes needed for the main
* algorithm.
Line 7,642 ⟶ 8,221:
}
 
}</langsyntaxhighlight>
 
Sample output for 1792:
Line 7,674 ⟶ 8,253:
31
</pre>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "time.s7i";
 
Line 7,728 ⟶ 8,306:
begin
printCalendar(1969, 3);
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/date.htm#calendar]
Line 7,774 ⟶ 8,352:
</pre>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">require('DateTime')
 
define months_per_col = 3
Line 7,827 ⟶ 8,404:
}
 
print fmt_year(ARGV ? Number(ARGV[0]) : 1969)</langsyntaxhighlight>
{{out}}
<pre>
Line 7,863 ⟶ 8,440:
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
 
=={{header|Simula}}==
{{trans|C}}
The subtleties of the post-increment logic which is used in the c program took me some time to detect.
For symmetry I also implemented the pre-increment.
<langsyntaxhighlight lang="simula">BEGIN
INTEGER WIDTH, YEAR;
INTEGER COLS, LEAD, GAP;
Line 8,011 ⟶ 8,587:
PRINT_YEAR;
END;
END;</langsyntaxhighlight>
{{out}}
<pre>
Line 8,052 ⟶ 8,628:
 
</pre>
 
=={{header|Smalltalk}}==
 
This implementation has been developed using '''Cuis Smalltalk''' [https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev] with '''Aconcagua''' loaded.
To run it, evaluate: <syntaxhighlight lang ="smalltalk">CalendarPrinter printOnTranscriptForYearNumber: 1969</langsyntaxhighlight>
<syntaxhighlight lang="smalltalk">
<lang Smalltalk>
"Instance Variables:
- yearToPrint: The year to print the calendar of
Line 8,194 ⟶ 8,769:
"Returns the number of character per month"
^20
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 8,230 ⟶ 8,805:
30
</pre>
 
=={{header|SPL}}==
<langsyntaxhighlight lang="spl">year = 1969
#.output(#.str(year,">68<"))
> row, 0..3
Line 8,268 ⟶ 8,842:
<
<= lines
.</langsyntaxhighlight>
{{out}}
<pre>
Line 8,305 ⟶ 8,879:
</pre>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
let monthWidth = 20
Line 8,399 ⟶ 8,972:
}
 
printCalendar(year: 1969, width: 80)</langsyntaxhighlight>
 
{{out}}
Line 8,440 ⟶ 9,013:
30
</pre>
 
=={{header|Tcl}}==
Due to the prevalence of 80 column devices instead of 132 column ones, this code produces 3 months at a time instead of 4.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
# Produce information about the days in a month, without any assumptions about
Line 8,517 ⟶ 9,089:
 
snoopy
cal</langsyntaxhighlight>
Which produces this output:
<pre>
Line 8,556 ⟶ 9,128:
</pre>
If a different year is chosen, it's printed…
<langsyntaxhighlight lang="tcl">snoopy
cal 1969</langsyntaxhighlight>
<pre>
[Snoopy Picture]
Line 8,594 ⟶ 9,166:
</pre>
The code also handles Julian/Gregorian switch dates correctly, but ''must'' be told what locale to format the switch as and for what timezone in that case. For example, Spain was one of the first countries to make the change:
<langsyntaxhighlight lang="tcl">snoopy
cal 1582 :Europe/Madrid es_ES</langsyntaxhighlight>
<pre>
[Snoopy Picture]
Line 8,632 ⟶ 9,204:
</pre>
As can be seen, a Real Programmer has many intricacies to deal with!
 
=={{header|uBasic/4tH}}==
The required "Snoopy" code is here. Use it when you feel like it.
<syntaxhighlight lang="text">Print " XXXX"
Print " X XX"
Print " X *** X XXXXX"
Line 8,678 ⟶ 9,249:
Print " =====********** * X ) \\ )"
Print " ====* * X \\ \\ )XXXXX"
Print " =========********** XXXXXXXXXXXXXXXXXXXXXX"</langsyntaxhighlight>
This is the code that generates the calendar:
<syntaxhighlight lang="text">Input "Year to print: "; Y ' input required year
 
Push 63 : Gosub _Space ' center year
Line 8,807 ⟶ 9,378:
010 Print " October "; : Return
011 Print " November"; : Return
012 Print " December"; : Return</langsyntaxhighlight>
Output (missing Snoopy):
<pre>Year to print: 1969
Line 8,831 ⟶ 9,402:
 
0 OK, 0:2753</pre>
 
=={{header|UNIX Shell}}==
All Unix variations come with the cal command which makes the task very easy.
<langsyntaxhighlight lang="bash">
#!/bin/sh
echo "Snoopy goes here"
cal 1969
</syntaxhighlight>
</lang>
 
Output produced:
Line 8,878 ⟶ 9,448:
30
</pre>
=={{header|VBScript}}==
VBScript has a good set of time-date functions and can easily get the names of weekdays and months from the OS. The program sends its output line by line to stdout so it can be redirected to a file or to a printer. By default the code will print the calendar in the locale set in your Windows, you can select another one if required. It should be called from cscript.
 
<syntaxhighlight lang="vb">
'call it with year, number of months per row (1,2,3,4,6) and locale ("" for default)
docal 1969,6,""
 
function center (s,n) x=n-len(s):center=space(x\2+(x and 1))& s & space(x\2):end function
sub print(x) wscript.stdout.writeline x : end sub
function iif(a,b,c) :if a then iif=b else iif =c end if : end function
 
sub docal (yr,nmonth,sloc)
'yr year to print
'nmonth number of monts side to side, allowed values :1,2,3,4,6
'sloc locale to use . "" uses the default
 
dim ld(6)
dim d(6)
if nmonth=5 or nmonth>6 or nmonth<1 then wscript.stderr.writeline "Can't use width " & nmonth :exit sub
 
'set the locale (names of months and weekdays plus first day of week)
if sloc<>"" then Setlocale sloc
 
'make a row of short weekday names to put on top of the month days
'trim the names to 2 char and align them right
wday=""
for i=1 to 7
wday=wday &" "&right(" "& left(weekdayname(i,true,vbUseSystemDayOfWeek),2),2)
next
 
'print header of the calendar
ncols=nmonth*21+(nmonth-1)*1
print center("[Snoopy]",ncols)
print center(yr,ncols)
print string(ncols,"=")
 
'row of months
for i=1 to 12\nmonth
s="": s1="":esp=""
for j=1 to nmonth
'build header of the month row
s=s & esp & center(monthname(m+j),21)
s1=s1 & esp & wday
'get negative offset of first day of week to the weekday of day 1 of each month
d(j)= -weekday(dateserial(yr,m+j,1),vbUseSystemDayOfWeek)+2
'get last day of each month from Windows
ld(j)=day(dateserial(yr,m+j+1,0))
esp=" "
next
'print the row of months header (name of month and weekday names)
print s: print s1
'weekday rows. makes 5 or 6 rows according to the month requiring most
while(d(1)<ld(1)) or (d(2)<ld(2)) or (d(3)<ld(3)) or (d(4)<ld(4))or (d(5)<ld(5)) or (d(6)<ld(6))
s=""
for j=1 to nmonth
'fill present week row
for k=1 to 7
'add a day number only if inside the range of days of this montg
s=s& right(space(3)&iif(d(j)<1 or d(j)>ld(j),"",d(j)),3)
d(j)=d(j)+1
next
s=s&" "
next
'print a complete row of days
print s
wend
'go for the next row of monts
m=m+nmonth
if i<>12\nmonth then print ""
next
 
'print footer
print string(ncols,"=")
end sub
</syntaxhighlight>
output
<pre>
[Snoopy]
1969
===================================================================================================================================
enero febrero marzo abril mayo junio
lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do
1 2 3 4 5 1 2 1 2 1 2 3 4 5 6 1 2 3 4 1
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
 
julio agosto septiembre octubre noviembre diciembre
lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 1 2 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30 27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
===================================================================================================================================
</pre>
=={{header|Vedit macro language}}==
This implementation uses the '''calendar.vdm''' macro that comes with Vedit. (So it serves as an example of how to call other macros from a macro.)
Line 8,884 ⟶ 9,554:
Calendar.vdm draws one month calendar. That is then copied as columnar block and pasted to the 1969 calendar.
 
<langsyntaxhighlight lang="vedit">Buf_Switch(Buf_Free)
Config_Tab(5,30,55)
#9 = 1 // first day of week: 0=Su, 1=Mo
Line 8,904 ⟶ 9,574:
EOF
Ins_Newline(2)
} </langsyntaxhighlight>
 
The code above creates calendar for 80x43 display. For 132 column display, change #3 (number of months per line) to 4 or 6, and adjust the tab positions if required.
The following would work with all widhts, but the left/right margin would not be balanced.
<syntaxhighlight lang ="vedit">Config_Tab(22)</langsyntaxhighlight>
 
Output:
Line 8,955 ⟶ 9,625:
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
 
=={{header|Visual Basic .NET}}==
'''Compiler:''' Roslyn Visual Basic (language version >= 15.8)
Line 8,965 ⟶ 9,634:
 
Options and imports statements (all parts must be in one file):
<langsyntaxhighlight lang="vbnet">Option Compare Binary
Option Explicit On
Option Infer On
Line 8,973 ⟶ 9,642:
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Runtime.CompilerServices</langsyntaxhighlight>
 
Helper to parse command-line arguments.
<langsyntaxhighlight lang="vbnet">Module ArgHelper
ReadOnly _ArgDict As New Dictionary(Of String, String)()
 
Line 9,013 ⟶ 9,682:
End If
End Sub
End Module</langsyntaxhighlight>
 
Program:
<langsyntaxhighlight lang="vbnet">Module Program
Sub Main(args As String())
Dim dt As Date
Line 9,300 ⟶ 9,969:
End Function
End Module
</syntaxhighlight>
</lang>
 
====Command-line arguments:====
Line 9,542 ⟶ 10,211:
</pre>
 
 
 
=={{header|VBScript}}==
VBScript has a good set of time-date functions. The program issues its output line to line to stdout so it can ba redirected to file or to printer. By deault the code will print the calender in your locale, you can chenge it.
<lang vb>
'call it with year, number of months per row (1,2,3,4,6) and locale ("" for default)
 
docal 2022,6,""
 
function center (s,n) x=n-len(s):center=space(x\2+(x and 1))& s & space(x\2):end function
 
sub print(x) wscript.stdout.writeline x : end sub
 
function iif(a,b,c) :if a then iif=b else iif =c end if : end function
 
sub docal (yr,nmonth,sloc)
dim ld(6)
dim d(6)
if nmonth=5 or nmonth>6 then wscript.stderr.writeline "Can't use width " & nmonth :exit sub
if sloc<>"" then Setlocale sloc
 
for i=1 to 7
wday=wday &" "&left(weekdayname(i,true,vbUseSystemDayOfWeek),2)
next
ncols=nmonth*21+(nmonth-1)*1
print center("[Snoopy]",ncols)
print center(yr,ncols)
print string(ncols,"=")
 
for i=1 to 12\nmonth
 
s="": s1="":esp=""
for j=1 to nmonth
s=s & esp & center(monthname(m+j),21)
s1=s1 & esp & wday
d(j)= -weekday(dateserial(yr,m+j,1),vbUseSystemDayOfWeek)+2
ld(j)=day(dateserial(yr,m+j+1,0))
esp=" "
next
print s: print s1
while(d(1)<ld(1)) or (d(2)<ld(2)) or (d(3)<ld(3)) or (d(4)<ld(4))
s=""
for j=1 to nmonth
for k=1 to 7
s=s& right(space(3)&iif(d(j)<1 or d(j)>ld(j),"",d(j)),3)
d(j)=d(j)+1
next
s=s&" "
next
print s
wend
m=m+nmonth
if i<>12\nmonth then print ""
next
print string(ncols,"=")
end sub
 
</lang>
output
<pre>
[Snoopy]
2022
===================================================================================================================================
enero febrero marzo abril mayo junio
lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do
1 2 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 1 1 2 3 4 5
3 4 5 6 7 8 9 7 8 9 10 11 12 13 7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12
10 11 12 13 14 15 16 14 15 16 17 18 19 20 14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19
17 18 19 20 21 22 23 21 22 23 24 25 26 27 21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26
24 25 26 27 28 29 30 28 28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30
 
julio agosto septiembre octubre noviembre diciembre
lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do lu ma mi ju vi sá do
1 2 3 1 2 3 4 5 6 7 1 2 3 4 1 2 1 2 3 4 5 6 1 2 3 4
4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11
11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18
18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25
25 26 27 28 29 30 31 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31
===================================================================================================================================
</pre>
 
=={{header|WYLBUR}}==
 
Unlike IBM 3278 terminals, WYLBUR was "of the time" of 1969. And rather than being a simulation of its display in 20-column mode, the "output" below is an adaptation of the actual display (as simple to create & use in any other text editor not using proportional spacing) for html. This entry is intended to show that WYLBUR could display 1969's calendar in a much smaller area than traditional alternatives (even without ORVYL). It also accomodates the display of Julian and even Old Style Calendars. One has to calculate the day for one Key Date in a given Calendar year, but using a less involved formula than for a pure computer program, since the burden for knowing whether certain months fall in a leap year shifts to the user.
 
A month appears as numbers (Roman numerals for leap years) below a column of dates which are defined as "Key Dates" for the corresponding months. A Key Day is the day of the week on which Key Dates occur in a Calendar year. Step A describes how to find the Key Day, which is used in Step B to display a calendar just by sliding the columns of dates in the lower output window. In WYLBUR, one slides the row of weekdays instead by inserting or deleting spaces to the left of the first "S" (and vastly easier to align than here via javascript).
 
A. Calculate the Key Day: for a given date in the Gregorian Calendar, calculate the day on which its Key Dates occur using ([YE32 div
16]+ YEAR div 4 + YEAR) mod 7. (For Julian & Old Style dates, omit the [YE32...] term where the non-century digits of the YEAR are
replaced by 32.) The remainder of the division by 7 gives the Key Day as follows: 0=Sunday -> 6=Saturday. For the Gregorian 1969, (120 + 492 + 1969) mod 7 gives Friday as the Key Day.
 
B. Display the calendar for November: slide the columns so that 28 rests under the Key Day. One has as well the calendar for the
prior MARCH & non-leap FEB corresponding to the relevant Calendar of the given date, as shown in the Month Key.
 
C. Display the calendar for another month: slide the columns so that the Key Day appears in the same column as the number of
the month to be displayed, as shown in the Month Key.
 
N.B. the output window may not scroll in Microsoft browsers; in Firefox a monospaced font might need to be specified, if it's using a
proportional one; Chrome displays this properly for both desktop & mobile (9.87+) versions.
{{output}}
<pre> Any Year Calendar
--------------------
S M T W T F S
</pre>
<pre style="height:35x;width:25ex;">
 
......................1..2..3..4..5..6
.1..2..3..4..5..6..7..8..9.10.11.12.13
.8..9.10.11.12.13.14.15.16.17.18.19.20
15.16.17.18.19.20.21.22.23.24.25.26.27
22.23.24.25.26.27.28.29.30.31
29.30.31
..........i.09.................i.09...
ii.05.00.04.12.06.03.ii.05.00.04.12.06
08.13.10.07.xv.14.11.08.13.10.07.xv.14
</pre>
Month Key
over the following
span of months for
relevant Calendar:
<pre>
00 non-leap JAN: Julian or Gregorian
.i a leap JAN on Julian or Gregorian
ii a leap FEB on Julian or Gregorian
03 non-leap FEB: Julian or Gregorian
03 MARCH on Julian or Gregorian
--
03 25-31 MARCH: Old Style Calendar
04 to 12: April-December on all three
13 jan on Old Style Calendar
14 feb on Old Style Calendar
14 march 01-24: O. S. non-leap year
xv march 01-24: Old Style leap year
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-date}}
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<langsyntaxhighlight ecmascriptlang="wren">import "./date" for Date
import "./fmt" for Fmt
import "./seq" for Lst
 
var calendar = Fn.new { |year|
Line 9,732 ⟶ 10,262:
}
 
calendar.call(1969)</langsyntaxhighlight>
 
{{out}}
Line 9,776 ⟶ 10,306:
</pre>
 
=={{header|WYLBUR}}==
 
Unlike IBM 3278 terminals, WYLBUR was "of the time" of 1969. And rather than being a simulation of its display in 20-column mode, the "output" below is an adaptation of the actual display (as simple to create & use in any other text editor not using proportional spacing) for html. This entry is intended to show that WYLBUR could display 1969's calendar in a much smaller area than traditional alternatives (even without ORVYL). It also accomodates the display of Julian and even Old Style Calendars. One has to calculate the day for one Key Date in a given Calendar year, but using a less involved formula than for a pure computer program, since the burden for knowing whether certain months fall in a leap year shifts to the user.
 
A month appears as numbers (Roman numerals for leap years) below a column of dates which are defined as "Key Dates" for the corresponding months. A Key Day is the day of the week on which Key Dates occur in a Calendar year. Step A describes how to find the Key Day, which is used in Step B to display a calendar just by sliding the columns of dates in the lower output window. In WYLBUR, one slides the row of weekdays instead by inserting or deleting spaces to the left of the first "S" (and vastly easier to align than here via javascript).
 
A. Calculate the Key Day: for a given date in the Gregorian Calendar, calculate the day on which its Key Dates occur using ([YE32 div
16]+ YEAR div 4 + YEAR) mod 7. (For Julian & Old Style dates, omit the [YE32...] term where the non-century digits of the YEAR are
replaced by 32.) The remainder of the division by 7 gives the Key Day as follows: 0=Sunday -> 6=Saturday. For the Gregorian 1969, (120 + 492 + 1969) mod 7 gives Friday as the Key Day.
 
B. Display the calendar for November: slide the columns so that 28 rests under the Key Day. One has as well the calendar for the
prior MARCH & non-leap FEB corresponding to the relevant Calendar of the given date, as shown in the Month Key.
 
C. Display the calendar for another month: slide the columns so that the Key Day appears in the same column as the number of
the month to be displayed, as shown in the Month Key.
 
N.B. the output window may not scroll in Microsoft browsers; in Firefox a monospaced font might need to be specified, if it's using a
proportional one; Chrome displays this properly for both desktop & mobile (9.87+) versions.
{{output}}
<pre> Any Year Calendar
--------------------
S M T W T F S
</pre>
<pre style="height:35x;width:25ex;">
 
......................1..2..3..4..5..6
.1..2..3..4..5..6..7..8..9.10.11.12.13
.8..9.10.11.12.13.14.15.16.17.18.19.20
15.16.17.18.19.20.21.22.23.24.25.26.27
22.23.24.25.26.27.28.29.30.31
29.30.31
..........i.09.................i.09...
ii.05.00.04.12.06.03.ii.05.00.04.12.06
08.13.10.07.xv.14.11.08.13.10.07.xv.14
</pre>
Month Key
over the following
span of months for
relevant Calendar:
<pre>
00 non-leap JAN: Julian or Gregorian
.i a leap JAN on Julian or Gregorian
ii a leap FEB on Julian or Gregorian
03 non-leap FEB: Julian or Gregorian
03 MARCH on Julian or Gregorian
--
03 25-31 MARCH: Old Style Calendar
04 to 12: April-December on all three
13 jan on Old Style Calendar
14 feb on Old Style Calendar
14 march 01-24: O. S. non-leap year
xv march 01-24: Old Style leap year
</pre>
=={{header|XLISP}}==
Targets the 132-column imaginary line printer. The day of the week on 1 January of a given year is found using Gauss's algorithm; <tt>LEAP-YEARP</tt> is taken from the Rosetta Code task on leap years.
<langsyntaxhighlight lang="lisp">(defun calendar (year)
(define months-list '(("JANUARY" 31) ("FEBRUARY" 28) ("MARCH" 31) ("APRIL" 30) ("MAY" 31) ("JUNE" 30) ("JULY" 31) ("AUGUST" 31) ("SEPTEMBER" 30) ("OCTOBER" 31) ("NOVEMBER" 30) ("DECEMBER" 31)))
(define days #(" Sunday " "Monday " "Tuesday " "Wednesday " "Thursday " "Friday " "Saturday"))
Line 9,835 ⟶ 10,418:
(print-calendar months-list (gauss-algorithm year)))
 
(calendar 1969)</langsyntaxhighlight>
{{out}}
<pre> ******** SNOOPY CALENDAR 1969 ********
Line 9,951 ⟶ 10,534:
21 22 23 24 25 26 27
28 29 30 31 </pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
func WeekDay(Year, Month, Day); \Return day of week (0=Sun 1=Mon..6=Sat)
Line 10,011 ⟶ 10,593:
];
 
Calendar(1969)</langsyntaxhighlight>
 
{{out}}
Line 10,054 ⟶ 10,636:
30
</pre>
 
=={{header|Yabasic}}==
{{trans|Lua}}
<syntaxhighlight lang="yabasic">
<lang Yabasic>
clear screen
 
Line 10,165 ⟶ 10,746:
 
print_cal(2018)
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
Gregorian calender only. Hard coded to a fit on a 80x43 terminal (somewhat half assed).
<langsyntaxhighlight lang="zkl">var [const] D=Time.Date, days="Su Mo Tu We Th Fr Sa";
 
fcn center(text,m) { String(" "*((m-text.len())/2),text) }
Line 10,194 ⟶ 10,774:
 
if(vm.numArgs){ y:=vm.nthArg(0).toInt(); oneYear(y,"").println(); }
else oneYear().println();</langsyntaxhighlight>
oneMonth produces a list of strings that make up a one month calender. Each day is three characters, the first line is padded with blanks to line it up with the 1st day of the week. The entire month is one long list of "days" that is then chunked into a list of weeks (one line per week). oneYear takes 3 months and builds the page line by line.
{{out}}
3

edits