#include #include #include #include #include int main(int argc, char *argv[]) { const struct tm date = { .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, .tm_mon = 0, .tm_year = -1899, .tm_wday = 1, .tm_yday = 0, .tm_isdst = -1, .tm_gmtoff = 0, .tm_zone = 0x0 }; const char *fmt = "%E"; const size_t maxsz = 65536u; size_t bufsz = 128; char *buf = NULL; // Mimic g_date_strftime (glib/gdate.c) approach do { buf = (char *)realloc(buf, bufsz); assert(buf); buf[0] = '\1'; // Mark to guess if empty or not enough space size_t len = strftime(buf, bufsz, fmt, &date); if (len != 0 || buf[0] == '\0') break; // OK, done. bufsz *= 2; // Assume more space needed } while (bufsz <= maxsz); // .. up to a limit int rc; if (bufsz <= maxsz) { printf("Date: %s\n", buf); rc = EXIT_SUCCESS; } else { puts("Date: longest date ever :-\\"); rc = EXIT_FAILURE; } free(buf); return rc; }