#include #include #include #include int main(int argc, char **argv) { struct tm tm = { .tm_isdst = -1 }; if (!strptime(argv[1], "%Y-%m-%d %H:%M:%S", &tm)) { perror("strptime"); return 1; } errno = 0; time_t t = mktime(&tm); if (t==-1 && errno) { perror("mktime"); return 1; } char buf[100]; strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", &tm); printf("normalized input: %s\n", buf); struct tm tm2 = tm; tm2.tm_mday++; errno = 0; if (mktime(&tm2)==-1 && errno) { perror("mktime day+1"); } else { strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", &tm2); printf("+1day per mktime: %s\n", buf); } localtime_r(&(time_t){t+86400}, &tm2); strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", &tm2); printf("+1day via time_t: %s\n", buf); tm2 = tm; tm2.tm_mday--; errno = 0; if (mktime(&tm2)==-1 && errno) { perror("mktime day-1"); } else { strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", &tm2); printf("-1day per mktime: %s\n", buf); } localtime_r(&(time_t){t-86400}, &tm2); strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", &tm2); printf("-1day via time_t: %s\n", buf); }