Skip to content

Instantly share code, notes, and snippets.

@bilalhusain
Created March 5, 2012 06:27
Show Gist options
  • Save bilalhusain/1977014 to your computer and use it in GitHub Desktop.
Save bilalhusain/1977014 to your computer and use it in GitHub Desktop.
array of YYYYMMDD for last week (Sunday to Saturday)
#!/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