On this page
strftime
Prev: timespec_get | Up: time.h | Next: gmtime
Rel: time.h, math, time & assert Guide
Ref: cppreference — strftime
Tags: #public #prog #c #stdlib #time #tier/core
Include
#include <time.h>
Signature & Overview
size_t strftime(char *restrict buf, size_t max, const char *restrict fmt, const struct tm *restrict tm);
Formats a broken-down struct tm into buf safely with bounds checking (ISO-8601: "%Y-%m-%dT%H:%M:%SZ").
Notes & Pitfalls
- Returns
0if buffer is too small: Preferred portable way to format ISO-8601 timestamps ("%Y-%m-%dT%H:%M:%SZ").
Example
#include <assert.h>
#include <string.h>
#include <time.h>
int main(void) {
time_t epoch = 0;
char out[32];
size_t n = strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", gmtime(&epoch));
assert(n > 0 && strcmp(out, "1970-01-01T00:00:00Z") == 0);
}
Linked from
Related notes
- tuts/c/stdlib/time/gmtime Linked here; links to this page; shared tags: #c, #prog, #stdlib, #tier, #time
- tuts/c/stdlib/time/timespec_get Linked here; links to this page; shared tags: #c, #prog, #stdlib, #tier, #time
- tuts/c/stdlib/time Linked here; links to this page; shared tags: #c, #prog, #stdlib, #time
- tuts/c/stdlib/time/asctime links to this page; shared tags: #c, #prog, #stdlib, #tier, #time
- tuts/c/stdlib/math_time_and_assert Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/time/time shared tags: #c, #prog, #stdlib, #tier, #time
- tuts/c/stdlib/assert/assert shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/assert/static_assert shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/ctype/isalpha shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/ctype/isdigit shared tags: #c, #prog, #stdlib, #tier