Calculate n-minutes from now.

In the post about JSON web tokens I showed how to calculate token expiration time. Below are two approaches how to do it in Python:

Using timedelta

Using local time

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2018, 3, 12, 21, 40, 6, 542583)
>>> now_plus_5min = now + datetime.timedelta(minutes=5)
>>> now_plus_5min
datetime.datetime(2018, 3, 12, 21, 45, 6, 542583)

Using UTC

>>> import datetime
>>> now = datetime.datetime.utcnow()
>>> now_plus_5min = now + datetime.timedelta(minutes=5)
>>> now_plus_5min
datetime.datetime(2018, 3, 12, 21, 46, 21, 124740)

Using time

>>> import time
>>> time.ctime()
'Mon Mar 12 21:46:39 2018'
>>>
>>> now_plus_5min = time.time() + 300
>>> time.ctime(now_plus_5min)
'Mon Mar 12 21:52:09 2018'

More information: calculating time in Python3