カピバラ好きなエンジニアブログ

興味ある技術とか検証した内容を赴くままに書いていきます。カピバラの可愛さこそ至高。

Pythonで今日が月の何週目か調べてみた

はじめに

あけましておめでとうございます。
今年も引き続き頑張って記事を書いていくので、どうぞよろしくお願いします。
(新年1発目は新年の目標関連の記事にするつもりだったんですけどね。。)

実施内容

タイトル通りです
実際にやりたかったことは実行日が特定の日(第〇週目の火曜日、等)かどうかを確認することですが、その前にその日が何週目かを取得する方法がわからなかったので、調べて検証してみました。

尚、以下の記事を参考にさせていただきました。(というか内容はほぼトレースじみたものですが...)
わかりやすい記事ありがとうございます(-人-)

qiita.com

1行ずつ理解しながら進めたいので、敢えてPythonの対話モードでやっていきます。
Pythonのバージョンは 3.7.3です。


実施作業

powershellを起動し、Pyhonを対話モードで実行できるようにします

f:id:live-your-life-dd18:20200109155146p:plain

必要なライブラリをimportします

コマンド

from datetime import datetime
from calendar import Calendar

結果
f:id:live-your-life-dd18:20200109155319p:plain

※datetimeライブラリについて
docs.python.org

※calendarライブラリについて
docs.python.org

現在の日付をnowメソッドで取得します

コマンド

date = datetime.now()

結果
f:id:live-your-life-dd18:20200109155700p:plain

※nowメソッドについて
f:id:live-your-life-dd18:20200109160350p:plain

カレンダーオブジェクトを作成します

コマンド

calendar = Calendar(firstweekday=0)

結果
f:id:live-your-life-dd18:20200109160944p:plain

※カレンダーオブジェクトについて
f:id:live-your-life-dd18:20200109161033p:plain

firstweekdayに設定する数字によって、カレンダーが何曜日始まりかが指定できます(デフォルトは月曜日)

0=月曜日
1=火曜日
2=水曜日
3=木曜日
4=金曜日
5=土曜日
6=日曜日


指定した年月の週のリストを取得します

コマンド

month_calendar = calendar.monthdays2calendar(date.year, date.month)

結果
f:id:live-your-life-dd18:20200109161641p:plain

結果整形後

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

取得した結果と実際のカレンダーを見比べてみると、取得したリストは(日付, 曜日)のフォーマットでデータが格納されており、対象月以外の日にち(ここでいうと12月と2月)の部分には、0が格納されていることが確認できます。


f:id:live-your-life-dd18:20200109162128p:plain

※週のリストを取得する関数について
f:id:live-your-life-dd18:20200109161729p:plain

週ごとの出力を確認します

コマンド

count = 1
for week in month_calendar:
  print(str(count)+'週目')
  print(week)
  count += 1

結果
f:id:live-your-life-dd18:20200109163353p:plain

日ごとの出力を確認します

コマンド

count = 1
for week in month_calendar:
  print(str(count)+'週目')
  for day in week:
    print(day)
  count += 1

結果
f:id:live-your-life-dd18:20200109163908p:plain

実行日が月の何週目か判断します

コマンド

count = 1
dayofweek = 0
for week in month_calendar:
  for day in week:
    if day[0] == date.day:
      break
  else:
    count += 1
    continue
  break

print(str(date.month)+"月"+str(date.day)+"日は第"+str(count)+"週目です")

結果
f:id:live-your-life-dd18:20200109170149p:plain

※多重ループした場合のbreak処理で少し詰まりました
note.nkmk.me

感想及び所感

ほぼ参考元の記事のコマンドを実行した感じですが、細かく出力を確認しながらだったので、どのコマンドでどういう結果が取得されているのかがよくわかりました。

最後に実行したコマンドを載せておきます。(途中の出力の確認しているコマンドは省略しています)

from datetime import datetime
from calendar import Calendar

date = datetime.now()
calendar = Calendar(firstweekday=0)
month_calendar = calendar.monthdays2calendar(date.year, date.month)

count = 1
dayofweek = 0
for week in month_calendar:
  for day in week:
    if day[0] == date.day:
      break
  else:
    count += 1
    continue
  break

print(str(date.month)+"月"+str(date.day)+"日は第"+str(count)+"週目です")