C语言时间处理函数strftime
2012-03-30 18:45
399 查看
很方便实用的函数,mark一下~
function
<ctime>
Format time to string
Copies into ptr the content of format,
expanding its format tags into the corresponding values as specified by timeptr, with a limit ofmaxsize characters.
ptrPointer to the destination array where the resulting C string is copied.maxsizeMaximum number of characters to be copied to ptr.formatC string containing any combination of regular characters and special format specifiers. These format specifiers are replaced by the function to the corresponding values to represent the time specified in timeptr. They all begin with a percentage
(%) sign, and are:
* The specifiers whose description is marked with an asterisk (*) are locale-dependent.
timeptrPointer to a tm structure that contains a calendar time broken down into its components (see tm).
If the resulting C string fits in less than maxsize characters
including the terminating null-character, the total number of characters copied to ptr (not including the terminating null-character)
is returned.
Otherwise, zero is returned and the contents of the array are indeterminate.
This description corresponds to the C++ version of this function (which is the same as in the ISO-C Standard of 1990). C compilers may support additional specifiers and modifiers for the format parameter
of this function, which are not described here.
Example output:
function
strftime
<ctime>size_t strftime ( char * ptr, size_t maxsize, const char * format, const struct tm * timeptr );
Format time to string
Copies into ptr the content of format,
expanding its format tags into the corresponding values as specified by timeptr, with a limit ofmaxsize characters.
Parameters
ptrPointer to the destination array where the resulting C string is copied.maxsizeMaximum number of characters to be copied to ptr.formatC string containing any combination of regular characters and special format specifiers. These format specifiers are replaced by the function to the corresponding values to represent the time specified in timeptr. They all begin with a percentage(%) sign, and are:
specifier | Replaced by | Example |
---|---|---|
%a | Abbreviated weekday name * | Thu |
%A | Full weekday name * | Thursday |
%b | Abbreviated month name * | Aug |
%B | Full month name * | August |
%c | Date and time representation * | Thu Aug 23 14:55:02 2001 |
%d | Day of the month (01-31) | 23 |
%H | Hour in 24h format (00-23) | 14 |
%I | Hour in 12h format (01-12) | 02 |
%j | Day of the year (001-366) | 235 |
%m | Month as a decimal number (01-12) | 08 |
%M | Minute (00-59) | 55 |
%p | AM or PM designation | PM |
%S | Second (00-61) | 02 |
%U | Week number with the first Sunday as the first day of week one (00-53) | 33 |
%w | Weekday as a decimal number with Sunday as 0 (0-6) | 4 |
%W | Week number with the first Monday as the first day of week one (00-53) | 34 |
%x | Date representation * | 08/23/01 |
%X | Time representation * | 14:55:02 |
%y | Year, last two digits (00-99) | 01 |
%Y | Year | 2001 |
%Z | Timezone name or abbreviation | CDT |
%% | A % sign | % |
timeptrPointer to a tm structure that contains a calendar time broken down into its components (see tm).
Return Value
If the resulting C string fits in less than maxsize charactersincluding the terminating null-character, the total number of characters copied to ptr (not including the terminating null-character)
is returned.
Otherwise, zero is returned and the contents of the array are indeterminate.
Portability
This description corresponds to the C++ version of this function (which is the same as in the ISO-C Standard of 1990). C compilers may support additional specifiers and modifiers for the format parameterof this function, which are not described here.
Example
[code]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [code]/* strftime example */ #include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm * timeinfo; char buffer [80]; time ( &rawtime ); timeinfo = localtime ( &rawtime ); strftime (buffer,80,"Now it's %I:%M%p.",timeinfo); puts (buffer); return 0; } |
Now it's 03:21PM. |
相关文章推荐
- c语言对时间的处理函数和计时的实现
- c语言对时间的处理函数和计时的实现
- C语言中的时间处理函数
- C语言格式化输出时间函数strftime()
- c语言对时间的处理函数和计时的实现
- C语言中,时间调用处理的相关函数介绍
- strftime()函数,C语言时间格式化
- c语言中的时间处理函数集合
- c语言对时间的处理函数和计时的实现
- C语言中时间调用处理的相关函数介绍
- C语言中,时间调用处理的相关函数介绍
- c语言对时间的处理函数和计时的实现
- C语言当中几个获取时间和处理的函数
- c语言可变参数函数--编译器内部处理过程
- C语言中关于获取时间的函数,包括如果获取微妙、毫秒级时间
- 游戏中,常见时间处理函数
- c语言中如何添加计时函数,统计程序执行的时间
- SQL Server 常用的时间处理函数
- 时间处理函数小结
- C语言系列(三)字符串处理函数