I'm trying to do some datetime processing. The input is a string of the form 20141005
('%Y%m%d')
and I want to return a data structure that stores the day before the input date, the day from the input, and the day after along with the day after that in a %y%m%d
format (141005
)
This is my function right now, and I'm wondering if there's a better way of doing it, whether it's too compact, if I should use different data structures, if my comments are too verbose, if my style is alright - basically any kind of tips and advice are very welcome.
from datetime import datetime, timedelta
def process_dates(date):
"""
Process adjacent days to account for flights that
have a different departure day than the day they were
archived (which is the date contained in the filenames)
"""
"""
Convert to datetime objects before processing
This is done so the conversion is less error-prone by
avoiding edge cases like 1st of Jan, 29th of Feb, y3k, etc
"""
datetimes = []
today = datetime.strptime(date, '%Y%m%d')
"""
Compute the adjacent days, but keeping the order of the elements
the following: today, yesterday, tomorrow, and the day after tomorrow.
This is done because the looking up later on is done sequentially
and the order reflects the probability of being the needed date
(i.e. it's most probable the needed day is today, so it will stop before
processing the other days)
"""
[datetimes.append(today + timedelta(days=i)) for i in [0, -1, 1, 2]]
return [datetime.strftime(j, '%y%m%d') for j in datetimes]