Skip to content

Instantly share code, notes, and snippets.

@utgwkk
Created February 26, 2017 06:09
Show Gist options
  • Save utgwkk/b7a162763fed10504f707479f619cd4a to your computer and use it in GitHub Desktop.
Save utgwkk/b7a162763fed10504f707479f619cd4a to your computer and use it in GitHub Desktop.
今日または指定された日を起点とした次のプレミアムフライデーの日付を取得する
from datetime import date, timedelta
def next_premium_friday(offset=None):
if offset is None:
offset = date.today()
if offset.month == 12:
end_of_month = date(offset.year + 1, 1, 1) - date.resolution
else:
end_of_month = date(offset.year, offset.month + 1, 1) - date.resolution
end_of_month_weekday = end_of_month.weekday()
premium_friday = end_of_month - timedelta(days=(3 + end_of_month_weekday) % 7)
if offset > premium_friday:
if offset.month == 12:
next_month = date(offset.year + 1, 1, 1)
else:
next_month = date(offset.year, offset.month + 1, 1)
return next_premium_friday(next_month)
else:
return premium_friday
if __name__ == '__main__':
print(next_premium_friday(date(2017, 2, 24)))
print(next_premium_friday(date(2017, 2, 25)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment