#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') {
return 0;
}
broken_time.tm_mday--; // one day before
time_t normalized
= mktime(&broken_time
); if (normalized == -1) {
return 0;
}
struct tm final;
gmtime_r(&normalized, &final);
char buf[16];
if (strftime(buf
, sizeof(buf
), "%Y%m%d", &final
) == 0) { }
printf("%s\n", buf
); // should be 20241031 return 0;
}
I2RlZmluZSBfWE9QRU5fU09VUkNFCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8dGltZS5oPgojaW5jbHVkZSA8ZXJybm8uaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKaW50IG1haW4odm9pZCkgewoJCgljb25zdCBjaGFyICpkYXRlID0gIjIwMjQxMTAxIjsKCQogICAgc3RydWN0IHRtIGJyb2tlbl90aW1lOwogICAgbWVtc2V0KCZicm9rZW5fdGltZSwgMCwgc2l6ZW9mKGJyb2tlbl90aW1lKSk7CiAgICBjaGFyKiByZXMgPSBzdHJwdGltZShkYXRlLCAiJVklbSVkIiwgJmJyb2tlbl90aW1lKTsKICAgIGlmICghcmVzIHx8ICpyZXMgIT0gJ1wwJykgewogICAgICAgIHByaW50ZigiZmFpbGVkIik7CiAgICAgICAgcmV0dXJuIDA7CiAgICB9CiAgICBicm9rZW5fdGltZS50bV9tZGF5LS07IC8vIG9uZSBkYXkgYmVmb3JlCgogICAgdGltZV90IG5vcm1hbGl6ZWQgPSBta3RpbWUoJmJyb2tlbl90aW1lKTsKICAgIGlmIChub3JtYWxpemVkID09IC0xKSB7CiAgICAJcHJpbnRmKCJta3RpbWUgZXJyb3I6ICVzIiwgc3RyZXJyb3IoZXJybm8pKTsKICAgIAlyZXR1cm4gMDsKICAgIH0KICAgIHN0cnVjdCB0bSBmaW5hbDsKICAgIGdtdGltZV9yKCZub3JtYWxpemVkLCAmZmluYWwpOwogICAgY2hhciBidWZbMTZdOwogICAgaWYgKHN0cmZ0aW1lKGJ1Ziwgc2l6ZW9mKGJ1ZiksICIlWSVtJWQiLCAmZmluYWwpID09IDApIHsKICAgICAgICBwcmludGYoInN0cmZ0aW1lICVzIiwgc3RyZXJyb3IoZXJybm8pKTsKICAgIH0KCXByaW50ZigiJXNcbiIsIGJ1Zik7IC8vIHNob3VsZCBiZSAyMDI0MTAzMQoJcmV0dXJuIDA7Cn0K