Created
January 21, 2013 12:47
-
-
Save rikva/4585800 to your computer and use it in GitHub Desktop.
Date generator for simple backup rotation
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
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