Created
March 5, 2012 06:27
-
-
Save bilalhusain/1977014 to your computer and use it in GitHub Desktop.
array of YYYYMMDD for last week (Sunday to Saturday)
This file contains 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
#!/usr/bin/python | |
import datetime | |
import calendar | |
def getLastWeekStamps(today=datetime.date.today()): | |
"""Usage: | |
getLastWeekStamps() | |
getLastWeekStamps(datetime.date(2012, 1, 4)) | |
""" | |
oneday = datetime.timedelta(days=1) | |
latestSunday = today # patience, we'll roll calendar to that day | |
while latestSunday.weekday() != calendar.SUNDAY: | |
latestSunday -= oneday # told ya | |
startDate = latestSunday - 7 * oneday | |
results = [] | |
for i in range(7): | |
stamp = (startDate + i * oneday).strftime('%Y%m%d') | |
results.append(stamp) | |
return results | |
if __name__ == '__main__': | |
print('last week') | |
print(getLastWeekStamps()) | |
today = datetime.date(2012, 1, 4) | |
print("today impersonation to %s" % today) | |
print(getLastWeekStamps(today)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment