Created
May 21, 2016 06:46
-
-
Save hiroakit/9800839bb8f078a3c3f91a37f7b017ca to your computer and use it in GitHub Desktop.
python daily.py | pbcopy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
今月の週番号と日付をのテンプレートを生成するスクリプト | |
今月の週番号と日付をのテンプレートを生成する | |
""" | |
from datetime import * | |
def count_day_of_month(month, year=None): | |
""" | |
指定した月の総日数を返す | |
month -- 月 | |
year -- 明示的な指定がなければ、現在の年を使う | |
""" | |
if year == None: | |
year = date.today().year | |
next_month = date(year, month % 12 + 1, 1) | |
last_day = next_month - timedelta(days=1) | |
return last_day.day | |
def to_formatted_date_string(date): | |
""" | |
表示用文字列にフォーマットして返す | |
date -- datetime.date | |
""" | |
__WEEKDAY = ('月','火','水','木','金','土','日') | |
date_string = "{0:%Y-%m-%d} ({1})".format(date, __WEEKDAY[date.weekday()]) | |
return date_string | |
if __name__ == '__main__': | |
today = date.today() | |
day_count = count_day_of_month(today.month) | |
prev_week_number = 0 | |
for i in range(day_count): | |
day = date(today.year, today.month, i + 1) | |
date_string = to_formatted_date_string(day) | |
current_week_number = "{0:%U}".format(day) | |
if not prev_week_number == current_week_number: | |
prev_week_number = "{0:%U}".format(day) | |
print ("** W{0:%U}".format(day)) | |
print ("*** {0}".format(date_string)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment