C program function ctime() returns wrong time/date - out by 3 days

70 Views Asked by At
#include<stdio.h>
#include<time.h>

int main(void){
    time_t now = time(NULL);
    printf("%s\n", ctime(&now));
    return 0;
}

This is compiled using GCC and run. Result: Wed May 10 19:13:18 2023 Actual: Sat May 13 09:45:37 2023 (PC system time as correctly displayed) Why the difference? The code could not be simpler!

Environment: I'm running Windows Subsystem for Linux WSL2 on a Windows 11 PC. I don't know if that is significant

Tried researching for similar problems - but this is not a UTC/local time issue.

1

There are 1 best solutions below

2
Nagy K. Aly On

can you run the following command and print the output ?

sudo hwclock -s