kookoo121
kookoo121

Reputation: 1136

Why the GetLocalTime(&sys); showed strange result?

I want to get the local time in real time.The code and result is as follow, it showed strange;

int main() {
    while (true) {
        char sentdata[64] = { 0 };
        GetLocalTime(&sys);
        sprintf(sentdata, "A,%02d:%02d:%02d.%03d,B\n", sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);

        cout << sentdata << endl;
    }
    return 0;
}

enter image description here

I tried same code on other PC, the result shown is as following, it can show every millisecond.

enter image description here

Why the tmie is same in some loop? I think the time should be different every loop.

Upvotes: 0

Views: 199

Answers (2)

Daniel Strul
Daniel Strul

Reputation: 1528

This a typical timing behavior on Windows systems, even with the high-resolution clock: time only increases in jumps of about 16 ms.

For accurate timing on Windows, see this answer

Upvotes: 5

HelloWorld123456789
HelloWorld123456789

Reputation: 5359

Your code runs in nanoseconds, while the output is in milliseconds.

Upvotes: 1

Related Questions