Skip to content

Instantly share code, notes, and snippets.

@rikva
Created January 21, 2013 12:47
Show Gist options
  • Save rikva/4585800 to your computer and use it in GitHub Desktop.
Save rikva/4585800 to your computer and use it in GitHub Desktop.
Date generator for simple backup rotation
from datetime import date, timedelta
KEEP_DAYS = 7
KEEP_WEEKS = 4
KEEP_MONTHS = 6
today = date.today()
def calculate_daily_days():
return [today - timedelta(days=day) for day in range(0, KEEP_DAYS)]
def calculate_weekly_days():
first_day_of_week = today - timedelta(days=today.weekday())
return [first_day_of_week - timedelta(days=7*week) for week in range(0, KEEP_DAYS)]
def calculate_monthly_days():
for month in range(0, KEEP_MONTHS):
day = today.replace(day=1) # first day of this month
for i in range(1, month+1):
day = day - timedelta(days=1) # last day of prev month
day = day.replace(day=1) # first day of prev month
yield day
keep_dates = sorted(list(set([d for d in calculate_daily_days()] + [d for d in calculate_monthly_days()] + [d for d in calculate_weekly_days()])))
for d in keep_dates: print d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment