The functions described in this section format time values as strings. These functions are declared in the header file `time.h'.
asctime
function converts the broken-down time value that brokentime points to into a string in a standard format:
"Tue May 21 13:46:22 1991\n"
The abbreviations for the days of week are: `Sun', `Mon', `Tue', `Wed', `Thu', `Fri', and `Sat'.
The abbreviations for the months are: `Jan', `Feb', `Mar', `Apr', `May', `Jun', `Jul', `Aug', `Sep', `Oct', `Nov', and `Dec'.
The return value points to a statically allocated string, which might be overwritten by subsequent calls to any of the date and time functions. (But no other library function overwrites the contents of this string.)
ctime
function is similar to asctime
, except that the time value is specified as a time_t
calendar time value rather than in broken-down local time format. It is equivalent to
asctime (localtime (time))
ctime
sets the variable tzname
, because localtime
does so. See Time Zone Functions.
sprintf
function (see Formatted Input), but the conversion specifications that can appear in the format template template are specialized for printing components of the date and time brokentime according to the locale currently specified for time conversion (see Locales). Ordinary characters appearing in the template are copied to the output string s; this can include multibyte character sequences. Conversion specifiers are introduced by a `%' character, and are replaced in the output string as follows:
%a
%A
%b
%B
%c
%d
01
to 31
).
%H
00
to 23
).
%I
01
to 12
).
%j
001
to 366
).
%m
01
to 12
).
%M
%p
%S
%U
%W
%w
0
.
%x
%X
%y
00
to 99
).
%Y
%Z
%%
The size parameter can be used to specify the maximum number of characters to be stored in the array s, including the terminating null character. If the formatted time requires more than size characters, the excess characters are discarded. The return value from strftime
is the number of characters placed in the array s, not including the terminating null character. If the value equals size, it means that the array s was too small; you should repeat the call, providing a bigger array.
If s is a null pointer, strftime
does not actually write anything, but instead returns the number of characters it would have written.
For an example of strftime
, see Time Functions Example.