What is wcsftime in C?
The wcsftime function is defined in the wchar.h header file in C. It converts the date and time stored in a time parameter into a wide string following the provided format.
Syntax
The syntax of the wcsftime function is as follows:
Parameters
wcsftime takes in four arguments:
-
dest: The pointer to the destination string to store the constructed wide string -
len: The maximum number of characters to copy into the destination string pointed to bydest. -
time: A pointer to atmobject containing time broken down to individual components. The declaration for the structuretmis as follows:
| Elements | Represent |
|---|---|
tm_sec |
Seconds passed in a minute |
tm_min |
Minutes passed in an hour |
tm_hour |
Hours passed in a day |
tm_mday |
Months passed in a year |
tm_mon |
Months passed in a year |
tm_wday |
Days passed in a week |
tm_yday |
Days passed in a year |
tm_year |
Years passed since 1900 |
tm_isdst |
Daylight saving hours |
format: the pointer to a wide string containing the time format to be used. This parameter can contain normal characters as well as several different specifiers. These specifiers determine which component of time will be represented in what way in the final wide string. Following is a table of some of the most commonly used specifiers and what they represent:
| Specifier | Represents |
|---|---|
%a |
Abbreviated name of the weekday (Mon) |
%^a |
Abbreviated name of the weekday in all capitals (MON) |
%A |
Full name of weekday (Monday) |
%b |
Abbreviated name of month (Jan) |
%^b |
Abbreviated name of month name in all capitals (JAN) |
%B |
Full name of month (January) |
%c |
Date and time (Mon Jan 01 01:01:01 2021) |
%d |
Day of the month (01-31) |
%H |
Hour in 24h format (00-23) |
%I |
Hour in 12h format (00-11) |
%j |
Day of the year (000-365) |
%m |
Month of the year (01-12) |
%M |
Minute in an hour (00-59) |
%S |
Seconds in an hour (00-59) |
%w |
Weekday as a number (0-6) |
%x |
Condensed date (01/01/21) |
%y |
Last two digits of the year (00-99) |
%Y |
Complete year (2021) |
Return value
wcsftime has two possible return values:
- A zero if the converted wide string has more characters than the given
lenparameter. - The number of characters in the converted string is returned if there is no error in execution and the character count is less than the specified
len.
Example
In this example, we first create and populate a tm object and a wchar_t array of size 40 (destination array). Next, we feed both these to the wcstrftime function, the destination array’s size, and the format. The format we use here is “My meeting is on [%x] at {%H:%M}”. Excluding the normal characters, the rest is basically the condensed date enclosed by square brackets followed by the Hours and minutes, separated by a colon, and enclosed in curly braces:
#include <stdio.h>#include <time.h>#include <wchar.h>int main(void){wchar_t conv_tm[40];// variable to give wcsftime as parameterstruct tm mytm;// populating the tm objectmytm.tm_year = 2021 - 1900; // 2021mytm.tm_isdst = 0; // day light saving set to 0mytm.tm_mon = 1; // Januarymytm.tm_mday = 0; // Sunday// setting the time as 00:00:00mytm.tm_hour = 12;mytm.tm_min = 0;mytm.tm_sec = 0;wcsftime(conv_tm, 40, L"My meeting is on [%x] at {%H:%M}", &mytm);printf("%ls\n", conv_tm);}
Free Resources