The Buffer That Worked In Testing

This prints the same timestamp twice, or garbage, depending on the compiler.

const char *fmt_ts(time_t t) {
    char buf[32];
    struct tm tm;
    localtime_r(&t, &tm);
    strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%S", &tm);
    return buf;                       /* frame dies here */
}

void log_pair(time_t a, time_t b) {
    printf("%s .. %s\n", fmt_ts(a), fmt_ts(b));
}

Four fixes are proposed. Pick the one that leaves log_pair printing two different timestamps, is safe when several threads call fmt_ts at once, and hands the caller nothing to release.

A

Allocate with char *buf = malloc(32); and return that pointer.

B

Change the signature to void fmt_ts(time_t t, char *out, size_t n) and let each caller supply its own array.

C

Declare the buffer static char buf[32];.

D

Return a pointer into a rotating table of four static buffers, advancing an index on each call.