Calendar: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 27:
{{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 250:
the [[Calendar_-_for_"real"_programmers#Ada]] task.
 
<langsyntaxhighlight lang=Ada>with Ada.Calendar.Formatting;
 
package Printable_Calendar is
Line 297:
end record;
 
end Printable_Calendar;</langsyntaxhighlight>
 
We continue with the implementation (printable_calendar.ads):
 
<langsyntaxhighlight lang=Ada>with Ada.Text_IO;
 
package body Printable_Calendar is
Line 476:
end Init_132;
 
end Printable_Calendar;</langsyntaxhighlight>
 
Now, the main program is really simple:
 
<langsyntaxhighlight lang=Ada>with Printable_Calendar;
 
procedure Cal is
Line 490:
C.New_Line;
C.Print(1969, "Nineteen-Sixty-Nine");
end Cal;</langsyntaxhighlight>
 
Here is the output:
Line 540:
{{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:
line printer width = 80; # as at 1969! #
print calendar(mankind stepped on the moon, line printer width)
)</langsyntaxhighlight>
Output:
<pre>
Line 696:
<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:
PRINT_YEAR
END
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 882:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>Calendar(Yr){
LastDay := [], Day := []
Titles =
Line 919:
Res:=RegExReplace(Res,"`am)(^|\s)\K0", " ")
return res
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight lang=AutoHotkey>Gui, font,s8, COURIER
Gui, add, edit, vYr w40 r1 Limit4 Number, 1969
Gui, add, edit, vEdit2 w580 r38
Line 934:
GuiClose:
ExitApp
return</langsyntaxhighlight>
Outputs:<pre> 1969
 
Line 974:
 
=={{header|AutoIt}}==
<langsyntaxhighlight lang=AutoIt>
#include <Date.au3>
 
Line 1,077:
ConsoleWrite($sLeft & $_sLine & $_sLF & @LF)
EndFunc ;==>_PrintLine
</syntaxhighlight>
</lang>
Output
<pre>
Line 1,122:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
 
Works with Gnu awk version 3.1.5 and with BusyBox v1.20.0.git awk
Line 1,223:
 
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,265:
=={{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:
PRINT day;
NEXT
NEXT</langsyntaxhighlight>
 
{{out}}
Line 1,321:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang=dos>::Calender Task from Rosetta Code Wiki
::Batch File Implementation
 
Line 1,413:
echo.
pause
endlocal</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,462:
{{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:
PRINT
NEXT
</syntaxhighlight>
</lang>
Output:
<pre> 1969
Line 1,541:
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:
> > $$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 lang=C>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,679:
bail: fprintf(stderr, "bad args\nUsage: %s year [-w width (>= 20)]\n", v[0]);
exit(1);
}</langsyntaxhighlight>
 
=={{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:
}
 
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang=cpp>
#include <windows.h>
#include <iostream>
Line 1,937:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,989:
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 2,114:
(println (center-string (str year) total-size))
(println)
(print-months (year-vector date) column-count margin))))</langsyntaxhighlight>
 
 
Line 2,163:
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.
<langsyntaxhighlight lang=COBOL>
IDENTIFICATION DIVISION.
PROGRAM-ID. CALEND.
Line 2,390:
.
END PROGRAM DATE2DOW.
</syntaxhighlight>
</lang>
Output (based on 80 character wide display)
<pre>
Line 2,432:
=={{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:
(lambda (&rest heads)
(format t format-string heads))
row))))</langsyntaxhighlight>
{{out}}
<pre>CL-USER> (print-calendar 1969)
Line 2,547:
 
=={{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:
void main() {
printCalendar(1969, 3);
}</langsyntaxhighlight>
 
<pre> [Snoopy Picture]
Line 2,633:
{{libheader| System.DateUtils}}
{{Trans|D}}
<langsyntaxhighlight lang=Delphi>
program Calendar;
 
Line 2,722:
printCalendar(1969, 4);
readln;
end.</langsyntaxhighlight>
{{out}}
<pre> [Snoopy Picture]
Line 2,755:
 
=={{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:
sb.ToString()
 
let printCalendar year = getCalendar year</langsyntaxhighlight>
 
{{out}}
Line 2,871:
 
=={{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:
: calendar-demo ( -- ) 1969 calendar ;
 
MAIN: calendar-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 2,924:
=={{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.
<langsyntaxhighlight lang=Fortran>
MODULE DATEGNASH !Assorted vexations. Time and calendar games, with local flavourings added.
 
Line 3,208:
END DO
END
</syntaxhighlight>
</lang>
Selected output, lacking alas the outbursts from Snoopy: three months /line
<pre>
Line 3,266:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>' version 17-02-2016
' compile with: fbc -s console
 
Line 3,390:
'Print : Print "hit any key to end program
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 1969
Line 3,412:
=={{header|FutureBasic}}==
Legacy version:
<langsyntaxhighlight lang=futurebasic>window 1, @"Calendar", (0, 0, 520, 520 )
 
Str255 a
Line 3,423:
close 1
 
HandleEvents</langsyntaxhighlight>
 
Modern version:
<langsyntaxhighlight lang=futurebasic>include "NSLog.incl"
 
local fn RunCommand( command as CFStringRef ) as CFStringRef
Line 3,442:
NSLog( @"%@", fn RunCommand( @"cal 1969" ) )
 
HandleEvents</langsyntaxhighlight>
 
Output of either version:
Line 3,483:
 
=={{header|Gambas}}==
<langsyntaxhighlight lang=gambas>Public Sub Main()
 
Shell "cal 1969"
 
End</langsyntaxhighlight>
Output:
<pre>
Line 3,529:
 
=={{header|Go}}==
<langsyntaxhighlight lang=Go>package main
 
import (
Line 3,599:
fmt.Println()
}
}</langsyntaxhighlight>
Output:
<pre> [SNOOPY]
Line 3,643:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=Haskell>import qualified Data.Text as T
import Data.Time
import Data.Time.Calendar
Line 3,732:
calcol' = calColFromCol columns
 
columns' = colFromCalCol calcol'</langsyntaxhighlight>
<pre>*Main> printCalendar 1969 80
[Maybe Snoopy]
Line 3,771:
 
=={{header|Huginn}}==
<langsyntaxhighlight lang=huginn>import DateTime as dt;
import Algorithms as algo;
import Text as text;
Line 3,831:
cols
);
}</langsyntaxhighlight>
 
Output: <pre> 1969
Line 3,870:
=={{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 lang=Icon>procedure main(A)
printCalendar(\A[1]|1969)
end
Line 3,914:
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,957:
=={{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 4,001:
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,061:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,105:
 
=={{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,226:
 
cal(1969, 'en-US', 3);
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,271:
 
=={{header|Julia}}==
<langsyntaxhighlight lang=julia>
using Dates
Line 4,336:
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,394:
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,459:
monthname$=word$("January February March April May June July August September October November December",month)
end function
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,524:
 
=={{header|Lingo}}==
<langsyntaxhighlight lang=lingo>----------------------------------------
-- @desc Class "Calendar"
-- @file parent script "Calendar"
Line 4,580:
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,628:
 
=={{header|Lua}}==
<langsyntaxhighlight lang=Lua>function print_cal(year)
local months={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE",
"JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"}
Line 4,690:
end
 
print_cal(1969)</langsyntaxhighlight>
{{out}}
<pre>
Line 4,740:
Module Calendar get the Year and the Language Id (1033 for English, 1032 for Greek and any other which support the Window OS).
 
<langsyntaxhighlight lang=M2000 Interpreter>
Module Calendar (Year, LocaleId) {
Function GetMax(Year, Month) {
Line 4,808:
k=Key$ ' wait key
Calendar 2018, 1032 ' Greek
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 4,833:
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 lang=Mathematica>DataGrid[
rowHeights:{__Integer},
colWidths:{__Integer},
Line 4,881:
 
(*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 lang=Mathematica>DataA={{"A"}};
GridA = DataGrid[{2},{5},{0,0},{{1,1},{1,1}},<|"border"->"*","alignment"->{1,-1}|>,{DataA}];
DataB = {{"B"},{"B"}};
Line 4,893:
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,923:
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 lang=Mathematica>HeadedGrid[
finalSpacings:{_Integer,_Integer},
finalBorderWidths:{{_Integer,_Integer},{_Integer,_Integer}},
Line 4,945:
{"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,976:
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 lang=Mathematica>MonthGrid[
finalSpacings:{_Integer,_Integer},
finalBorderWidths:{{_Integer,_Integer},{_Integer,_Integer}},
Line 5,001:
{"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 5,018:
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 lang=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,029:
 
(*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,062:
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 lang=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,070:
 
(*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 lang=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,096:
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,154:
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 lang=Mathematica>Dates1582France=
Join[
DateRange[DateObject[{1582,1,1},CalendarType->"Julian"],DateObject[{1582,10,4},CalendarType->"Julian"],CalendarType->"Julian"],
Line 5,183:
{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,246:
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 lang=Mathematica>WidestFitDimensions[
targetWidth_Integer,
columnSpacings_Integer,
Line 5,263:
{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 lang=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 lang=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 lang=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>
Line 5,281:
=={{header|Nim}}==
{{trans|D}}
<langsyntaxhighlight lang=nim>import times
import strformat
 
Line 5,318:
echo ""
 
printCalendar(1969, 3)</langsyntaxhighlight>
{{out}}
<pre>
Line 5,365:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang=ocaml>#load "unix.cma"
 
let lang = "en" (* language: English *)
Line 5,560:
| _ ->
usage ()</langsyntaxhighlight>
 
{{out}}
Line 5,603:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>#!/usr/bin/perl -l
 
use strict; # https://rosettacode.org/wiki/Calendar
Line 5,635:
my ($string, $w) = @_;
sprintf "%${w}s", $string . ' ' x ($w - length($string) >> 1);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 5,699:
=={{header|Phix}}==
Gregorian calender only.
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">year</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1969</span>
Line 5,772:
<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>
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 5,831:
=={{header|Phixmonti}}==
{{trans|Lua}}
<langsyntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt
 
32 var space
Line 5,950:
enddef
 
2020 print_cal</langsyntaxhighlight>
{{out}}
<pre>
Line 6,022:
=={{header|PicoLisp}}==
This "calendar" is nicely formatted, and fits into 20 columns ;-)
<langsyntaxhighlight lang=PicoLisp>(de cal (Year)
(prinl "====== " Year " ======")
(for Dat (range (date Year 1 1) (date Year 12 31))
Line 6,034:
(pack "Week " (week Dat)) ) ) ) ) )
 
(cal 1969)</langsyntaxhighlight>
Output:
<pre>====== 1969 ======
Line 6,068:
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 lang=Pike>#!/bin/env pike
 
int main(int argc, array(string) argv)
Line 6,187:
return dayname;
}
</syntaxhighlight>
</lang>
Output: (holidays lost in copy-paste)
<pre>
Line 6,227:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang=PL/I>
calendar: procedure (year) options (main);
declare year character (4) varying;
Line 6,279:
 
end calendar;
</syntaxhighlight>
</lang>
Output:
<pre>
Line 6,333:
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang=PowerShell>
Param([int]$Year = 1969)
Begin {
Line 6,370:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Line 6,376:
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,469:
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,516:
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|Racket}}==
<langsyntaxhighlight lang=racket>#lang racket
(require racket/date net/base64 file/gunzip)
(define (calendar yr)
Line 6,550:
(λ(s) (equal? "" s)))))
 
(calendar 1969)</langsyntaxhighlight>
 
{{out}}
Line 6,605:
=={{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,633:
((' ' 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>
do [if "" = y: ask "Year (ENTER for current):^/^/" [prin y: now/year]
foreach m system/locale/months [
Line 6,649:
]
] ask "^/^/Press [ENTER] to Continue..."]
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
Line 6,692:
<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,838:
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,935:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
# Project : Calendar
 
Line 7,092:
next
next
</syntaxhighlight>
</lang>
Output image:
 
Line 7,100:
<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,169:
80; end; end; end
 
puts cal(Integer(ARGV[0]), columns)</langsyntaxhighlight>
 
Here is 1969 in 132 columns.
Line 7,231:
 
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>// Assume your binary name is 'calendar'.
// Command line:
// >>$ calendar 2019 150
Line 7,329:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
Line 7,337:
 
[[Category:Scala examples needing attention]]
<langsyntaxhighlight lang=Scala>import java.util.{ Calendar, GregorianCalendar }
import language.postfixOps
import collection.mutable.ListBuffer
Line 7,489:
printCalendar(getGregCal(1969), List("[Snoopy Picture]", "1969"))
printCalendar(getGregCal(1582), List("[Snoopy Picture]", "1582"), printerWidth = 132)
}</langsyntaxhighlight>
 
{{out}}
Line 7,559:
{{works with|Scala|2.10.1}}
 
<langsyntaxhighlight lang=Scala>/**
* Loosely based on the Ruby implementation.
*
Line 7,608:
}
 
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang=Scala>/**
* This provides extra classes needed for the main
* algorithm.
Line 7,662:
}
 
}</langsyntaxhighlight>
 
Sample output for 1792:
Line 7,696:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "time.s7i";
 
Line 7,748:
begin
printCalendar(1969, 3);
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/date.htm#calendar]
Line 7,797:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang=ruby>require('DateTime')
 
define months_per_col = 3
Line 7,847:
}
 
print fmt_year(ARGV ? Number(ARGV[0]) : 1969)</langsyntaxhighlight>
{{out}}
<pre>
Line 7,888:
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,031:
PRINT_YEAR;
END;
END;</langsyntaxhighlight>
{{out}}
<pre>
Line 8,076:
 
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>
<langsyntaxhighlight lang=Smalltalk>
"Instance Variables:
- yearToPrint: The year to print the calendar of
Line 8,214:
"Returns the number of character per month"
^20
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 8,252:
 
=={{header|SPL}}==
<langsyntaxhighlight lang=spl>year = 1969
#.output(#.str(year,">68<"))
> row, 0..3
Line 8,288:
<
<= lines
.</langsyntaxhighlight>
{{out}}
<pre>
Line 8,327:
 
=={{header|Swift}}==
<langsyntaxhighlight lang=swift>import Foundation
 
let monthWidth = 20
Line 8,419:
}
 
printCalendar(year: 1969, width: 80)</langsyntaxhighlight>
 
{{out}}
Line 8,463:
=={{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,537:
 
snoopy
cal</langsyntaxhighlight>
Which produces this output:
<pre>
Line 8,576:
</pre>
If a different year is chosen, it's printed…
<langsyntaxhighlight lang=tcl>snoopy
cal 1969</langsyntaxhighlight>
<pre>
[Snoopy Picture]
Line 8,614:
</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,655:
=={{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,698:
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,827:
010 Print " October "; : Return
011 Print " November"; : Return
012 Print " December"; : Return</langsyntaxhighlight>
Output (missing Snoopy):
<pre>Year to print: 1969
Line 8,854:
=={{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,904:
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,924:
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,985:
 
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,993:
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,033:
End If
End Sub
End Module</langsyntaxhighlight>
 
Program:
<langsyntaxhighlight lang=vbnet>Module Program
Sub Main(args As String())
Dim dt As Date
Line 9,320:
End Function
End Module
</syntaxhighlight>
</lang>
 
====Command-line arguments:====
Line 9,568:
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>
<lang vb>
'call it with year, number of months per row (1,2,3,4,6) and locale ("" for default)
docal 1969,6,""
Line 9,643:
print string(ncols,"=")
end sub
</syntaxhighlight>
</lang>
output
<pre>
Line 9,725:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang=ecmascript>import "/date" for Date
import "/fmt" for Fmt
import "/seq" for Lst
Line 9,772:
}
 
calendar.call(1969)</langsyntaxhighlight>
 
{{out}}
Line 9,818:
=={{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,875:
(print-calendar months-list (gauss-algorithm year)))
 
(calendar 1969)</langsyntaxhighlight>
{{out}}
<pre> ******** SNOOPY CALENDAR 1969 ********
Line 9,993:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=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,051:
];
 
Calendar(1969)</langsyntaxhighlight>
 
{{out}}
Line 10,097:
=={{header|Yabasic}}==
{{trans|Lua}}
<langsyntaxhighlight lang=Yabasic>
clear screen
 
Line 10,205:
 
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,234:
 
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}}
10,333

edits