1

I am working on a project where we are collecting data from various sensors and logging various logs.

Mostly all the sensors are logged using TimeGetTime in millisecs which is time measured from start time of PC. But I have one sensor that has data logged in epoch time which is in millisecs .

Now how can I relate the epoch time with TimeGetTime if I had to synchronize all sensor datas together?

GirijaDelphi
  • 23
  • 1
  • 5

1 Answers1

1

One way to convert the epoch time (I assume it's the number of milliseconds (ms) elapsed since [Wikipedia]: Unix time or ([MAN]: time)* 1000) to ms elapsed since Win started, you have to:

  1. Get the number of ms since Win started, using (as you already mentioned) [MSDN]: timeGetTime function (as an alternative you could use [MSDN]: GetTickCount64 function; I didn't study the difference - in terms of precision - between the 2)
  2. Get the current Win time, using [MSDN]: GetSystemTimeAsFileTime function
  3. Since the time retrieved at previous step is (according to [MSDN]: FILETIME structure):

    a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

    you need to convert it to Ux epoch style time. The conversion algorithm is quite straightforward, there are lots of examples on the web, here's one: [SO]: Convert Windows Filetime to second in Unix/Linux (since that result is in seconds, you'll need to multiply it by 1000). This represents the number of ms elapsed since Ux epoch time, til now

  4. By subtracting the value from Step 1 (ms since Win started) from the one from Step 3 (ms since Ux epoch time), you'll get the number of ms between the Ux epoch time and the time Win started. This is the offset that you'll have to subtract from every timestamp that you get from the funky sensor, to be in sync with the timestamps got from all the other sensors.
CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • thanks so much for your reply. yes i am trying to call both _ftime64_s( &timebuffer ); unsigned long lwTime = timeGetTime(); under a critical section so that there will be not more than 1-2 millisecs difference if there is a difference in execution of the functions ,and then will try looking at the difference between two results at frequent intervals of maybe 20 minutes . – GirijaDelphi Jun 09 '17 at 02:18
  • You're welcome :). That's why I ordered Steps _#1_ and _#2_ to be consecutive so any processing done doesn't influence the result. You can perf test it by adding (duplicating) _#1_ between _#2_ and _#3_, so it will be _#2.5_ if you will. By subtracting _#1_ from _#2.5_, you'd see the time difference between the 2 calls (in _ms_), but Id doubt it's notable (well unless the machine (_CPU_) is heavily loaded by other processes). As long as the app doesn't involve threads, I'm not sure you'd need a critical section. – CristiFati Jun 09 '17 at 16:30