liveBook · Manning

Strftime

As explained in Modern C, Third Edition

strftime is a C standard-library function that formats a broken-down time into a caller-provided character array according to a format string.

strftime formats a broken-down time stored in struct tm into a caller-provided character array. Like printf, it combines ordinary text with special percent codes, allowing the desired textual representation to be composed explicitly. section 8.6

A formatted date

A call such as strftime(buf, sizeof buf, "%F", &tm) requests a date using %F, equivalent to %Y-%m-%d. Other codes select fields such as seconds, month names, weekday names, time-zone information, or locale-dependent date and time forms. section 8.6

time_t time(time_t *t);
double difftime(time_t time1, time_t time0);
time_t mktime(struct tm tm[1]);
size_t strftime(char s[static 1], size_t max,
                char const format[static 1],
                struct tm const tm[static 1]);
int timespec_get(struct timespec ts[static 1], int base);
int timespec_getres(struct timespec ts[static 1], int base);

Use the declaration to identify the output array, its maximum size, the format string, and the struct tm value being represented. section 8.6

Important formatting choices include:

  • %F produces an ISO 8601-style year-month-day date, while %x, %X, %B, %b, %A, and %a may depend on locale settings. section 8.6
  • Prefer strftime for textual time presentation instead of deprecated asctime, whose static result buffer can be overwritten by later or parallel calls. section 8.6

How Modern C, Third Edition covers it

How deep it goes
Defines the interface and catalogs format codes, but defers locale access and setting to another treatment.
Examples
Uses format strings containing %F, locale-sensitive date and time codes, and the strftime declaration.
What it assumes
Assumes familiarity with C arrays, strings, format strings, and struct tm while deferring pointer rationale.
What you can do after
Apply strftime to compose textual presentations of values held in struct tm.
How it connects
Connects formatted output with printf, struct tm, locale, time zones, ISO 8601, and deprecated asctime.

Also explained in

Compare how 2 books explain strftime →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.