fork download
  1. #define _XOPEN_SOURCE
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <errno.h>
  5. #include <string.h>
  6.  
  7. int main(void) {
  8.  
  9. const char *date = "20241101";
  10.  
  11. struct tm broken_time;
  12. memset(&broken_time, 0, sizeof(broken_time));
  13. char* res = strptime(date, "%Y%m%d", &broken_time);
  14. if (!res || *res != '\0') {
  15. printf("failed");
  16. return 0;
  17. }
  18. broken_time.tm_mday--; // one day before
  19.  
  20. time_t normalized = mktime(&broken_time);
  21. if (normalized == -1) {
  22. printf("mktime error: %s", strerror(errno));
  23. return 0;
  24. }
  25. struct tm final;
  26. gmtime_r(&normalized, &final);
  27. char buf[16];
  28. if (strftime(buf, sizeof(buf), "%Y%m%d", &final) == 0) {
  29. printf("strftime %s", strerror(errno));
  30. }
  31. printf("%s\n", buf); // should be 20241031
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
20241031