Last active
March 17, 2017 20:18
-
-
Save ezheidtmann/9025d619dc9958db7c92e4978f1d68ed to your computer and use it in GitHub Desktop.
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 datetime, timedelta, date | |
from unittest import TestCase | |
def get_week_start_day(dt, firstday='monday'): | |
if firstday == 'monday': | |
return dt.date() - timedelta(days=(dt.weekday() + 0)%7) | |
elif firstday == 'sunday': | |
return dt.date() - timedelta(days=(dt.weekday() + 1)%7) | |
class TestWeekMath(TestCase): | |
def test_week_start_day(self): | |
# sunday | |
d = get_week_start_day(datetime(2017, 1, 1, 5, 5, 5), firstday='monday') | |
self.assertEqual(d, date(2016, 12, 26)) | |
# monday | |
d = get_week_start_day(datetime(2017, 1, 2, 5, 5, 5), firstday='monday') | |
self.assertEqual(d, date(2017, 1, 2)) | |
# sunday | |
d = get_week_start_day(datetime(2017, 1, 1, 5, 5, 5), firstday='sunday') | |
self.assertEqual(d, date(2017, 1, 1)) | |
# monday | |
d = get_week_start_day(datetime(2017, 1, 2, 5, 5, 5), firstday='sunday') | |
self.assertEqual(d, date(2017, 1, 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment