13

How to add days in epoch time in Python

#lssec -a lastupdate -s root -f /etc/security/passwd 2>/dev/null | cut -f2 -d=
1425917335

above command giving me epoch time I want to add 90 days in that time. how do I add days in epoch time?

Martin Valgur
  • 5,793
  • 1
  • 33
  • 45
Satish
  • 16,544
  • 29
  • 93
  • 149

3 Answers3

19

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. :-)

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • my python 2.6 doesn't have `>>> print(new.timestamp())` function getting error `AttributeError: 'datetime.datetime' object has no attribute 'timestamp'` – Satish Dec 17 '15 at 02:57
  • @Satish: Pre-3.3, you can cobble together `.timestamp()` using `time.mktime(x.timetuple())`. It's uglier (and you need to `import time`), but equivalent. Note, you may, depending on source of the timestamp and whether it's using UTC or local time, want to use `utcfromtimestamp`and `utctimetuple` instead of the non-UTC functions given. – ShadowRanger Dec 17 '15 at 03:03
  • Side-note: 2.6? Really? 2.7 has been out for over five years! – ShadowRanger Dec 17 '15 at 03:05
  • I have imported both `datetime` and `time` but it didn't work, same error, any other work around? – Satish Dec 17 '15 at 03:09
  • 3
    @Satish: Wait, what? You couldn't possibly have the same error if you didn't call `.timestamp()`, and my suggestion was to replace `new.timestamp()` with `time.mktime(new.timetuple())`. – ShadowRanger Dec 17 '15 at 03:12
  • Its works! you are right, i use `print new` and it works – Satish Dec 17 '15 at 03:16
  • it may fail if the local timezone may have different utc offset at different dates. – jfs Dec 17 '15 at 15:30
6

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.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

You can you dateutil.parser as well:

since24Hours = datetime.utcnow() - timedelta(days=1)
since24Hours=dateutil.parser.parse(str(since24Hours))
since24Hours = since24Hours.strftime('%s')
since24Hours=int(since24Hours)*1000
Pavneet Singh
  • 342
  • 1
  • 2
  • 10