Python add days in epoch time

datetime makes it easy between fromtimestamp, timedelta and timestamp:

>>> import datetime
>>> orig = datetime.datetime.fromtimestamp(1425917335)
>>> new = orig + datetime.timedelta(days=90)
>>> print(new.timestamp())
1433693335.0

On Python 3.2 and earlier, datetime objects don't have a .timestamp() method, so you must change the last line to the less efficient two-stage conversion:

>>> import time
>>> print(time.mktime(new.timetuple()))

The two-stage conversion takes ~10x longer than .timestamp() on my machine, taking ~2.5 µs, vs. ~270 ns for .timestamp(); admittedly still trivial if you aren't doing it much, but if you need to do it a lot, consider it another argument for using modern Python. :-)


If the input is POSIX timestamp then to get +90 days:

DAY = 86400 # POSIX day (exact value)
future_time = epoch_time + 90*DAY

If you want to work with datetime objects then use UTC timezone:

from datetime import datetime, timedelta

utc_time = datetime.utcfromtimestamp(epoch_time)
future_time = utc_time + timedelta(90)

Don't use local time for the date/time arithmetic (avoid naive fromtimestamp(), mktime(), naive_dt.timestamp() if you can help it). To understand when it may fail, read Find if 24 hrs have passed between datetimes - Python.