Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working on an File-based history system, whose function is to store and retrieve some history information.

Inside file, I use GetLocalTime() to get the File Start Time, store it on the file header. And then I use GetTickCount() to store the Time Offset of items into file.
Finally, I create a tool to calculate the absolute time with the start and the offset time.
But I found the calculated time is not match with the real absolute time.

for example:

Suppose I start the file at 10:30:28

DWORD x = GetTickCount();

void TimeoutWakeupFucntion()
{
  DWORD y = GetTickCount();

  DWORD Offset = (y >= x) ? (y - x) : (MAXDWORD - x + y);
 
  SYSTEMTIME st;
  GetLocalTime(&st);
}



Then
Time Offset Real Absolute Time
1997 10:30:40.578
2995 10:30:41.583
.
.
.
184986 10:33:44.138

That is to say, the calculated time is always shorter than the real time.

Does anybody know what's wrong?

Thank you so much!
Posted
Comments
[no name] 30-Jun-14 4:59am    
Your code sample is incomplete and your explanation is sketchy so it is not possible to give a definitive answer. I would suggest you edit the question.

1 solution

You are using two different kinds of time sources.

GetTickCount() is a monotonic timer that increments at fixed intervals without any adjustments while GetLocalTime() is a real time clock that is affected by adjustments (Windows may detect an inaccuracy and add small offsets at each tick or even synchronize the clock with other time sources like NTP). As a result, real time clocks may jump forward and even backward.

But I have never seen such big differences. You may extend your program to print the differences and run it for longer times on multiple systems to check if you have a system with very inaccurate timers.

A final tip not related to your question:
You should store times in UTC using GetSystemTime() rather than as local time. Then you did not have problems when the real time jumps when daylight saving time changes. If you want to display times later just convert them to local time.
 
Share this answer
 
Comments
nv3 30-Jun-14 4:53am    
Looks like his timer interrupt is about 0.28% off. Looks kind of strange to me as well. But your explanation is good. 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900