The strftime() function in C is used to format date and time values according to specified formatting rules. It is defined in the <time.h> header file and uses the struct tm structure to represent the date and time components.
- Converts a struct tm time value into a formatted charact er string.
- Supports format specifiers such as %d, %m, %Y, %H, %M, and %a to customize the output format.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define Size 50
int main ()
{
time_t t ;
struct tm *tmp ;
char MY_TIME[Size];
time( &t );
//localtime() uses the time pointed by t ,
// to fill a tm structure with the
// values that represent the
// corresponding local time.
tmp = localtime( &t );
// using strftime to display time
strftime(MY_TIME, sizeof(MY_TIME), "%x - %I:%M%p", tmp);
printf("Formatted date & time : %s\n", MY_TIME );
return(0);
}
Formatted date & time : 03/20/17 - 02:55PMSyntax
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
1. Parameters
Explain the four parameters clearly:
s: Character array where the formatted date and time is stored.max: Maximum number of characters that can be written tos.format: Format string containing specifiers such as%d,%m,%Y, etc.tm: Pointer to thestruct tmcontaining the date and time information.
2. Return Value
This is important for strftime().
- Returns the number of characters written to the destination array, excluding the terminating null character.
- Returns
0if the formatted result cannot fit within the specified maximum size.
strftime() function formats the broken-down time tm according to the formatting rules specified in format and store it in character array s.
Some format specifiers for strftime() are shown as follows:
%x = Preferred date representation
%I = Hour as a decimal number (12-hour clock).
%M = Minutes in decimal ranging from 00 to 59.
%p = Either "AM" or "PM" according to the given time value, etc.
%a = Abbreviated weekday name
%^a = Abbreviated weekday name in capital letters
%A = Full weekday name
%b = Abbreviated month name
%^b = Abbreviated month name in capital letters
%B = Full month name March
%c = Date and time representation
%d = Day of the month (01-31)
%H = Hour in 24h format (00-23)
%I = Hour in 12h format (01-12)
%j = Day of the year (001-366)
%m = Month as a decimal number (01-12)
%M = Minute (00-59)
Structure struct tm is defined in time.h as follows:
struct tm
{
int tm_sec; // seconds
int tm_min; // minutes
int tm_hour; // hours
int tm_mday; // day of the month
int tm_mon; // month
int tm_year; // The number of years since 1900
int tm_wday; // day of the week
int tm_yday; // day in the year
int tm_isdst; // daylight saving time
};
Advantages of strftime()
The function provides a flexible way to convert date and time information into human-readable strings.
- Supports multiple predefined format specifiers for customizing date and time output.
- Helps generate consistent date and time formats for display, logging, and reports.
Limitations of strftime()
Although strftime() is flexible, it has some limitations when formatting date and time values.
- The output depends on the specified format and the locale settings of the system.
- If the destination buffer is too small, the function returns 0 and the formatted result may not be stored completely.