Strftime() function in C
0 681
strftime() in C is a function from the time.h header, essential for formatting date and time.
It utilizes a struct tm to hold time and date details.
Introduction to strftime Function in C
In C programming, the strftime function is a crucial tool for formatting date and time values into a string.
It stands for "string format time," and it's a part of the standard C library time.h.
This function allows developers to represent dates and times in various formats based on specified format codes.
strftime takes several parameters, including a character array where the formatted string will be stored, the maximum length of that array, a format string specifying the desired output format, and a struct tm object representing the time to be formatted.
Format Specifiers:
Format specifiers are placeholders within the format string that strftime uses to represent different components of the date and time.
Some common format specifiers include %Y for the year, %m for the month, %d for the day, %H for the hour (24-hour format), %M for the minute, %S for the second, and %A for the full weekday name.
The struct tm structure is utilized to hold time information.
It contains members for year, month, day, hour, minute, second, and more.
Its syntax is:
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
strftime() formats time according to the rules specified in the format and stores it in the character array s.
The struct tm structure holds time details, including seconds, minutes, hours, day of the month, month, year (since 1900), day of the week, day in the year, and daylight saving time information.
Example:
// Program for strftime() in C #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); tmp = localtime(&t); strftime(MY_TIME, sizeof(MY_TIME), "%x - %I:%M%p", tmp); printf("Formatted date & time : %s\n", MY_TIME); return 0; }
Output:
Formatted date & time : 05/07/24 - 09:25AM
This program showcases the current date and time formatted according to the specified format using strftime().
We use it when creating applications that need to display the current time in various formats as per user preferences, allowing versatility in date and time representation.
// Program for strftime() in C #include<stdio.h>Output:#include<time.h> int main() { char buffer[80]; struct tm timeinfo; // Get the current time time_t rawtime; time(&rawtime); localtime_r(&rawtime, &timeinfo); // Format the time using strftime strftime(buffer, sizeof(buffer), "Today is %A, %B %d, %Y. The time is %I:%M %p.", &timeinfo); // Print the formatted time printf("Formatted time: %s\n", buffer); return 0; }
Formatted time: Today is Tuesday, May 07, 2024. The time is 09:27 AM.
This program retrieves the current time, formats it using strftime according to the specified format string, and then prints the formatted time.
It provides a clear illustration of how strftime can be utilized in C programs to manipulate date and time representations.
Share:
Comments
Waiting for your comments