Python Forum
Time Difference in Epoch Microseconds then convert to human readable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Time Difference in Epoch Microseconds then convert to human readable (/thread-8585.html)



Time Difference in Epoch Microseconds then convert to human readable - firesh - Feb-27-2018

hi i have 2 epoch microseconds and i would like to know the datetime difference then print out the difference with human readable.

Epoch Microseconds:

Output:
Start : 1519614072686129 End : 1519614073798275
Would like to know the difference on this time and print human readable with microseconds intact.


RE: Time Difference in Epoch Microseconds then convert to human readable - Larz60+ - Feb-27-2018

learn all about it here: https://pymotw.com/3/datetime/


RE: Time Difference in Epoch Microseconds then convert to human readable - firesh - Feb-27-2018

(Feb-27-2018, 06:40 AM)Larz60+ Wrote: learn all about it here: https://pymotw.com/3/datetime/

I am sorry but it doesnt tell me anywhere in there to
convert epoch microseconds to a datetime object.
Everything starts from datetime object.

Most of the tutorial shows datetime.datetime.now()
but i have epoch microseconds.


RE: Time Difference in Epoch Microseconds then convert to human readable - DeaD_EyE - Feb-27-2018

deadeye@nexus ~ $ import datetime
deadeye@nexus ~ $ import time
deadeye@nexus ~ $ datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2018, 2, 27, 8, 29, 46, 464030)
deadeye@nexus ~ $ 



RE: Time Difference in Epoch Microseconds then convert to human readable - firesh - Feb-27-2018

Hi ,

Thank you all, despite all trying to help, but i couldn't understand.
I have managed to solve it myself. Hence, i will just share with whoever looking for solutions too.

from datetime import datetime, timedelta

# epoch timestamp in microseconds
arr = [
'1519649295496484 -> 1519649327106949',
'1519649400617116 -> 1519649400728112',
'1519649441795007 -> 1519649442855342',
'1519649654498861 -> 1519649655227328'
]

epoch = datetime(1970, 1, 1)

if len(arr):
    for line in arr:

        tmp = []
        dateTimes = line.split('->')

        for epochTime in dateTimes:

            cookie_microseconds_since_epoch = int(epochTime)
            cookie_datetime = epoch + timedelta(microseconds=cookie_microseconds_since_epoch)
            tmp.append(cookie_datetime)

        for index, item in enumerate(tmp):

            if len(tmp) == index + 1:
                break

            elapsedTime = tmp[index + 1] - item

            print str(item) + ' -> ' + str(tmp[index + 1]) + ' : ' + str(elapsedTime)
Output:
2018-02-26 12:48:15.496484 -> 2018-02-26 12:48:47.106949 : 0:00:31.610465 2018-02-26 12:50:00.617116 -> 2018-02-26 12:50:00.728112 : 0:00:00.110996 2018-02-26 12:50:41.795007 -> 2018-02-26 12:50:42.855342 : 0:00:01.060335 2018-02-26 12:54:14.498861 -> 2018-02-26 12:54:15.227328 : 0:00:00.728467
This solution will get a list of timestamp with native epoch timestamp.
Then split the timestamp and add to its own array with datetime format.
Perform the time difference calculation.
And finally print the output. // change the printing output if you need it differently.