The strftime() Function

Being a C nerd, I occasionally browse the C library to discover new functions. Such an exercise is a must, not just for C but for any programming language. That’s because programmers can become complacent and rely upon the same old tricks. Only by reviewing the libraries, or by looking at other programmers’ code, can you learn new things.

Recently, I stumbled upon the strftime() function, which most likely stands for “string-formatted time.” I’ve never had a use for this function. That’s because I use the tm structure and a call to localtime() to create time strings. As a review, here’s how that process works:

1. First, get the current time and save it in a time_t variable:

time(&tictoc);

tictoc is my favorite time_t variable name.

2. Second, fill a tm structure with the current time’s values:

now = localtime(&tictoc);

Use the time_t variable returned from time() in the localtime() function. The now variable (again, a favorite) is a pointer declared as struct tm *now. The structure contains fields for each time element; you can look up the specifics in my books.

3. Use the now structure to manipulate or display the time values stored in the array.

For example, if I wanted to display a string for the current date, I use something like:

    printf("Today is %02d-%02d-%02d\n",
            now->tm_year-100,
            now->tm_mon+1,
            now->tm_mday);

This function outputs:

Today is 17-04-08

for today’s date, April 8, 2017.

The strftime() function makes that type of output easier, especially because it does the year calculation for you (no need to add 1900 or subtract 100), plus it contains placeholders for the month and day strings, preformatted and common date and time strings, time zone specifications, locality details, and other stuff. All the options are found on the strftime() man page, if you’re curious.

The strftime() function is declared in the time.h header file. Here is the man page format:

size_t strftime(char *restrict s, size_t maxsize, const char *restrict format, const struct tm *restrict timeptr);

The four arguments are:

s a buffer to hold the resulting time string.
maxsize the size of the buffer s.
format a formatting string, including the strftime() placeholders as well as other text.
timeptr a pointer to a tm structure, such as one returned from the localtime() function.

In the following code, I use the strftime() function to display the current date as the day string, month string, date, and year:

#include <stdio.h>
#include <time.h>

int main()
{
    time_t tictoc;
    struct tm *now;
    char time_string[64];

    /* get the current clock tick */
    time(&tictoc);
    /* fill tm structure */
    now = localtime(&tictoc);
    /* create a formatted time string */
    strftime(time_string,64,"%A, %B %d, %Y",now);

    printf("Today is %s\n",time_string);

    return(0);
}

Here is sample output:

Today is Saturday, April 8, 2017

The strftime() function presents the day of the week and the month as strings, thanks to the %A and %B placeholders. I could have coded the same thing myself, but strftime() saves a lot of overhead. And because I need to call localtime() to fill the tm structure anyway, using strftime() makes a lot of sense.

Leave a Reply