C library function -strftime()
Description
C Library Functionssize_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)According toformatThe formatting rules and formatting structure defined intimeptrthe time represented, and store it instrIn.
Declaration
Below is the declaration of the strftime() function.
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
Parameter
- str-- This is a pointer to the destination array, used to copy the resulting C string.
- maxsize-- This is the maximum number of characters to be copied to str.
- format-- This is a C string containing any combination of ordinary characters and special format specifiers. These format specifiers are replaced by the function with corresponding values representing the time specified in tm. The format specifiers are:
| Specifier | Replace with | Example |
|---|---|---|
| %a | Abbreviated weekday name | Sun |
| %A | Full weekday name | Sunday |
| %b | Abbreviated month name | Mar |
| %B | Full month name | March |
| %c | Date and time representation | Sun Aug 19 02:56:02 2012 |
| %d | Day of the month (01-31) | 19 |
| %H | Hour in 24-hour format (00-23) | 14 |
| %I | Hour in 12-hour format (01-12) | 05 |
| %j | Day of the year (001-366) | 231 |
| %m | Month as a decimal number (01-12) | 08 |
| %M | Minute (00-59) | 55 |
| %p | AM or PM name | PM |
| %S | Second (00-61) | 02 |
| %U | Week of the year, with the first Sunday as the first day of the first week (00-53) | 33 |
| %w | Day of the week as a decimal number, Sunday represented as 0 (0-6) | 4 |
| %W | Week of the year, with the first Monday as the first day of the first week (00-53) | 34 |
| %x | Date representation | 08/19/12 |
| %X | Time representation | 02:50:06 |
| %y | Year, last two digits (00-99) | 01 |
| %Y | Year | 2012 |
| %Z | Time zone name or abbreviation | CDT |
| %% | A % sign | % |
- timeptr-- This is a pointer to a tm structure that contains a calendar time broken down into the following parts:
struct tm {
int tm_sec; /* 秒,范围从 0 到 59 */
int tm_min; /* 分,范围从 0 到 59 */
int tm_hour; /* 小时,范围从 0 到 23 */
int tm_mday; /* 一月中的第几天,范围从 1 到 31 */
int tm_mon; /* 月份,范围从 0 到 11 */
int tm_year; /* 自 1900 起的年数 */
int tm_wday; /* 一周中的第几天,范围从 0 到 6 */
int tm_yday; /* 一年中的第几天,范围从 0 到 365 */
int tm_isdst; /* 夏令时 */
};
Return Value
If the resulting C string is less than size characters (including the null terminating character), the total number of characters copied to str is returned (excluding the null terminating character); otherwise, zero is returned.
Example
The following example demonstrates the usage of the strftime() function.
Example
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm *info;
char buffer[80];
time( &rawtime );
info = localtime( &rawtime );
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
printf("Formatted date & time : |%s|\n", buffer );
return(0);
}
Let us compile and run the above program, which will produce the following result:
格式化的日期 & 时间 : |2018-09-19 08:59:07|other extensions