0

I have question about the EPOCH time.

I need to calculate the time difference between two packets. and I am not so sure how:

printf("Epoch Time: %d:%d seconds\n", header->ts.tv_sec, header->ts.tv_usec);

the first packet shows: 1396191661:164162

the second packet shows:  1396191661:164193

I need that variable u_int diff_time will contain the time difference between two packets- in microseconds. as you can see, the difference is between packet_1 and packet_2- only in the microseconds part.

how should I calculate it if the difference not only within the tv.u_sec?

thanks in advanced.

Barmar
  • 741,623
  • 53
  • 500
  • 612
user3378689
  • 209
  • 1
  • 4
  • 12
  • possible duplicate of [How to calculate the execution time in C?](http://stackoverflow.com/questions/7675136/how-to-calculate-the-execution-time-in-c) – Barmar May 07 '14 at 08:10

2 Answers2

0

Suppose you have two times t1.sec, t1.usec, t2.sec and t2.usec with t1 > t2, then I suppose you can calculate the difference by (t1.sec - t2.sec) * 1000000 + (t1.usec - t2.usec).

user12205
  • 2,684
  • 1
  • 20
  • 40
0

Just add the difference between seconds:

udiff = (second.tv_sec - first.tv_sec) * 1000000 + (second.tv_usec - first.tv_usec)

You just have to check that the difference between two packets is less than ~2000 seconds to stay in the size of a 32-bits int.

Holt
  • 36,600
  • 7
  • 92
  • 139
  • the difference now in EPOCH right? do I need to convert it to regular microseconds..? or it is o.k to leave it this way.. thanks. – user3378689 May 08 '14 at 11:57
  • @user3378689 EPOCH is the name from which time is mesured, not a unit, it's January 1fst 1970 00:00:00. But in our case, we are getting the difference between 2 dates from EPOCH, so you get the result in microsecond, you don't have anything more to do. It just like if you have `X = EPOCH + T1 ; Y = EPOCH + T2`, in this case `T2 - T1` (what you have) is same as `Y - X`. – Holt May 08 '14 at 12:56