#define _XOPEN_SOURCE
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <string.h>

int main(void) {
	
	const char *date = "20241101";
	
    struct tm broken_time;
    memset(&broken_time, 0, sizeof(broken_time));
    char* res = strptime(date, "%Y%m%d", &broken_time);
    if (!res || *res != '\0') {
        printf("failed");
        return 0;
    }
    broken_time.tm_mday--; // one day before

    time_t normalized = mktime(&broken_time);
    if (normalized == -1) {
    	printf("mktime error: %s", strerror(errno));
    	return 0;
    }
    struct tm final;
    gmtime_r(&normalized, &final);
    char buf[16];
    if (strftime(buf, sizeof(buf), "%Y%m%d", &final) == 0) {
        printf("strftime %s", strerror(errno));
    }
	printf("%s\n", buf); // should be 20241031
	return 0;
}
