Epoch time parser

This is the place for queries that don't fit in any of the other categories.

Epoch time parser

Postby spikeinc » Fri Aug 09, 2013 9:44 am

Introducing myself as a very new beginner in python so sorry for basic questions as always,

so heres my problem. I have a log output from a router which I collect using python. The output contains several lines of logs each with a timestamp. Now I need to process this output and print just one line out of the output. This line tat I want to print is based on the specific time of the day.

The challenge is the time is epoch time. I have to print say for example the line corresponding to 12:15 AM in the logs by parsing each line of epoch time, convert it to date/time format and then if it matches 12:15, print only that line from the log.

My idea was to import the epoch time into array, convert the epoch time to readable time, then do a condition match. But it turned out to be much more complex than that and I couldnt get it work.

Could someone help me. Here is a sample output: the big numbers like 1375976271 are the epoch time start and end. from this output I want to print the line matching a specific time.

Code: Select all
{"DisplayRecords":"12","Records":"12","sColumns":"startTime,endTime,
remoteNode,srcIP,srcPort,destIP,destPort,egress,ingress","aaData":[["1375976271"
,"1375976430","LAN","D0:05:FE","172.20.30.2",1093,"172.20.28.2",1330,"1034,348",
"0,0"],["1375976246","1375976404","LAN","D0:05:FE","172.20.30.2",1092,"172.20.28
.2",1327,"1034,348","0,0"],["1375976191","1375976381","LAN","D0:05:FE","172.20.3
0.2",1091,"172.20.28.2",1324,"1034,348","0,0"],["1375976171","1375976314","LAN",
"D0:05:FE","172.20.30.2",1090,"172.20.28.2",1321,"1034,348","0,0"],["1375976144"
,"1375976302","LAN","D0:05:FE","172.20.30.2",1089,"172.20.28.2",1319,"1034,348",
"0,0"],["1375975155","1375976089","LAN","D0:05:FE","172.20.30.2",1085,"172.20.28
.2",1278,"1034,348","0,0"],["1375975194","1375975352","LAN","D0:05:FE","172.20.3
0.2",1086,"172.20.28.2",1281,"1034,348","0,0"],["1375974693","1375974835","LAN",
"D0:05:FE","172.20.30.2",1083,"172.20.28.2",1271,"1034,348","0,0"],["1375974668"
,"1375974827","LAN","D0:05:FE","172.20.30.2",1081,"172.20.28.2",1268,"1034,348",
"0,0"],["1375974691","1375974821","LAN","D0:05:FE","172.20.30.2",1082,"172.20.28
.2",1270,"0,0","0,0"],["1375881371","1375881561","LAN","D0:05:FE","172.20.30.2",
1055,"172.20.28.2",1122,"1034,348","0,0"],["1375881410","1375881540","LAN","E0:0
5:AE","172.20.30.2",1056,"172.20.28.2",1125,"0,0","0,0"]]}
Last edited by Yoriz on Fri Aug 09, 2013 1:39 pm, edited 1 time in total.
Reason: added code tags
spikeinc
 
Posts: 7
Joined: Fri Aug 09, 2013 9:40 am

Re: Epoch time parser

Postby micseydel » Fri Aug 09, 2013 4:46 pm

The datetime module is probably what you want. If you still have questions after looking at that, please post your code attempt, even if it fails, and explain what your code attempts to accomplish but doesn't manage to, as well as what it does do.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Epoch time parser

Postby tnknepp » Fri Aug 09, 2013 4:59 pm

It is beneficial to see what you have tried, perhaps we can help you fix what you have.

Converting epoch time is simple enough, the question is how do you want it converted? This really depends on the application. For example, of you are only concerned about the time, not dates:

Code: Select all
import time

# loop through your epoch times and create time tuples of time structures
tod = [time.localtime(t) for t in timeList]

>>>tod[0]
    time.struct_time(tm_year=2013, tm_mon=8, tm_mday=9, tm_hour=16, tm_min=34, tm_sec=51, tm_wday=4, tm_yday=221, tm_isdst=0)


You could then search through to find where time==00:15, something like:

Code: Select all
# operating on the previously made <tod>
tod = [ t[3]+t[4] for t in tod ]


You can search in an array, or you can make lists, dictionaries, or use Pandas (my favorite). This comes down to the application, which we don't know enough of.

Play around with the time module, datetime, etc. You will find these interesting and useful.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 114
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests