Python Forum
Time Difference in Epoch Microseconds then convert to human readable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time Difference in Epoch Microseconds then convert to human readable
#1
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.
Reply
#2
learn all about it here: https://pymotw.com/3/datetime/
Reply
#3
(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.
Reply
#4
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 ~ $ 
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  time difference bettwenn logs enkliy 14 967 Nov-21-2023, 04:51 PM
Last Post: rob101
  io.UnsupportedOperation: not readable RedSkeleton007 2 18,729 Nov-06-2023, 06:32 AM
Last Post: gpurdy
  Hard time trying to figure out the difference between two strings carecavoador 2 676 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Sum up Time difference tester_V 10 2,518 Apr-06-2023, 06:54 AM
Last Post: Gribouillis
  [SOLVED] Epoch timestamp without milliseconds? Winfried 5 2,982 Jan-27-2023, 04:35 PM
Last Post: deanhystad
  Human Sorting (natsort) does not work [SOLVED] AlphaInc 2 1,128 Jul-04-2022, 10:21 AM
Last Post: AlphaInc
  How to make x-axis readable with matplotlib Mark17 7 3,910 Mar-01-2022, 04:30 PM
Last Post: DPaul
  Function global not readable by 'main' fmr300 1 1,335 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  sorting alphanumeric values in a human way idiotonboarding 3 2,611 Jan-22-2021, 05:57 PM
Last Post: idiotonboarding
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,124 Dec-30-2020, 12:04 AM
Last Post: MvGulik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020