C++官网参考链接:https://cplusplus.com/reference/ctime/strftime/
函数
<ctime>
strftime
size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );
时间格式化为字符串
将format的内容复制到ptr中,将其格式说明符扩展为表示timeptr中描述的时间的对应值,并具有maxsize个字符的限制。
形参
ptr
指向复制结果C字符串的目标数组的指针。
maxsize
要复制到ptr的最大字符数,包括终止的空字符。
format
C字符串,包含普通字符和特殊格式说明符的任意组合。这些格式说明符被对应值的函数替换,以表示在timeptr中指定的时间。它们都以百分号(%)开头,并且是:
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 |
%C | Year divided by 100 and truncated to integer (00-99 ) | 20 |
%d | Day of the month, zero-padded (01-31 ) | 23 |
%D | Short MM/DD/YY date, equivalent to %m/%d/%y | 08/23/01 |
%e | Day of the month, space-padded ( 1-31 ) | 23 |
%F | Short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2001-08-23 |
%g | Week-based year, last two digits (00-99 ) | 01 |
%G | Week-based year | 2001 |
%h | Abbreviated month name * (same as %b ) | Aug |
%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 |
%n | New-line character ('\n' ) | |
%p | AM or PM designation | PM |
%r | 12-hour clock time * | 02:55:02 pm |
%R | 24-hour HH:MM time, equivalent to %H:%M | 14:55 |
%S | Second (00-61 ) | 02 |
%t | Horizontal-tab character ('\t' ) | |
%T | ISO 8601 time format (HH:MM:SS ), equivalent to %H:%M:%S | 14:55:02 |
%u | ISO 8601 weekday as number with Monday as 1 (1-7 ) | 4 |
%U | Week number with the first Sunday as the first day of week one (00-53 ) | 33 |
%V | ISO 8601 week number (01-53 ) | 34 |
%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 | ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100) If timezone cannot be determined, no characters | +100 |
%Z | Timezone name or abbreviation * If timezone cannot be determined, no characters | CDT |
%% | A % sign | % |
*带有星号(*)的说明符是依赖于语言环境的。
注:黄色行表示C99引入的说明符和子说明符。从C99开始,还可以在百分比符号(%)和适当的说明符之间插入两个特定于语言环境的修饰符,以便在适用的情况下请求另一种可替换的格式:
Modifier | Meaning | Applies to |
---|---|---|
E | Uses the locale's alternative representation | %Ec %EC %Ex %EX %Ey %EY |
O | Uses the locale's alternative numeric symbols | %Od %Oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow %OW %Oy |
timeptr
指向tm结构的指针,该结构包含分解为其组件的日历时间(参见struct tm)。
返回值
如果生成的C字符串(包括结束的空字符)的长度不超过maxsize,则函数返回复制到ptr的总字符数(不包括结束的空字符)。
否则,它返回0,并且ptr指向的数组的内容是不确定的。
兼容性
特定的库实现可能支持额外的说明符或组合。
这里列出的是由最新的C和C++标准支持的(都是在2011年发布的),但是黄色的那些是在C99中引入的(只有C++11之后的C++实现才需要),并且可能不被遵循旧标准的库所支持。
用例
/* strftime example */
#include <stdio.h> /* puts */
#include <time.h> /* time_t, struct tm, time, localtime, strftime */
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;
}
输出:
数据竞争
该函数访问由format指向的数组和由timeptr指向的对象。如果成功,它还修改ptr指向的数组中的元素。
同时更改语言环境还可能导致数据竞争。
异常(C++)
无抛出保证:此函数从不抛出异常。