I asked a similar question recently, however none of the responses ended up working. The main difference now is that I have the logic for the program yet I still don't know why the clock runs faster than expected.
Anyway, I have a program that should slow down the speed of the windows clock by whatever percent I request. For some reason, it appears to make the clock run faster than I expected and I can't figure out why. The issue doesn't appear to be in the logic, as I've tested it with a large variety of numbers. I believe the issue lies in the optimization but I could be wrong
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <sysinfoapi.h>
int main() {
int timeFactor;
SYSTEMTIME lt;
std::cout << "Enter a number 1-100: ";
std::cin >> timeFactor;
GetLocalTime(<);
long timeBefore = lt.wMilliseconds;//975
while (true) {
Sleep(100);
GetLocalTime(<);//025
long SC = lt.wMilliseconds;
long timeDifference = lt.wMilliseconds - timeBefore;//-950
if (timeDifference < 0)
{
long timeToSet = ((timeFactor / 100.0f) * (((timeBefore -1000) * -1) + SC)) + timeBefore;//1000
if (timeToSet >= 1000)
{
timeToSet -= 1000;
lt.wMilliseconds = timeToSet;
}else
{
lt.wMilliseconds = timeToSet;
lt.wSecond -= 1;
}
}else
{
long timeToSet = ((timeFactor / 100.0f) * timeDifference) + timeBefore;
lt.wMilliseconds = timeToSet;
}
SetLocalTime(<);
printf("The time is: %02d:%02d:%02d.%02d\n", lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
GetLocalTime(<);
timeBefore = lt.wMilliseconds;
}
}