Node:Formatting Calendar Time, Next:Parsing Date and Time, Previous:High Accuracy Clock, Up:Calendar Time
The functions described in this section format calendar time values as
strings. These functions are declared in the header file time.h
.
char * asctime (const struct tm *brokentime) | Function |
The 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: The abbreviations for the months are: The return value points to a statically allocated string, which might be
overwritten by subsequent calls to |
char * asctime_r (const struct tm *brokentime, char *buffer) | Function |
This function is similar to asctime but instead of placing the
result in a static buffer it writes the string in the buffer pointed to
by the parameter buffer. This buffer should have room
for at least 26 bytes, including the terminating null.
If no error occurred the function returns a pointer to the string the
result was written into, i.e., it returns buffer. Otherwise
return |
char * ctime (const time_t *time) | Function |
The ctime function is similar to asctime , except that you
specify the calendar time argument as a time_t simple time value
rather than in broken-down local time format. It is equivalent to
asctime (localtime (time))
|
char * ctime_r (const time_t *time, char *buffer) | Function |
This function is similar to ctime , but places the result in the
string pointed to by buffer. It is equivalent to (written using
gcc extensions, see Statement Exprs):
({ struct tm tm; asctime_r (localtime_r (time, &tm), buf); }) If no error occurred the function returns a pointer to the string the
result was written into, i.e., it returns buffer. Otherwise
return |
size_t strftime (char *s, size_t size, const char *template, const struct tm *brokentime) | Function |
This function is similar to the 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
The default action is to pad the number with zeros to keep it a constant width. Numbers that do not have a range indicated below are never padded, since there is no natural width for them. Following the flag an optional specification of the width is possible. This is specified in decimal notation. If the natural size of the output is of the field has less than the specified number of characters, the result is written right adjusted and space padded to the given size. An optional modifier can follow the optional flag and width specification. The modifiers, which are POSIX.2 extensions, are:
If the format supports the modifier but no alternate representation is available, it is ignored. The conversion specifier ends with a format specifier taken from the
following list. The whole
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, Warning: This convention for the return value which is prescribed
in ISO C can lead to problems in some situations. For certain
format strings and certain locales the output really can be the empty
string and this cannot be discovered by testing the return value only.
E.g., in most locales the AM/PM time format is not supported (most of
the world uses the 24 hour time representation). In such locales
buf[0] = '\1'; len = strftime (buf, bufsize, format, tp); if (len == 0 && buf[0] != '\0') { /* Something went wrong in the strftime call. */ ... } If s is a null pointer, According to POSIX.1 every call to For an example of |
size_t wcsftime (wchar_t *s, size_t size, const wchar_t *template, const struct tm *brokentime) | Function |
The wcsftime function is equivalent to the strftime
function with the difference that it operates on wide character
strings. The buffer where the result is stored, pointed to by s,
must be an array of wide characters. The parameter size which
specifies the size of the output buffer gives the number of wide
character, not the number of bytes.
Also the format string template is a wide character string. Since
all characters needed to specify the format string are in the basic
character set it is portably possible to write format strings in the C
source code using the The The return value of |