Quoting a very clear & self-explanatory reply from Rahul on a similar question:
The issue you're experiencing with the ctime() function returning a wrong time/date can be explained by the difference in the underlying system clock between your Windows Subsystem for Linux (WSL2) environment and your PC's system time.
WSL2 operates in a virtualized environment, and it syncs its time with the Windows host system. However, there can be small discrepancies in the clock synchronization due to various factors like system clock drift or timing issues in the virtualization layer.
In your case, it seems that the WSL2 environment is lagging behind the actual system time on your Windows 11 PC by approximately three days. When you run the C program within WSL2, it retrieves the system time from the underlying host system, which has this three-day difference.
To address this issue, you can try the following steps:
Update the time in your WSL2 environment: Open a terminal within WSL2 and run the following command to synchronize the time with the Windows host:
sudo hwclock -s This command sets the WSL2 hardware clock to match the Windows system time.
This works, but my next question is how can I build this into a C program so the clock syncs without the need for user input without divulging sudo password or other security problem.
added:
#include<stdlib.h>
system("sudo hwclock -s")
to my code, but it requires a root password. I'm guessing there is not a secure way around this. By the way, the time difference is not small. It appears that the WSL2 clock does not increment while the PC is asleep