| STRFTIME(3) | Library Functions Manual | STRFTIME(3) |
strftime — format
date and time
Standard C Library (libc, -lc)
#include
<time.h>
size_t
strftime(char * restrict buf,
size_t maxsize, const char * restrict
format, const struct tm * restrict timeptr);
size_t
strftime_l(char * restrict buf,
size_t maxsize, const char * restrict
format, const struct tm * restrict timeptr,
locale_t loc);
size_t
strftime_z(const timezone_t tz,
char * restrict buf, size_t
maxsize, const char * restrict format,
const struct tm * restrict timeptr);
size_t
strftime_lz(const timezone_t tz,
char * restrict buf, size_t
maxsize, const char * restrict format,
const struct tm * restrict timeptr,
locale_t loc);
The
strftime()
function formats the information from timeptr into the
array pointed to by buf according to the string
pointed to by format.
The function
strftime_l()
does the same as strftime() but takes an explicit
locale specified in the locale_t
loc argument, rather than using the current
locale.
The function
strftime_z()
is similar to strftime(), but uses an explicit
timezone specified in the const timezone_t
tz argument, instead of using the default from the
environment.
The function
strftime_lz()
does the same as strftime() but takes both an
explicit timezone and locale arguments.
The format string consists of zero or more
conversion specifications and ordinary characters. All ordinary characters
are copied directly into the array. A conversion specification consists of a
percent sign ‘%’ and one other
character.
No more than maxsize
characters will be placed into the array. If the total number of resulting
characters, including the terminating NUL character, is not more than
maxsize,
strftime()
returns the number of characters in the array, not counting the terminating
NUL. Otherwise, zero is returned and the buffer contents are
indeterminate.
Each conversion specification is replaced by the characters as
follows which are then copied into the array. The characters depend on the
values of zero or more members of timeptr as specified
by brackets in the description. If a bracketed member name is followed by
“+”, strftime can use the named member
even though POSIX.1-2024 does not list it; if the name is followed by
“-”, strftime ignores the member even
though POSIX.1-2024 lists it which means portable code should set it. For
portability, timeptr should be initialized as if by a
successful call to gmtime(3),
localtime(3),
mktime(3),
timegm(3), or similar
functions.
%Atm_wday]%atm_wday]%Btm_mon]%b
or %h%C%ctm_year, tm_yday,
tm_mon, tm_mday,
tm_wday, tm_hour,
tm_min, tm_sec,
tm_gmtoff, tm_zone,
tm_isdst -].%D%m/%d/%y”. Although used in the
United States for current dates, this format is ambiguous elsewhere and
for dates that might involve other centuries.
[tm_year, tm_mon,
tm_mday]%dtm_mday]%d%E*
%O*Additionally %OB implemented to represent alternative months names (used standalone, without day mentioned).
%etm_mday]%F%Y-%m-%d” (the ISO
8601 date format). [tm_year,
tm_mon, tm_mday]%G%V conversion specification
[tm_year, tm_yday,
tm_wday]%g%V’ conversion specification.
[tm_year, tm_yday,
tm_wday]%Htm_hour]%Itm_hour]%jtm_yday]%ktm_hour]%ltm_hour]%Mtm_min]%mtm_mon]%n%ptm_hour]%R%H:%M”.
[tm_hour, tm_min]%r%Stm_sec]%sstrftime to set errno to
EINVAL or EOVERFLOW and
return 0 if the number of seconds would be negative or out of range for
Portable code should therefore format a value directly via something like
snprintf(3) instead of via
localtime(3) followed by
strftime with "%s".
[tm_year, tm_mon,
tm_mday, tm_hour,
tm_min, tm_sec,
tm_gmtoff +, tm_isdst
-].%T%H:%M:%S”.
[tm_hour, tm_min,
tm_sec]%t%Utm_wday,
tm_yday, tm_year -]%utm_wday]%V%G’ conversion specification.
[tm_year, tm_yday,
tm_wday]%v%e-%b-%Y”.
[tm_year, tm_yday,
tm_wday]%Wtm_yday,
tm_wday]%wtm_year,
tm_yday, tm_wday]%Xtm_year -, tm_yday -,
tm_mon -, tm_mday -,
tm_wday -, tm_hour,
tm_min, tm_sec,
tm_gmtoff, tm_zone,
tm_isdst -].%xtm_year, tm_yday,
tm_mon, tm_mday,
tm_wday, tm_hour -,
tm_min -, tm_sec -,
tm_gmtoff -, tm_zone -,
tm_isdst -].%Ytm_year]%ytm_year]%Ztm_zone,
tm_isdst -]%z[-]”.%+tm_year, tm_yday,
tm_mon, tm_mday,
tm_wday, tm_hour,
tm_min, tm_sec,
tm_gmtoff, tm_zone]%-*%_*%0*%%%’.As a side effect, strftime also behaves as
if tzset(3) were called. This
is for compatibility with older platforms, as required by POSIX; it is not
needed for strftime own use.
If the conversion is successful, strftime
returns the number of bytes placed into the array, not counting the
terminating NUL; errno is
unchanged if the returned value is zero. Otherwise,
errno is set to indicate the error, zero is returned,
and the array contents are unspecified.
This function fails if:
The strftime() function conforms to
ISO/IEC 9899:1999 (“ISO C99”).
The ‘%C’,
‘%D’,
‘%e’,
‘%g’,
‘%G’,
‘%h’,
‘%k’,
‘%l’,
‘%n’,
‘%r’,
‘%R’,
‘%s’,
‘%t’,
‘%T’,
‘%u’,
‘%V’, and
‘%v’ conversion specifications are
extensions.
Use of the ISO 8601 conversions may produce non-intuitive results. Week 01 of a year is per definition the first week which has the Thursday in this year, which is equivalent to the week which contains the fourth day of January. In other words, the first week of a new year is the week which has the majority of its days in the new year. Week 01 might also contain days from the previous year and the week before week 01 of a year is the last week (52 or 53) of the previous year even if it contains days from the new year. A week starts with Monday (day 1) and ends with Sunday (day 7). For example, the first week of the year 1997 lasts from 1996-12-30 to 1997-01-05.
There is no conversion specification for the phase of the moon.
The strftime() function does not correctly
handle multibyte characters in the format
argument.
A return value of zero does not necessarily indicate an error. If
the resulting string is an empty string, the result value is zero and it is
not possible to distinguish between success and error. For example, in many
locales %p yields an empty string. This problem can
be avoided by inserting an extra space at the beginning of the format string
and then skipping over it or removing it from the result.
| April 6, 2025 | NetBSD 11.0 |