Why the clock under Windows 2K/XP/2K3 show “four quick seconds and one slow second”?

If you click the clock at the right-bottom corner of Windows, it will open a small animated clock, which has seconds showed on it. But if you watch it carefully for a few moment, you will notice that every four seconds are the same length, but the fifth one is much longer than that. What happened?

The reason is quite interesting. As we know, in Windows C++ coding, we use WM_TIMER to set the time trigger for many events. The resolution of WM_TIMER is at about 50ms, which cannot be used under time-critical real time processing. But for a clock, it is enough. In the code of clock application, the following code is used to set the Timer(You can also get these pieces of code from VC98 CDROM Samples, if you still have it, I doubt):

 SetTimer (hWnd, TimerID, OPEN_TLEN, 0L);

OPEN_TLEN is the length of the timer, it is a constant. So when we look at clock.h, you will get the number, which is 450.

What does this 450 mean? This means, every 450ms the timer will be triggered, it will detect time changes and redraw the clock.

So, we can draw a small table to make it clear, the first row is the number of trigger, the second row is the time:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
450 900 1350 1800 2250 2700 3150 3600 4050 4500 4950 5400 5850 6300 6750 7200 7650 8100 8550 9000 9450 9900

You will notice that within the first second, the timer gets triggered twice, and it will be updated at the 3rd trigger. The same situation is for 2nd, 3rd and 4th second. But within the fifth second, which is marked as red, the timer gets triggered for three times, and the clock will be updated at 4th trigger.

So this is why the fifth second update is much longer than the other four.

Why this have to be 450? Well, I’m not a developer nor a program manager, I don’t know their decision. But I guess this is a consideration about performance and resources. After all, how many people will use that clock as an accurate timer?

BTW – the clock application under Vista/2K8 is completely rewritten, so you may not have that problem. But if you watch it for a minute, you will still notice a quite “quick” second. :)