summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fcalendar.html
blob: c131f5785f39ea5027e421311688f3991acd25e4 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
 <span id="calendar-general-calendar-related-functions"></span><h1>calendar — General calendar-related functions</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/calendar.py">Lib/calendar.py</a></p>  <p>This module allows you to output calendars like the Unix <strong class="program">cal</strong> program, and provides additional useful functions related to the calendar. By default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention). Use <a class="reference internal" href="#calendar.setfirstweekday" title="calendar.setfirstweekday"><code>setfirstweekday()</code></a> to set the first day of the week to Sunday (6) or to any other weekday. Parameters that specify dates are given as integers. For related functionality, see also the <a class="reference internal" href="datetime#module-datetime" title="datetime: Basic date and time types."><code>datetime</code></a> and <a class="reference internal" href="time#module-time" title="time: Time access and conversions."><code>time</code></a> modules.</p> <p>The functions and classes defined in this module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. This matches the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold’s book “Calendrical Calculations”, where it’s the base calendar for all computations. Zero and negative years are interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is 2 BC, and so on.</p> <dl class="py class"> <dt class="sig sig-object py" id="calendar.Calendar">
<code>class calendar.Calendar(firstweekday=0)</code> </dt> <dd>
<p>Creates a <a class="reference internal" href="#calendar.Calendar" title="calendar.Calendar"><code>Calendar</code></a> object. <em>firstweekday</em> is an integer specifying the first day of the week. <a class="reference internal" href="#calendar.MONDAY" title="calendar.MONDAY"><code>MONDAY</code></a> is <code>0</code> (the default), <a class="reference internal" href="#calendar.SUNDAY" title="calendar.SUNDAY"><code>SUNDAY</code></a> is <code>6</code>.</p> <p>A <a class="reference internal" href="#calendar.Calendar" title="calendar.Calendar"><code>Calendar</code></a> object provides several methods that can be used for preparing the calendar data for formatting. This class doesn’t do any formatting itself. This is the job of subclasses.</p> <p><a class="reference internal" href="#calendar.Calendar" title="calendar.Calendar"><code>Calendar</code></a> instances have the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.iterweekdays">
<code>iterweekdays()</code> </dt> <dd>
<p>Return an iterator for the week day numbers that will be used for one week. The first value from the iterator will be the same as the value of the <a class="reference internal" href="#calendar.firstweekday" title="calendar.firstweekday"><code>firstweekday</code></a> property.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.itermonthdates">
<code>itermonthdates(year, month)</code> </dt> <dd>
<p>Return an iterator for the month <em>month</em> (1–12) in the year <em>year</em>. This iterator will return all days (as <a class="reference internal" href="datetime#datetime.date" title="datetime.date"><code>datetime.date</code></a> objects) for the month and all days before the start of the month or after the end of the month that are required to get a complete week.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.itermonthdays">
<code>itermonthdays(year, month)</code> </dt> <dd>
<p>Return an iterator for the month <em>month</em> in the year <em>year</em> similar to <a class="reference internal" href="#calendar.Calendar.itermonthdates" title="calendar.Calendar.itermonthdates"><code>itermonthdates()</code></a>, but not restricted by the <a class="reference internal" href="datetime#datetime.date" title="datetime.date"><code>datetime.date</code></a> range. Days returned will simply be day of the month numbers. For the days outside of the specified month, the day number is <code>0</code>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.itermonthdays2">
<code>itermonthdays2(year, month)</code> </dt> <dd>
<p>Return an iterator for the month <em>month</em> in the year <em>year</em> similar to <a class="reference internal" href="#calendar.Calendar.itermonthdates" title="calendar.Calendar.itermonthdates"><code>itermonthdates()</code></a>, but not restricted by the <a class="reference internal" href="datetime#datetime.date" title="datetime.date"><code>datetime.date</code></a> range. Days returned will be tuples consisting of a day of the month number and a week day number.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.itermonthdays3">
<code>itermonthdays3(year, month)</code> </dt> <dd>
<p>Return an iterator for the month <em>month</em> in the year <em>year</em> similar to <a class="reference internal" href="#calendar.Calendar.itermonthdates" title="calendar.Calendar.itermonthdates"><code>itermonthdates()</code></a>, but not restricted by the <a class="reference internal" href="datetime#datetime.date" title="datetime.date"><code>datetime.date</code></a> range. Days returned will be tuples consisting of a year, a month and a day of the month numbers.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.itermonthdays4">
<code>itermonthdays4(year, month)</code> </dt> <dd>
<p>Return an iterator for the month <em>month</em> in the year <em>year</em> similar to <a class="reference internal" href="#calendar.Calendar.itermonthdates" title="calendar.Calendar.itermonthdates"><code>itermonthdates()</code></a>, but not restricted by the <a class="reference internal" href="datetime#datetime.date" title="datetime.date"><code>datetime.date</code></a> range. Days returned will be tuples consisting of a year, a month, a day of the month, and a day of the week numbers.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.monthdatescalendar">
<code>monthdatescalendar(year, month)</code> </dt> <dd>
<p>Return a list of the weeks in the month <em>month</em> of the <em>year</em> as full weeks. Weeks are lists of seven <a class="reference internal" href="datetime#datetime.date" title="datetime.date"><code>datetime.date</code></a> objects.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.monthdays2calendar">
<code>monthdays2calendar(year, month)</code> </dt> <dd>
<p>Return a list of the weeks in the month <em>month</em> of the <em>year</em> as full weeks. Weeks are lists of seven tuples of day numbers and weekday numbers.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.monthdayscalendar">
<code>monthdayscalendar(year, month)</code> </dt> <dd>
<p>Return a list of the weeks in the month <em>month</em> of the <em>year</em> as full weeks. Weeks are lists of seven day numbers.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.yeardatescalendar">
<code>yeardatescalendar(year, width=3)</code> </dt> <dd>
<p>Return the data for the specified year ready for formatting. The return value is a list of month rows. Each month row contains up to <em>width</em> months (defaulting to 3). Each month contains between 4 and 6 weeks and each week contains 1–7 days. Days are <a class="reference internal" href="datetime#datetime.date" title="datetime.date"><code>datetime.date</code></a> objects.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.yeardays2calendar">
<code>yeardays2calendar(year, width=3)</code> </dt> <dd>
<p>Return the data for the specified year ready for formatting (similar to <a class="reference internal" href="#calendar.Calendar.yeardatescalendar" title="calendar.Calendar.yeardatescalendar"><code>yeardatescalendar()</code></a>). Entries in the week lists are tuples of day numbers and weekday numbers. Day numbers outside this month are zero.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.Calendar.yeardayscalendar">
<code>yeardayscalendar(year, width=3)</code> </dt> <dd>
<p>Return the data for the specified year ready for formatting (similar to <a class="reference internal" href="#calendar.Calendar.yeardatescalendar" title="calendar.Calendar.yeardatescalendar"><code>yeardatescalendar()</code></a>). Entries in the week lists are day numbers. Day numbers outside this month are zero.</p> </dd>
</dl> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="calendar.TextCalendar">
<code>class calendar.TextCalendar(firstweekday=0)</code> </dt> <dd>
<p>This class can be used to generate plain text calendars.</p> <p><a class="reference internal" href="#calendar.TextCalendar" title="calendar.TextCalendar"><code>TextCalendar</code></a> instances have the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="calendar.TextCalendar.formatmonth">
<code>formatmonth(theyear, themonth, w=0, l=0)</code> </dt> <dd>
<p>Return a month’s calendar in a multi-line string. If <em>w</em> is provided, it specifies the width of the date columns, which are centered. If <em>l</em> is given, it specifies the number of lines that each week will use. Depends on the first weekday as specified in the constructor or set by the <a class="reference internal" href="#calendar.setfirstweekday" title="calendar.setfirstweekday"><code>setfirstweekday()</code></a> method.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.TextCalendar.prmonth">
<code>prmonth(theyear, themonth, w=0, l=0)</code> </dt> <dd>
<p>Print a month’s calendar as returned by <a class="reference internal" href="#calendar.TextCalendar.formatmonth" title="calendar.TextCalendar.formatmonth"><code>formatmonth()</code></a>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.TextCalendar.formatyear">
<code>formatyear(theyear, w=2, l=1, c=6, m=3)</code> </dt> <dd>
<p>Return a <em>m</em>-column calendar for an entire year as a multi-line string. Optional parameters <em>w</em>, <em>l</em>, and <em>c</em> are for date column width, lines per week, and number of spaces between month columns, respectively. Depends on the first weekday as specified in the constructor or set by the <a class="reference internal" href="#calendar.setfirstweekday" title="calendar.setfirstweekday"><code>setfirstweekday()</code></a> method. The earliest year for which a calendar can be generated is platform-dependent.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.TextCalendar.pryear">
<code>pryear(theyear, w=2, l=1, c=6, m=3)</code> </dt> <dd>
<p>Print the calendar for an entire year as returned by <a class="reference internal" href="#calendar.TextCalendar.formatyear" title="calendar.TextCalendar.formatyear"><code>formatyear()</code></a>.</p> </dd>
</dl> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="calendar.HTMLCalendar">
<code>class calendar.HTMLCalendar(firstweekday=0)</code> </dt> <dd>
<p>This class can be used to generate HTML calendars.</p> <p><code>HTMLCalendar</code> instances have the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.formatmonth">
<code>formatmonth(theyear, themonth, withyear=True)</code> </dt> <dd>
<p>Return a month’s calendar as an HTML table. If <em>withyear</em> is true the year will be included in the header, otherwise just the month name will be used.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.formatyear">
<code>formatyear(theyear, width=3)</code> </dt> <dd>
<p>Return a year’s calendar as an HTML table. <em>width</em> (defaulting to 3) specifies the number of months per row.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.formatyearpage">
<code>formatyearpage(theyear, width=3, css='calendar.css', encoding=None)</code> </dt> <dd>
<p>Return a year’s calendar as a complete HTML page. <em>width</em> (defaulting to 3) specifies the number of months per row. <em>css</em> is the name for the cascading style sheet to be used. <a class="reference internal" href="constants#None" title="None"><code>None</code></a> can be passed if no style sheet should be used. <em>encoding</em> specifies the encoding to be used for the output (defaulting to the system default encoding).</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.formatmonthname">
<code>formatmonthname(theyear, themonth, withyear=True)</code> </dt> <dd>
<p>Return a month name as an HTML table row. If <em>withyear</em> is true the year will be included in the row, otherwise just the month name will be used.</p> </dd>
</dl> <p><code>HTMLCalendar</code> has the following attributes you can override to customize the CSS classes used by the calendar:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.cssclasses">
<code>cssclasses</code> </dt> <dd>
<p>A list of CSS classes used for each weekday. The default class list is:</p> <pre data-language="python">cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
</pre> <p>more styles can be added for each day:</p> <pre data-language="python">cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
</pre> <p>Note that the length of this list must be seven items.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.cssclass_noday">
<code>cssclass_noday</code> </dt> <dd>
<p>The CSS class for a weekday occurring in the previous or coming month.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.cssclasses_weekday_head">
<code>cssclasses_weekday_head</code> </dt> <dd>
<p>A list of CSS classes used for weekday names in the header row. The default is the same as <a class="reference internal" href="#calendar.HTMLCalendar.cssclasses" title="calendar.HTMLCalendar.cssclasses"><code>cssclasses</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.cssclass_month_head">
<code>cssclass_month_head</code> </dt> <dd>
<p>The month’s head CSS class (used by <a class="reference internal" href="#calendar.HTMLCalendar.formatmonthname" title="calendar.HTMLCalendar.formatmonthname"><code>formatmonthname()</code></a>). The default value is <code>"month"</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.cssclass_month">
<code>cssclass_month</code> </dt> <dd>
<p>The CSS class for the whole month’s table (used by <a class="reference internal" href="#calendar.HTMLCalendar.formatmonth" title="calendar.HTMLCalendar.formatmonth"><code>formatmonth()</code></a>). The default value is <code>"month"</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.cssclass_year">
<code>cssclass_year</code> </dt> <dd>
<p>The CSS class for the whole year’s table of tables (used by <a class="reference internal" href="#calendar.HTMLCalendar.formatyear" title="calendar.HTMLCalendar.formatyear"><code>formatyear()</code></a>). The default value is <code>"year"</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.HTMLCalendar.cssclass_year_head">
<code>cssclass_year_head</code> </dt> <dd>
<p>The CSS class for the table head for the whole year (used by <a class="reference internal" href="#calendar.HTMLCalendar.formatyear" title="calendar.HTMLCalendar.formatyear"><code>formatyear()</code></a>). The default value is <code>"year"</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <p>Note that although the naming for the above described class attributes is singular (e.g. <code>cssclass_month</code> <code>cssclass_noday</code>), one can replace the single CSS class with a space separated list of CSS classes, for example:</p> <pre data-language="python">"text-bold text-red"
</pre> <p>Here is an example how <code>HTMLCalendar</code> can be customized:</p> <pre data-language="python">class CustomHTMLCal(calendar.HTMLCalendar):
    cssclasses = [style + " text-nowrap" for style in
                  calendar.HTMLCalendar.cssclasses]
    cssclass_month_head = "text-center month-head"
    cssclass_month = "text-center month"
    cssclass_year = "text-italic lead"
</pre> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="calendar.LocaleTextCalendar">
<code>class calendar.LocaleTextCalendar(firstweekday=0, locale=None)</code> </dt> <dd>
<p>This subclass of <a class="reference internal" href="#calendar.TextCalendar" title="calendar.TextCalendar"><code>TextCalendar</code></a> can be passed a locale name in the constructor and will return month and weekday names in the specified locale.</p> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="calendar.LocaleHTMLCalendar">
<code>class calendar.LocaleHTMLCalendar(firstweekday=0, locale=None)</code> </dt> <dd>
<p>This subclass of <a class="reference internal" href="#calendar.HTMLCalendar" title="calendar.HTMLCalendar"><code>HTMLCalendar</code></a> can be passed a locale name in the constructor and will return month and weekday names in the specified locale.</p> </dd>
</dl> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The constructor, <code>formatweekday()</code> and <code>formatmonthname()</code> methods of these two classes temporarily change the <code>LC_TIME</code> locale to the given <em>locale</em>. Because the current locale is a process-wide setting, they are not thread-safe.</p> </div> <p>For simple text calendars this module provides the following functions.</p> <dl class="py function"> <dt class="sig sig-object py" id="calendar.setfirstweekday">
<code>calendar.setfirstweekday(weekday)</code> </dt> <dd>
<p>Sets the weekday (<code>0</code> is Monday, <code>6</code> is Sunday) to start each week. The values <a class="reference internal" href="#calendar.MONDAY" title="calendar.MONDAY"><code>MONDAY</code></a>, <a class="reference internal" href="#calendar.TUESDAY" title="calendar.TUESDAY"><code>TUESDAY</code></a>, <a class="reference internal" href="#calendar.WEDNESDAY" title="calendar.WEDNESDAY"><code>WEDNESDAY</code></a>, <a class="reference internal" href="#calendar.THURSDAY" title="calendar.THURSDAY"><code>THURSDAY</code></a>, <a class="reference internal" href="#calendar.FRIDAY" title="calendar.FRIDAY"><code>FRIDAY</code></a>, <a class="reference internal" href="#calendar.SATURDAY" title="calendar.SATURDAY"><code>SATURDAY</code></a>, and <a class="reference internal" href="#calendar.SUNDAY" title="calendar.SUNDAY"><code>SUNDAY</code></a> are provided for convenience. For example, to set the first weekday to Sunday:</p> <pre data-language="python">import calendar
calendar.setfirstweekday(calendar.SUNDAY)
</pre> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.firstweekday">
<code>calendar.firstweekday()</code> </dt> <dd>
<p>Returns the current setting for the weekday to start each week.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.isleap">
<code>calendar.isleap(year)</code> </dt> <dd>
<p>Returns <a class="reference internal" href="constants#True" title="True"><code>True</code></a> if <em>year</em> is a leap year, otherwise <a class="reference internal" href="constants#False" title="False"><code>False</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.leapdays">
<code>calendar.leapdays(y1, y2)</code> </dt> <dd>
<p>Returns the number of leap years in the range from <em>y1</em> to <em>y2</em> (exclusive), where <em>y1</em> and <em>y2</em> are years.</p> <p>This function works for ranges spanning a century change.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.weekday">
<code>calendar.weekday(year, month, day)</code> </dt> <dd>
<p>Returns the day of the week (<code>0</code> is Monday) for <em>year</em> (<code>1970</code>–…), <em>month</em> (<code>1</code>–<code>12</code>), <em>day</em> (<code>1</code>–<code>31</code>).</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.weekheader">
<code>calendar.weekheader(n)</code> </dt> <dd>
<p>Return a header containing abbreviated weekday names. <em>n</em> specifies the width in characters for one weekday.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.monthrange">
<code>calendar.monthrange(year, month)</code> </dt> <dd>
<p>Returns weekday of first day of the month and number of days in month, for the specified <em>year</em> and <em>month</em>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.monthcalendar">
<code>calendar.monthcalendar(year, month)</code> </dt> <dd>
<p>Returns a matrix representing a month’s calendar. Each row represents a week; days outside of the month are represented by zeros. Each week begins with Monday unless set by <a class="reference internal" href="#calendar.setfirstweekday" title="calendar.setfirstweekday"><code>setfirstweekday()</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.prmonth">
<code>calendar.prmonth(theyear, themonth, w=0, l=0)</code> </dt> <dd>
<p>Prints a month’s calendar as returned by <a class="reference internal" href="#calendar.month" title="calendar.month"><code>month()</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.month">
<code>calendar.month(theyear, themonth, w=0, l=0)</code> </dt> <dd>
<p>Returns a month’s calendar in a multi-line string using the <a class="reference internal" href="#calendar.TextCalendar.formatmonth" title="calendar.TextCalendar.formatmonth"><code>formatmonth()</code></a> of the <a class="reference internal" href="#calendar.TextCalendar" title="calendar.TextCalendar"><code>TextCalendar</code></a> class.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.prcal">
<code>calendar.prcal(year, w=0, l=0, c=6, m=3)</code> </dt> <dd>
<p>Prints the calendar for an entire year as returned by <a class="reference internal" href="#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code>calendar()</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.calendar">
<code>calendar.calendar(year, w=2, l=1, c=6, m=3)</code> </dt> <dd>
<p>Returns a 3-column calendar for an entire year as a multi-line string using the <a class="reference internal" href="#calendar.TextCalendar.formatyear" title="calendar.TextCalendar.formatyear"><code>formatyear()</code></a> of the <a class="reference internal" href="#calendar.TextCalendar" title="calendar.TextCalendar"><code>TextCalendar</code></a> class.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="calendar.timegm">
<code>calendar.timegm(tuple)</code> </dt> <dd>
<p>An unrelated but handy function that takes a time tuple such as returned by the <a class="reference internal" href="time#time.gmtime" title="time.gmtime"><code>gmtime()</code></a> function in the <a class="reference internal" href="time#module-time" title="time: Time access and conversions."><code>time</code></a> module, and returns the corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In fact, <a class="reference internal" href="time#time.gmtime" title="time.gmtime"><code>time.gmtime()</code></a> and <a class="reference internal" href="#calendar.timegm" title="calendar.timegm"><code>timegm()</code></a> are each others’ inverse.</p> </dd>
</dl> <p>The <a class="reference internal" href="#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code>calendar</code></a> module exports the following data attributes:</p> <dl class="py data"> <dt class="sig sig-object py" id="calendar.day_name">
<code>calendar.day_name</code> </dt> <dd>
<p>An array that represents the days of the week in the current locale.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="calendar.day_abbr">
<code>calendar.day_abbr</code> </dt> <dd>
<p>An array that represents the abbreviated days of the week in the current locale.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="calendar.MONDAY">
<code>calendar.MONDAY</code> </dt> <dt class="sig sig-object py" id="calendar.TUESDAY">
<code>calendar.TUESDAY</code> </dt> <dt class="sig sig-object py" id="calendar.WEDNESDAY">
<code>calendar.WEDNESDAY</code> </dt> <dt class="sig sig-object py" id="calendar.THURSDAY">
<code>calendar.THURSDAY</code> </dt> <dt class="sig sig-object py" id="calendar.FRIDAY">
<code>calendar.FRIDAY</code> </dt> <dt class="sig sig-object py" id="calendar.SATURDAY">
<code>calendar.SATURDAY</code> </dt> <dt class="sig sig-object py" id="calendar.SUNDAY">
<code>calendar.SUNDAY</code> </dt> <dd>
<p>Aliases for the days of the week, where <code>MONDAY</code> is <code>0</code> and <code>SUNDAY</code> is <code>6</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="calendar.Day">
<code>class calendar.Day</code> </dt> <dd>
<p>Enumeration defining days of the week as integer constants. The members of this enumeration are exported to the module scope as <a class="reference internal" href="#calendar.MONDAY" title="calendar.MONDAY"><code>MONDAY</code></a> through <a class="reference internal" href="#calendar.SUNDAY" title="calendar.SUNDAY"><code>SUNDAY</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="calendar.month_name">
<code>calendar.month_name</code> </dt> <dd>
<p>An array that represents the months of the year in the current locale. This follows normal convention of January being month number 1, so it has a length of 13 and <code>month_name[0]</code> is the empty string.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="calendar.month_abbr">
<code>calendar.month_abbr</code> </dt> <dd>
<p>An array that represents the abbreviated months of the year in the current locale. This follows normal convention of January being month number 1, so it has a length of 13 and <code>month_abbr[0]</code> is the empty string.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="calendar.JANUARY">
<code>calendar.JANUARY</code> </dt> <dt class="sig sig-object py" id="calendar.FEBRUARY">
<code>calendar.FEBRUARY</code> </dt> <dt class="sig sig-object py" id="calendar.MARCH">
<code>calendar.MARCH</code> </dt> <dt class="sig sig-object py" id="calendar.APRIL">
<code>calendar.APRIL</code> </dt> <dt class="sig sig-object py" id="calendar.MAY">
<code>calendar.MAY</code> </dt> <dt class="sig sig-object py" id="calendar.JUNE">
<code>calendar.JUNE</code> </dt> <dt class="sig sig-object py" id="calendar.JULY">
<code>calendar.JULY</code> </dt> <dt class="sig sig-object py" id="calendar.AUGUST">
<code>calendar.AUGUST</code> </dt> <dt class="sig sig-object py" id="calendar.SEPTEMBER">
<code>calendar.SEPTEMBER</code> </dt> <dt class="sig sig-object py" id="calendar.OCTOBER">
<code>calendar.OCTOBER</code> </dt> <dt class="sig sig-object py" id="calendar.NOVEMBER">
<code>calendar.NOVEMBER</code> </dt> <dt class="sig sig-object py" id="calendar.DECEMBER">
<code>calendar.DECEMBER</code> </dt> <dd>
<p>Aliases for the months of the year, where <code>JANUARY</code> is <code>1</code> and <code>DECEMBER</code> is <code>12</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="calendar.Month">
<code>class calendar.Month</code> </dt> <dd>
<p>Enumeration defining months of the year as integer constants. The members of this enumeration are exported to the module scope as <a class="reference internal" href="#calendar.JANUARY" title="calendar.JANUARY"><code>JANUARY</code></a> through <a class="reference internal" href="#calendar.DECEMBER" title="calendar.DECEMBER"><code>DECEMBER</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <p>The <a class="reference internal" href="#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code>calendar</code></a> module defines the following exceptions:</p> <dl class="py exception"> <dt class="sig sig-object py" id="calendar.IllegalMonthError">
<code>exception calendar.IllegalMonthError(month)</code> </dt> <dd>
<p>A subclass of <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>, raised when the given month number is outside of the range 1-12 (inclusive).</p> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.IllegalMonthError.month">
<code>month</code> </dt> <dd>
<p>The invalid month number.</p> </dd>
</dl> </dd>
</dl> <dl class="py exception"> <dt class="sig sig-object py" id="calendar.IllegalWeekdayError">
<code>exception calendar.IllegalWeekdayError(weekday)</code> </dt> <dd>
<p>A subclass of <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>, raised when the given weekday number is outside of the range 0-6 (inclusive).</p> <dl class="py attribute"> <dt class="sig sig-object py" id="calendar.IllegalWeekdayError.weekday">
<code>weekday</code> </dt> <dd>
<p>The invalid weekday number.</p> </dd>
</dl> </dd>
</dl> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt>
<code>Module</code> <a class="reference internal" href="datetime#module-datetime" title="datetime: Basic date and time types."><code>datetime</code></a>
</dt>
<dd>
<p>Object-oriented interface to dates and times with similar functionality to the <a class="reference internal" href="time#module-time" title="time: Time access and conversions."><code>time</code></a> module.</p> </dd> <dt>
<code>Module</code> <a class="reference internal" href="time#module-time" title="time: Time access and conversions."><code>time</code></a>
</dt>
<dd>
<p>Low-level time related functions.</p> </dd> </dl> </div> <section id="command-line-usage"> <span id="calendar-cli"></span><h2>Command-Line Usage</h2> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.5.</span></p> </div> <p>The <a class="reference internal" href="#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code>calendar</code></a> module can be executed as a script from the command line to interactively print a calendar.</p> <pre data-language="shell">python -m calendar [-h] [-L LOCALE] [-e ENCODING] [-t {text,html}]
                   [-w WIDTH] [-l LINES] [-s SPACING] [-m MONTHS] [-c CSS]
                   [year] [month]
</pre> <p>For example, to print a calendar for the year 2000:</p> <pre data-language="console">$ python -m calendar 2000
                                  2000

      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          1  2  3  4  5  6             1  2  3  4  5
 3  4  5  6  7  8  9       7  8  9 10 11 12 13       6  7  8  9 10 11 12
10 11 12 13 14 15 16      14 15 16 17 18 19 20      13 14 15 16 17 18 19
17 18 19 20 21 22 23      21 22 23 24 25 26 27      20 21 22 23 24 25 26
24 25 26 27 28 29 30      28 29                     27 28 29 30 31
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       1  2  3  4  5  6  7                1  2  3  4
 3  4  5  6  7  8  9       8  9 10 11 12 13 14       5  6  7  8  9 10 11
10 11 12 13 14 15 16      15 16 17 18 19 20 21      12 13 14 15 16 17 18
17 18 19 20 21 22 23      22 23 24 25 26 27 28      19 20 21 22 23 24 25
24 25 26 27 28 29 30      29 30 31                  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          1  2  3  4  5  6                   1  2  3
 3  4  5  6  7  8  9       7  8  9 10 11 12 13       4  5  6  7  8  9 10
10 11 12 13 14 15 16      14 15 16 17 18 19 20      11 12 13 14 15 16 17
17 18 19 20 21 22 23      21 22 23 24 25 26 27      18 19 20 21 22 23 24
24 25 26 27 28 29 30      28 29 30 31               25 26 27 28 29 30
31

      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             1  2  3  4  5                   1  2  3
 2  3  4  5  6  7  8       6  7  8  9 10 11 12       4  5  6  7  8  9 10
 9 10 11 12 13 14 15      13 14 15 16 17 18 19      11 12 13 14 15 16 17
16 17 18 19 20 21 22      20 21 22 23 24 25 26      18 19 20 21 22 23 24
23 24 25 26 27 28 29      27 28 29 30               25 26 27 28 29 30 31
30 31
</pre> <p>The following options are accepted:</p> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-help">
<code>--help, -h</code> </dt> <dd>
<p>Show the help message and exit.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-locale">
<code>--locale LOCALE, -L LOCALE</code> </dt> <dd>
<p>The locale to use for month and weekday names. Defaults to English.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-encoding">
<code>--encoding ENCODING, -e ENCODING</code> </dt> <dd>
<p>The encoding to use for output. <a class="reference internal" href="#cmdoption-calendar-encoding"><code>--encoding</code></a> is required if <a class="reference internal" href="#cmdoption-calendar-locale"><code>--locale</code></a> is set.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-type">
<code>--type {text,html}, -t {text,html}</code> </dt> <dd>
<p>Print the calendar to the terminal as text, or as an HTML document.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-arg-year">
<code>year</code> </dt> <dd>
<p>The year to print the calendar for. Must be a number between 1 and 9999. Defaults to the current year.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-arg-month">
<code>month</code> </dt> <dd>
<p>The month of the specified <a class="reference internal" href="#cmdoption-calendar-arg-year"><code>year</code></a> to print the calendar for. Must be a number between 1 and 12, and may only be used in text mode. Defaults to printing a calendar for the full year.</p> </dd>
</dl> <p><em>Text-mode options:</em></p> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-width">
<code>--width WIDTH, -w WIDTH</code> </dt> <dd>
<p>The width of the date column in terminal columns. The date is printed centred in the column. Any value lower than 2 is ignored. Defaults to 2.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-lines">
<code>--lines LINES, -l LINES</code> </dt> <dd>
<p>The number of lines for each week in terminal rows. The date is printed top-aligned. Any value lower than 1 is ignored. Defaults to 1.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-spacing">
<code>--spacing SPACING, -s SPACING</code> </dt> <dd>
<p>The space between months in columns. Any value lower than 2 is ignored. Defaults to 6.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-months">
<code>--months MONTHS, -m MONTHS</code> </dt> <dd>
<p>The number of months printed per row. Defaults to 3.</p> </dd>
</dl> <p><em>HTML-mode options:</em></p> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-calendar-css">
<code>--css CSS, -c CSS</code> </dt> <dd>
<p>The path of a CSS stylesheet to use for the calendar. This must either be relative to the generated HTML, or an absolute HTTP or <code>file:///</code> URL.</p> </dd>
</dl> </section> <div class="_attribution">
  <p class="_attribution-p">
    &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
    <a href="https://docs.python.org/3.12/library/calendar.html" class="_attribution-link">https://docs.python.org/3.12/library/calendar.html</a>
  </p>
</div>