C programming strftime time.h


In C programming the <time.h> strftime function allows you to print the time in the format the way you want.It render you the privilege to construct the date and time format for better understanding and nativity using some conversion specifier.For instance using the conversion specifier(%a or %A) you can either shorten or elongate the week day name,another useful property is you can change the time to 24 hours or 12 hours format and append ‘AM’ or ‘PM’ accordingly.The declaration of the function is given below

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

Parameters:
s -This points to the string that holds the date and time in the format specified using the specifier conversion.

maxsize -The maximum number of characters to placed in the ‘s‘ pointed string.

format -This string will consist of any characters that you would like to print out,along with the conversion specifier which specify the format of the date and time.The conversion specifier table is given below.

timeptr -A pointer to ‘struct tm‘ type,this structure holds the required date and time as it’s data members.

Return type
size_t -If the total number of resulting characters including the terminating null character is not more 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 and the contents of the array are indeterminate.

Conversion specifier along with it’s meaning

Conversion specifier
type
‘struct tm’ member nameMeaning
%atm_wdayDay of the week in abbreviated form e.g. Sat,Sun,Mon,etc.
%Atm_wdayDay of the week in full form e.g. Saturday,Sunday,etc.
%b or %htm_monName of the month in abbreviated form e.g. Jan,Feb,etc.
%Btm_monName of the month in full form e.g. January,Febuary,etc.
%callThis is replaced by locale’s date and time in appropriate format.
%Ctm_yearYear divided by 100 as a decimal integer (0-99).
%dtm_mdayDay of the month (01-31).
%Dtm_mon,tm_mday,tm_yearEquivalent to “%m/%d/%y” for instance “07/23/17”
%etm_mdayDay of the month,for single digit the first character is a space (1 – 31)
%Ftm_mon , tm_mday , tm_yearEquivalent to “%Y-%m-%d” e.g. 2017-07-23
%gtm_yearThe year is replaced by the last two digits e.eg 94
%Gtm_yearThe year is full e.g. 1994
%Htm_hourReplaced by 24 hour format (00-23)
%Itm_hourReplaced by 12 hours format (01-12)
%Jtm_ydayThe day of the year (001-366)
%mtm_monReplaced month as decimal number (01-12)
%Mtm_minReplaced by minute (00-59)
%nReplaced by new-line character.
%ptm_hourAM or PM indication with 12 hour clock
%rtm_hour,tm_min,tm_secReplaced by 12 hours clock.
%Rtm_hour,tm_minEquivalent to %H:%M ,e.g. 21:56
%Stm_secReplaced seconds a decimal number (00-60).
%t(none)A horizontal tab
%Ttm_hour,tm_min,tm_secShorthand for %H:%M:%S , e.g.g 21:59:19
%u tm_wdayReplaced week day as decimal digit (1-7)
%Utm_year,tm_wday,tm_ydayWeek day of the year as two digits decimal value (00-53),counting starts from 1st sunday January.
%Vtm_year,tm_wday,tm_ydayWeek day of the year as two decimal digits beginning on the last Monday that falls on or before January 4.
%wtm_wdayDay of the week as decimal digit (0-6) where 0 is Sunday.
%Wtm_year,tm_wday,tm_ydayWeek of the year as two decimal digits (00-53),week 1 begins on the first Monday in January
%xnoneThe date
%XnoneThe time
%y tm_yearThe last two digits of the year, as a decimal number(00 to 99)
%Y tm_yearYear as a decimal number (example:2005)
%z tm_isdstReplaced by the offset from UTC,e.g. “−0430” (meaning 4 hours 30 minutes behind UTC, west of Greenwich),no characters if no time zone is determinable.
%Ztm_isdstreplaced by the locale’s time zone name or abbreviation, or by no characters if no time zone is determinable.
%%noneReplaced by %




Some conversion specifier can be modified by including E or O modifier character to indicate an alternative format or specification.If this alternative format is not supported or does not exist the modifier is ignored.

Conversion specifier
type
Meaning
%EcReplaced by locale date and time e.g. “Sun Jul 23 22:16:06 2017”
%ECReplaced by the name of the base year
%ExReplaced by the alternative date representation.
%EXReplaced by the alternative time representation.
%Ey replaced by the offset from %EC
%EYreplaced by the locale’s full alternative year representation.
%Odreplaced by the locale’s full alternative year representation.
%Oereplaced by the day of the month
%OHreplaced by the hour (24-hour clock).
%OIReplaced by the hour (12-hour clock)
%OmReplaced by the month
%OMReplaced by the minutes.
%OSReplaced by the seconds.
%Oureplaced by the ISO 8601 weekday as a number
%OUreplaced by the week number
%OVreplaced by the ISO 8601 week number
%Owreplaced by the weekday as a number
%OW replaced by the week number of the year
%Oy replaced by the last 2 digits of the year

If the conversion specifier provided is not among one of them the behavior is undefined.

A program is given below to print the date and time in the format,

Sunday Jul 23 10:44:40 PM:

note,the only way to acquaint yourself with the conversion specifier thoroughly is to use them in your program.

Code example

time_t t;

struct tm *tt;

time(&t); //get the calendar time

size_t sz = 45;

char hrdt[45] = “”;

tt=localtime(&t); //convert the calendar time to local time

if ( strftime(hrdt, sz, “Date : %A %h %d %I:%M:%S %p” , tt) );
{
printf( “%s” , hrdt );
}

Output,

Date : Sunday Jul 23 10:48:19 PM


Related links

->C localtime time.h

->C time time.h