O'Reilly logo

C in a Nutshell by Tony Crawford, Peter Prinz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

strftime

Synopsis

Generates a formatted time and date string

#include <time.h>
size_tstrftime( char * restrict s, size_t n,
                 const char * restrict format,
                 const struct tm * restrict timeptr );

The strftime() function converts date and time information from a struct tm object addressed by the last pointer argument into a character string, following a format specified by the string addressed by the pointer argument format. The strftime() function stores the resulting string in the buffer addressed by the first pointer argument, without exceeding the buffer length specified by the second argument, n. The locations that strftime() reads from and writes to using its restricted pointer parameters must not overlap.

Typically, the struct tm object is obtained by calling localtime() or gmtime(). For a description of this structure type, see mktime() in this chapter.

The generation of the output string is governed by the format string. In this way, strftime() is similar to the functions in the printf() family. The format string consists of ordinary characters, which are copied to the output buffer unchanged, and conversion specifications, which direct strftime() to convert a data item from the struct tm object and include it in the output string.

Conversion specification syntax

The conversion specifications have the following syntax:

%[modifier]specifier

The modifier, if present, instructs strftime() to use an alternative, locale-specific conversion for the specified data item, and is either

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required