C++

C++ Strftime()

“C++ is an exceptionally versatile language whenever it pertains to carrying out various functions and changing a variable of one type to another. strftime()  is one of the functions which converts the date/time to the string. The string array of length maximum contains the outcome of the strftime() function, which writes the broken-down time tm by the formatting specified format. In time.<h>, the decomposed time object “tm” is implemented. Every formatting requirement is an uninitialized string that might also contain conversion requirements, which have been special character strings that are initiated by the symbol “%” and concluded by another character called a conversion identifier symbol. Common character sequencing makes up all those other special characters.”

Syntax of strftime() Function

The syntax of the strftime() function is mentioned below:

There are four parameters available for the strftime() function, i.e., str, count, format, and time. According to the value of format, the date – time information provided by the time is transformed into an uninitialized multi-byte string and saved in the array identified by str. The written data can only be up to count bytes.

Parameters of strftime() Function

str: Index towards the initial element of the string array used to hold the output.

count: The most bytes that can be written.

format: Address to a multi-byte given string that specifies the conversion’s layout and is uninitialized. The conversion specifier (which starts with % and may be preceded by either E or O) and other regular character types make up the formatted string.

time: Time is used to print the date and time in the output.

Format Specifiers in strftime() Function

Format Specifiers play an important role in the input and output of the data in C++. It serves as a mechanism to inform the translator what kind of information is included in a variable when taking input with cin() or outputting with the cout() function. Following are the format specifiers used in the strftime() function.

Conversion Specifiers Description Used Fields
% To display a character.
n To add a newline character.
t Displays the horizontal tab characters.

Time: The given below format specifiers are used to represent time:

Y Displays the year with four digits, for example, 20122. tm_year
EY Displays the year’s first four digits in the localization’s alternate form. tm_year
y Displays the final two digits of the year in the range [00, 99]. tm_year
Oy Displays the final two digits of the year in the alternate format of the place. tm_year
Ey Displays the year as an offset from the local time zone %EC (locale-dependent). tm_year
C Displays the year’s initial two digits in the interval [00, 99]. tm_year
EC Displays base year’s name is written using the alternate localization, tm_year
G Displays an ISO 8601 week-based decade or the decade that includes the given week. tm_year,

tm_wday,

tm_yday

g Displays the final two digits of an ISO 8601 week-based year, – i.e., a decade that produces the defined week (interval [00,99]). tm_year,

tm_wday,

tm_yday

Month: The aforementioned format specifier is used time display the time in C++:

b/h Displays a month’s name abbreviation, such as Feb. tm_mon
B Displays the complete month name, such as January. tm_mon
Om Uses the locality’s alternate numeral system to display the month. tm_mon

Week: To display the week, the given below format specifiers are used:

U Displays the week of the calendar year from 00 to 53 as a numeric number, i.e., the first day of the week is Sunday tm_year,

tm_wday,

tm_yday

OU Uses the alternate decimal approach to represent the week of the year as by %U. tm_year,

tm_wday,

tm_yday

W The first day of the week is Monday because the letter W represents the week of the year as a numeric number between 00 and 53. tm_year,

tm_wday,

tm_yday

OW Uses the alternate numerical approach to represent the week of the year as by %W. tm_year,

tm_wday,

tm_yday

V Display the year’s weeks according to ISO 8601. (range [01,53]). tm_year,

tm_wday,

tm_yday

OV Displays the week of the calendar year, indicated by %V, using an alternate numbering scheme. tm_year,

tm_wday,

tm_yday

Days of Month/Year:

j The day of the year is represented by a numeric number in the region [001,366]. tm_yday
d The day of the month is represented by a numeric number in the interval [01, 31]. tm_mday
Od Uses the alternate decimal method to represent the day of the month as by %d. tm_mday
e The day of the month is represented by a numeric number with an interval of [1, 31]. tm_mday
Oe Displays the day of the month by employing a different numeric system, as by %e. tm_mday

Days of The Week: The following below-mentioned specifiers are used to display the days of the week.

a Displays the name of the weekday in abbreviation, such as Friday (locale dependent) tm_wday
A Displays the complete name of the workday, such as Friday (locale dependent) tm_wday
w Specifies the weekday as a numeric number in the [0–6] range. Sunday is zero. tm_wday
Ow Weekdays are expressed using the alternate numbering system as by %w. tm_wday
u In the ISO 8601 standard, a weekday is written as a numerical value with Monday as 1 and a spectrum of [1–7]. tm_wday
Ou Uses the alternate numeral system to represent “weekday” as by %u. tm_wday

Second, Minute, Hour: These below format specifiers in C++ will display the output second, minute and hour:

H Hour is expressed as a numeric value with a spectrum of [00,23]. tm_hour
OH Employing the alternate numeral system displays hour as by %H. tm_hour
I Hour is written as a numeric value with a range of [01,12]. tm_hour
OI Utilizes the alternate numeral method to represent hour as by %I. tm_hour
M Minutes are expressed as a decimal system between 00 and 59. tm_min
OM Employs the alternate numeral method when writing minutes as by %M. tm_min
S Employs the range [00, 60] for the second as a decimal value. tm_sec
OS The second is written as by %S when employing the alternate solution number system. tm_sec

Other Format Specifiers:

c Generates a normal date and time string, such as Sun., Oct. 17, 04:41:13, 2010. All
Ec Alternative locale-specific date and time strings. All
x Shows a localized depiction of the date (locale-dependent). All
Ex Uses the alternate date format for the locality. All
X Shows a regional depiction of time (locale-dependent). All
EX Displays alternate time formats for the area. All
D Comparable to “%m /%d /%y” tm_mon, tm_mday, tm_year
F Similar to “%Y-%m-%d”. tm_mon, tm_mday, tm_year
r These expressions represent local 12-hour clock time. tm_hour,

tm_min,

tm_sec

R The alternative of “%H:%M”. tm_hour,

tm_min

T The alternative of  “%H:%M:%S.” tm_hour,

tm_min,

tm_sec

P Displays a localized a.m. or p.m. tm_hour
z Displays offset from UTC in the ISO 8601 standard (for example, -0545), or maybe no letters if the time zone data isn’t given. tm_isdst
Z Prints a local time offset from UTC in the ISO 8601 standard (for instance, -0545), or even no letters if the local zone information is unavailable. tm_isdst

Return Value of strftime() Function

The strftime() function, when successful, outputs the number of bytes that were successfully stored into the char array indicated by str, excluding the final “0”. The values are unknown, and 0 is returned if the count was exceeded just before the complete string might be saved.

Example of strftime() Function

C++ code can never run without header files. So, firstly we have added two basic and necessary header files, i.e., “time.h” and “iostream”. “time.h” is used to display the time and date. It uses “iosteam” for both input and output. The main function is defined as starting the execution of an actual line of code. It contains the three-character types of length 50 “local_date_string, local_time_string, current_day”. As you can see, we have used “tm” to print the local date/time of the country and have initialized the variable of time_h that will store the current time.

Then strftime() function is applied to the character type variable “local_date_string, local_time_string, current_day” to print the current date, time, and the day of the country. In this function, we have used different format specifiers. The complete name of the month is printed in the first line of the strftime() function using the %B operator, the current date is printed using the %d operator, and the four-digit year is printed using the %Y operator.

The second line of the function strftime(), %T is used to print the full-time means hours, mins, and seconds of the current time. And in the third line of the function, we have used %A to print the Current day. As you see in the illustration below, we have used “endl” at the end of the strftime() function; the “endl” is used to print the output in the next line. After that, we utilized two “cout” statements to display the output. Then we return the 0 to the main function.

This is what the strftime() method returns which displays the current date, time, and day of the country Pakistan.

Conclusion

In this article, we have discussed the strftime() Function in C++ to format date and time. We have learned 4 arguments and format specifiers of the strftime() function. When we develop applications or software, it must display the time and, more importantly, the most pertinent information in a variety of forms as requested by the user. Then we shall employ the strftime() function in that scenario. Its unique selling point is the variety of ways in which we can present the date and time by using different format specifiers.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.