format the time into a string
#include <time.h> size_t strftime( char *s, size_t maxsize, const char *format, const struct tm *timeptr );
The strftime() function formats the time in the argument timeptr into the array pointed to by the argument s, according to the format argument.
The format string consists of zero or more directives and ordinary characters. A directive consists of a '%' character followed by a character that determines the substitution that is to take place. All ordinary characters are copied unchanged into the array. No more than maxsize characters are placed in the array. The format directives %D, %h, %n, %r, %t, and %T are from POSIX.
When the %Z directive is specified, the tzset() function is called.
If the number of characters to be placed into the array is less than maxsize, the strftime() function returns the number of characters placed into the array pointed to by s, not including the terminating null character. Otherwise, zero is returned.
When an error has occurred, errno contains a value that indicates the type of error that has been detected.
#include <stdio.h> #include <time.h> void main() { time_t time_of_day; char buffer[ 80 ]; time_of_day = time( NULL ); strftime( buffer, 80, "Today is %A %B %d, %Y", localtime( &time_of_day ) ); printf( "%s\n", buffer ); }
produces the output:
Today is Friday December 25, 1987
ANSI, POSIX
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |
asctime(), clock(), ctime(), difftime(), errno, gmtime(), localtime(), mktime(), setlocale(), time(), tzset()
The tm structure is described in the section on <time.h> in the Header Files chapter.