After a little experiment and thinking, I believe that I have a solution for you. My previous answer was incorrect, as you indicated; the timedelta object for days = 1 is basically the same as for seconds = 86400 (except when it comes to seconds).
One way, as I would recommend, to increase the date without taking into account the time of day, is to use the datetime.date object instead of the datetime.datetime object:
>>> oneday = datetime.timedelta(days=1) >>> d = datetime.date(2011,4,3) >>> str(d + oneday) '2011-04-04'
You can then add the time of day to form a complete datetime.datetime object in which you know that the time of day fields do not change from your original value.
Another method that I would feel safe using is to temporarily work with βnaiveβ dates. Therefore, the policy for adding time zones is not applied when adding timedelta .
>>> hob = pytz.timezone('Australia/Hobart') >>> dstlast = datetime.datetime(2011,4,3) >>> str(dstlast) '2011-04-03 00:00:00' >>> dstlasthob = hob.localize(dstlast) >>> str(dstlasthob) '2011-04-03 00:00:00+11:00' >>> oneday = datetime.timedelta(days=1) >>> str(hob.normalize(dstlasthob + oneday)) '2011-04-03 23:00:00+10:00' >>> nextday = hob.localize(dstlasthob.replace(tzinfo=None) + oneday) >>> str(nextday) '2011-04-04 00:00:00+10:00'
I tested this method for dates that have leap seconds in them (one example is 2008-12-31) and the result has a time of 00:00:00. This may actually be wrong, I'm not sure, but this is what you want :-)
wberry Aug 30 '11 at 15:45 2011-08-30 15:45
source share