STRFTIME(3C) | Standard C Library Functions | STRFTIME(3C) |
strftime, strftime_l, cftime, ascftime - convert date and time to string
#include <time.h> size_t strftime(char *restrict s, size_t maxsize,
const char *restrict format,
const struct tm *restrict timeptr);
size_t strftime_l(char *restrict s, size_t maxsize,
const char *restrict format,
const struct tm *restrict timeptr, locale_t loc);
int cftime(char *s, char *format, const time_t *clock);
int ascftime(char *s, const char *format,
const struct tm *timeptr);
The strftime(), strftime_l(), ascftime(), and cftime() functions place bytes into the array pointed to by s as controlled by the string pointed to by format. The format string consists of zero or more conversion specifications and ordinary characters. A conversion specification consists of a '%' (percent) character and one or two terminating conversion characters that determine the conversion specification's behavior. All ordinary characters (including the terminating null byte) are copied unchanged into the array pointed to by s. If copying takes place between objects that overlap, the behavior is undefined. For strftime(), no more than maxsize bytes are placed into the array. The strftime_l() function behaves identically to strftime() function, but instead of operating in the current locale, it operates in the locale specified by loc.
If format is NULL, then the locale's default format is used. For strftime() the default format is the same as %c; for cftime() and ascftime() the default format is the same as %+. cftime() and ascftime() first try to use the value of the environment variable CFTIME, and if that is undefined or empty, the default format is used.
Each conversion specification is replaced by appropriate characters as described in the following list. The appropriate characters are determined by the LC_TIME category of the program's locale and by the values contained in the structure pointed to by timeptr for strftime() and ascftime(), and by the time represented by clock for cftime().
%%
%a
%A
%b
%B
%c
%a %b %e %H:%M:%S %Y
Other locales may have different locale-specific formats.
%C
%d
%D
%e
%F
%g
%G
%h
%H
%I
%j
%k
%l
%m
%M
%n
%p
%r
%R
%s
%S
%t
%T
%u
%U
%v
%V
%w
%W
%x
%X
%y
%Y
%z
%Z
%+
If a conversion specification does not correspond to any of the above or to any of the modified conversion specifications listed below, the behavior is undefined and 0 is returned.
The difference between %U and %W (and also between modified conversion specifications %OU and %OW) lies in which day is counted as the first of the week. Week number 1 is the first week in January starting with a Sunday for %U or a Monday for %W. Week number 0 contains those days before the first Sunday or Monday in January for %U and %W, respectively.
Some conversion specifications can be modified by the E and O modifiers to indicate that an alternate format or specification should be used rather than the one normally used by the unmodified conversion specification. If the alternate format or specification does not exist in the current locale, the behavior will be as if the unmodified specification were used.
%Ec
%EC
%Eg
%EG
%Ex
%EX
%Ey
%EY
%Od
%Oe
%Og
%OH
%OI
%Om
%OM
%OS
%Ou
%OU
%Ow
%OW
%Oy
These routines produce output that is formatted according to the LC_TIME locale category. They use either the current locale, or in the case of strftime_l(), the locale supplied by loc.
Local time zone information is used as though tzset(3C) were called.
These functions return the number of characters placed into the array pointed to by s, not including the terminating null character. If the total number of resulting characters including the terminating null character is more than maxsize, strftime() returns 0 and the contents of the array are indeterminate.
Example 1 An example of the strftime() function.
The following example illustrates the use of strftime() for the POSIX locale. It shows what the string in str would look like if the structure pointed to by tmptr contains the values corresponding to Thursday, August 28, 1986 at 12:44:36.
strftime(str, strsize, "%A %b %d %j", tmptr);
This results in str containing "Thursday Aug 28 240".
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
CSI | Enabled |
Interface Stability | See below. |
MT-Level | MT-Safe |
Standard | See below. |
The strftime() and strftime_l() functions are Standard. cftime() and ascftime() functions are Committed.
For strftime() and strftime_l(), see standards(7).
date(1), ctime(3C), mktime(3C), newlocale(3C), setlocale(3C), strptime(3C), tzset(3C), uselocale(3C), TIMEZONE(5), zoneinfo(5), attributes(7), environ(7), standards(7)
The conversion specification for %V was changed in the Solaris 7 release. This change was based on the public review draft of the ISO C9x standard at that time. Previously, the specification stated that if the week containing 1 January had fewer than four days in the new year, it became week 53 of the previous year. The ISO C9x standard committee subsequently recognized that that specification had been incorrect.
The conversion specifications for %g, %G, %Eg, %EG, and %Og were added in the Solaris 7 release. This change was based on the public review draft of the ISO C9x standard at that time. The %g and %G specifications were adopted in the formal standard. The other two were not, and should not be used in portable applications.
The conversion specification for %u was changed in the Solaris 8 release. This change was based on the XPG4 specification.
If using the %Z specifier and zoneinfo timezones and if the input date is outside the range 20:45:52 UTC, December 13, 1901 to 03:14:07 UTC, January 19, 2038, the timezone name may not be correct.
The conversion specification for %+ was added in illumos. It is not part of any standard, although it is available on a number of other platforms. Its use is discouraged for conforming applications.
November 8, 2020 | OmniOS |