Very often I have to deal with logs that come with timestamps.
In some cases, I collect logs from two different tools at the same time, yet they have their own timestamp style, or even in different locales.
That makes the time analysis difficult, because I have to (manually) mapping the packages using the universal timestamp in my head.
If, somehow, the time difference calculation is involved, it totally becomes a nightmare.
So, if there is any way to convert the timestamp format so that we can handily align them?
Well, the Python packages datetime and pytz come to the rescue!
Below are two example log excerpts over the same period of time:
LOG1 – from the phone
LOG2 – from tcpdump
No.
Time
Timestamp
Source
Destination
Protocol
Length
Info
15
16.311732
Feb 3, 2016 19:40:56.337615000 PST
fd18:58d7:**
fd18:58d7:**
DNS
99
Standard query 0xc933 A stun.l.google.com
19
16.367842
Feb 3, 2016 19:40:56.393725000 PST
192.168.3.*
74.125.142.*
STUN
64
Binding Request
Then using the following code snippet, we can easily convert LOG1’s time to the standard timestamp in PST!
LOG1 – timestamp converted
Notice that we also need some tricks to play with the Python regex package re and math package math. Enjoy.