i am currently using the time.time()
command to calculate the current amount of seconds since the epoch, however could this be adapted to easily show the number of seconds since the epoch for a past date, I.E. 2014-03-14-18-00-00
Thanks
i am currently using the time.time()
command to calculate the current amount of seconds since the epoch, however could this be adapted to easily show the number of seconds since the epoch for a past date, I.E. 2014-03-14-18-00-00
Thanks
Python datetime
module has the functionality to compute the total number of seconds since the epoch for a past date. For example if you want to compute the number of seconds 2014-03-14-18-00-00
epoch = datetime.datetime.strptime('2014-03-14-18-00-00', '%Y-%m-%d-%H-%M-%S')
# to compute current amount of seconds from epoch
total_seconds = (epoch - datetime.datetime.fromtimestamp(time.time())).total_seconds()
Use time.mktime()
:
In [1]: import time
In [2]: target = time.strptime('2014-03-14-18-00-00', '%Y-%m-%d-%H-%M-%S')
In [3]: int(time.mktime(target))
Out[3]: 1394816400