ntp-tools / set_time_back.c /
ad01ac7 2 months ago
1 contributor
26 lines | 0.525kb
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

int main() {
    struct timeval tv;

    // Get current time
    if (gettimeofday(&tv, NULL) != 0) {
        perror("gettimeofday");
        return 1;
    }

    // Subtract 1 hour (3600 seconds)
    tv.tv_sec -= 3600;

    // Set the new time
    if (settimeofday(&tv, NULL) != 0) {
        perror("settimeofday (are you root?)");
        return 1;
    }

    printf("System time set to 1 hour earlier: %s", ctime(&tv.tv_sec));
    return 0;
}