[Python]calendar[ライブラリ]

Python

calendar」はひと月分や一年分のまとまった日付に関するライブラリです。

標準ライブラリのため、インストール不要で使うことができます。

このページでは、ざっくりとした使い方――便利な関数の紹介と、Calendarクラスの似ているメソッド、その戻り値の違いを確認します。

参考ページ
calendar 一般的なカレンダーに関する関数群

使用した環境
WSL2 Ubuntu - 20.04
Python 3.10.0

 Androidアプリを作成しました。
 感情用のメモ帳です。

スポンサーリンク
スポンサーリンク

便利な関数

カレンダーライブラリの便利な関数をいくつかピックアップ。

関数は、

  1. import calendar
  2. calendar.関数名()

で呼び出します。

うるう年の判定

calendar.isleap(year)

Python 3.10.0 (default, Oct 12 2021, 16:02:08) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import calendar
>>> calendar.isleap(2023)
False
>>> calendar.isleap(2024)
True
>>> for i in range(2022, 2033):
...     if calendar.isleap(i):
...             print(i)
... 
2024
2028
2032

これを書いている時が2022年ですが、今後10年内のうるう年は、2024年、28年、32年、のようです。

曜日の判定

calendar.weekday(year, month, day)

戻り値は0から6の数値です。
0が月曜日、1が火曜日、というように順番に割り振られていて、6が日曜日です。

>>> calendar.weekday(3000, 12, 24)
2
>>> calendar.day_name[2]
'Wednesday'
>>> list(calendar.day_name)
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

3000年にまだクリスマスの習慣があるなら、24日は戻り値が2なので水曜日、クリスマス当日は木曜日になります。

月始めの曜日と日数

calendar.monthrange(year, month)

戻り値はタプルで(曜日, 日数)です。
曜日は0から6の数値で表されます。

>>> calendar.monthrange(2024, 2)
(3, 29)

2024年の2月1日は木曜日、2月の日数は29あります。

ひと月分のカレンダーを出力

calendar.prmonth(year, month)

>>> calendar.prmonth(2022, 5)
      May 2022
Mo Tu We Th Fr Sa Su
                   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

Calendarクラス

Calendarクラスは、ひと月や一年分の日付を生成するクラスです。

それ以外にもサブクラスを除くと、TextCalendar――文字列としてカレンダーを生成するクラスと、HTMLCalendar――カレンダーをHTMLのテーブルやページとして生成するクラスがあります。

これらのクラスを利用する手順。

  1. モジュールのインポート
  2. クラスからインスタンスを作成(週の初めの曜日を設定)
  3. メソッドで必要な年や月などの引数を渡す
Python 3.10.0 (default, Oct 12 2021, 16:02:08) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import calendar
>>> cal = calendar.TextCalendar(6)
>>> cal.prmonth(2022,4)
     April 2022
Su Mo Tu We Th Fr Sa
                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

上記では、インスタンスの作成時に「6」を渡し、日曜日を週のはじまりとしています。
省略すると、デフォルトの「0」が適用され、月曜日となります。

>>> cal2 = calendar.TextCalendar()
>>> cal2.prmonth(2022,4)
     April 2022
Mo Tu We Th Fr Sa Su
             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

ヨーロッパの慣例で月曜日が週の始まりとなっているようです。
私が確認した手近のカレンダーはどれも日曜日が週のはじまりだったので、日本ではこちらが主流でしょうか。

以下の項目では、Calendarクラスのメソッドの違いを見ていきますが、出力されたものは、日曜日を週のはじまりとして設定しています。

ひと月分のイテレータを返すメソッド

itermonthdates(year, month)

ひと月分の日付をイテレータとして返します。
中身はdatatime.date型です。カレンダーとして足りない箇所は、前月や翌月の日付で埋めます。

>>> import calendar
>>> cal = calendar.Calendar(6)
>>> cal.itermonthdates(2022,4)
<generator object Calendar.itermonthdates at 0x7f181937bae0>
>>> for date in cal.itermonthdates(2022, 4):
...     date
... 
datetime.date(2022, 3, 27)
datetime.date(2022, 3, 28)
datetime.date(2022, 3, 29)
datetime.date(2022, 3, 30)
datetime.date(2022, 3, 31)
datetime.date(2022, 4, 1)
datetime.date(2022, 4, 2)
datetime.date(2022, 4, 3)
datetime.date(2022, 4, 4)
datetime.date(2022, 4, 5)
datetime.date(2022, 4, 6)
datetime.date(2022, 4, 7)
datetime.date(2022, 4, 8)
datetime.date(2022, 4, 9)
datetime.date(2022, 4, 10)
datetime.date(2022, 4, 11)
datetime.date(2022, 4, 12)
datetime.date(2022, 4, 13)
datetime.date(2022, 4, 14)
datetime.date(2022, 4, 15)
datetime.date(2022, 4, 16)
datetime.date(2022, 4, 17)
datetime.date(2022, 4, 18)
datetime.date(2022, 4, 19)
datetime.date(2022, 4, 20)
datetime.date(2022, 4, 21)
datetime.date(2022, 4, 22)
datetime.date(2022, 4, 23)
datetime.date(2022, 4, 24)
datetime.date(2022, 4, 25)
datetime.date(2022, 4, 26)
datetime.date(2022, 4, 27)
datetime.date(2022, 4, 28)
datetime.date(2022, 4, 29)
datetime.date(2022, 4, 30)

itermonthdays(year, month)

戻り値はイテレータで中身はint型
足りない日付は「0」で穴埋めされます。

>>> list(cal.itermonthdays(2022,4))
[0, 0, 0, 0, 0, 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]

※上記はリストに変換しました。


itermonthdays2(year, month)

戻り値はイテレータで、タプルの(日付, 曜日)

>>> list(cal.itermonthdays2(2022,4))
[(0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (1, 4), (2, 5), (3, 6), (4, 0), (5, 1), (6, 2), (7, 3), (8, 4), (9, 5), (10, 6), (11, 0), (12, 1), (13, 2), (14, 3), (15, 4), (16, 5), (17, 6), (18, 0), (19, 1), (20, 2), (21, 3), (22, 4), (23, 5), (24, 6), (25, 0), (26, 1), (27, 2), (28, 3), (29, 4), (30, 5)]

itermonthdays3(year, month)

イテレータで、タプルの(年, 月, 日)が返ってきます。
※Python3.7以降

>>> list(cal.itermonthdays3(2022,4))
[(2022, 3, 27), (2022, 3, 28), (2022, 3, 29), (2022, 3, 30), (2022, 3, 31), (2022, 4, 1), (2022, 4, 2), (2022, 4, 3), (2022, 4, 4), (2022, 4, 5), (2022, 4, 6), (2022, 4, 7), (2022, 4, 8), (2022, 4, 9), (2022, 4, 10), (2022, 4, 11), (2022, 4, 12), (2022, 4, 13), (2022, 4, 14), (2022, 4, 15), (2022, 4, 16), (2022, 4, 17), (2022, 4, 18), (2022, 4, 19), (2022, 4, 20), (2022, 4, 21), (2022, 4, 22), (2022, 4, 23), (2022, 4, 24), (2022, 4, 25), (2022, 4, 26), (2022, 4, 27), (2022, 4, 28), (2022, 4, 29), (2022, 4, 30)]

itermonthdays4(year, month)

イテレータで、タプルの(年, 月, 日, 曜日)。
※Python3.7以降

>>> list(cal.itermonthdays4(2022,4))
[(2022, 3, 27, 6), (2022, 3, 28, 0), (2022, 3, 29, 1), (2022, 3, 30, 2), (2022, 3, 31, 3), (2022, 4, 1, 4), (2022, 4, 2, 5), (2022, 4, 3, 6), (2022, 4, 4, 0), (2022, 4, 5, 1), (2022, 4, 6, 2), (2022, 4, 7, 3), (2022, 4, 8, 4), (2022, 4, 9, 5), (2022, 4, 10, 6), (2022, 4, 11, 0), (2022, 4, 12, 1), (2022, 4, 13, 2), (2022, 4, 14, 3), (2022, 4, 15, 4), (2022, 4, 16, 5), (2022, 4, 17, 6), (2022, 4, 18, 0), (2022, 4, 19, 1), (2022, 4, 20, 2), (2022, 4, 21, 3), (2022, 4, 22, 4), (2022, 4, 23, 5), (2022, 4, 24, 6), (2022, 4, 25, 0), (2022, 4, 26, 1), (2022, 4, 27, 2), (2022, 4, 28, 3), (2022, 4, 29, 4), (2022, 4, 30, 5)]

ひと月分の週のリストを返すメソッド

monthdatescalendar(year, month)

datetime.date型の週のリスト。

>>> cal.monthdatescalendar(2022, 4)
[[datetime.date(2022, 3, 27), datetime.date(2022, 3, 28), datetime.date(2022, 3, 29), datetime.date(2022, 3, 30), datetime.date(2022, 3, 31), datetime.date(2022, 4, 1), datetime.date(2022, 4, 2)], [datetime.date(2022, 4, 3), datetime.date(2022, 4, 4), datetime.date(2022, 4, 5), datetime.date(2022, 4, 6), datetime.date(2022, 4, 7), datetime.date(2022, 4, 8), datetime.date(2022, 4, 9)], [datetime.date(2022, 4, 10), datetime.date(2022, 4, 11), datetime.date(2022, 4, 12), datetime.date(2022, 4, 13), datetime.date(2022, 4, 14), datetime.date(2022, 4, 15), datetime.date(2022, 4, 16)], [datetime.date(2022, 4, 17), datetime.date(2022, 4, 18), datetime.date(2022, 4, 19), datetime.date(2022, 4, 20), datetime.date(2022, 4, 21), datetime.date(2022, 4, 22), datetime.date(2022, 4, 23)], [datetime.date(2022, 4, 24), datetime.date(2022, 4, 25), datetime.date(2022, 4, 26), datetime.date(2022, 4, 27), datetime.date(2022, 4, 28), datetime.date(2022, 4, 29), datetime.date(2022, 4, 30)]]
>>> len(cal.monthdatescalendar(2022, 4))
5

上記の場合では、リストの中に週のリストが5つはいっています。


monthdays2calendar(year, month)

(日付, 曜日)の週のリスト。int型。

>>> cal.monthdays2calendar(2022, 4)
[[(0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (1, 4), (2, 5)], [(3, 6), (4, 0), (5, 1), (6, 2), (7, 3), (8, 4), (9, 5)], [(10, 6), (11, 0), (12, 1), (13, 2), (14, 3), (15, 4), (16, 5)], [(17, 6), (18, 0), (19, 1), (20, 2), (21, 3), (22, 4), (23, 5)], [(24, 6), (25, 0), (26, 1), (27, 2), (28, 3), (29, 4), (30, 5)]]

monthdayscalendar(year, month)

日付のみの週リスト。

>>> cal.monthdayscalendar(2022, 4)
[[0, 0, 0, 0, 0, 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]]

一年分のリストを返すメソッド

この項のメソッドの引数には「width」が設定できるようになっています。
外側から2番目のリストに、いくつのデータを収めるか設定するものです。
widthを省略するとデフォルト値の「3」が適用され、3ヶ月分のデータが入り、4行になります。


[
[[ 1月分], [ 2月分], [ 3月分]],
[[ 4月分], [ 5月分], [ 6月分]],
[[ 7月分], [ 8月分], [ 9月分]],
[[10月分], [11月分], [12月分]]
]

「2」にすると2ヶ月分で6行、「4」だと4ヶ月分で3行、といったように変化します。
便宜上、例では、1月分、2月分・・・としていますが、ひと月の中には週のリスト、さらにその中には日付のリストが入っています。

ではメソッドにいきましょう。

yeardatescalendar(year, width)

datetime.date型を収めたリスト。

>>> year = cal.yeardatescalendar(2022)
>>> len(year)
4
>>> year[0][0][0][0]
datetime.date(2021, 12, 26)

yeardays2calendar(year, width)

(日付, 曜日)を収めたリスト。

>>> year = cal.yeardays2calendar(2022)
>>> year[0][0][0][0]
(0, 6)
>>> year[0][0][0]
[(0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5)]

yeardayscalendar(year, width)

日付のみのリスト。

>>> year = cal.yeardayscalendar(2022)
>>> # 一週間の日付リスト(1月1週)
>>> year[0][0][0]
[0, 0, 0, 0, 0, 0, 1]
>>> # 週のリスト(1月分)
>>> year[0][0]
[[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0]]
>>> # widthで設定した月のリスト(1月,2月,3月)
>>> year[0]
[[[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0]], [[0, 0, 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, 0, 0, 0, 0, 0]], [[0, 0, 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, 0, 0]]]

まとめ

うるう年の判定やちょっと曜日が知りたい、カレンダーが見たいときなど、日常的なことに使えるライブラリでした。

余談ですが、曜日に関しては、datetimeモジュールからメソッドで判別することもできます。

>>> import datetime
>>> datetime.date(2022, 4, 1).weekday()
4

calendarを使うことで、それこそカレンダーアプリ、日記、家計簿など、作れるものが増えそうです。

このページが少しでもお役に立てたのなら幸いです。

タイトルとURLをコピーしました