C++ Reference: Standard C++ Library reference: C Library: ctime: strftime

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中指定的时间。它们都以百分号(%)开头,并且是:

specifierReplaced byExample
%aAbbreviated weekday name *Thu
%AFull weekday name *Thursday
%bAbbreviated month name *Aug
%BFull month name *August
%cDate and time representation *Thu Aug 23 14:55:02 2001
%CYear divided by 100 and truncated to integer (00-99)20
%dDay of the month, zero-padded (01-31)23
%DShort MM/DD/YY date, equivalent to %m/%d/%y08/23/01
%eDay of the month, space-padded ( 1-31)23
%FShort YYYY-MM-DD date, equivalent to %Y-%m-%d2001-08-23
%gWeek-based year, last two digits (00-99)01
%GWeek-based year2001
%hAbbreviated month name * (same as %b)Aug
%HHour in 24h format (00-23)14
%IHour in 12h format (01-12)02
%jDay of the year (001-366)235
%mMonth as a decimal number (01-12)08
%MMinute (00-59)55
%nNew-line character ('\n')
%pAM or PM designationPM
%r12-hour clock time *02:55:02 pm
%R24-hour HH:MM time, equivalent to %H:%M14:55
%SSecond (00-61)02
%tHorizontal-tab character ('\t')
%TISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S14:55:02
%uISO 8601 weekday as number with Monday as 1 (1-7)4
%UWeek number with the first Sunday as the first day of week one (00-53)33
%VISO 8601 week number (01-53)34
%wWeekday as a decimal number with Sunday as 0 (0-6)4
%WWeek number with the first Monday as the first day of week one (00-53)34
%xDate representation *08/23/01
%XTime representation *14:55:02
%yYear, last two digits (00-99)01
%YYear2001
%zISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100)
If timezone cannot be determined, no characters
+100
%ZTimezone name or abbreviation *
If timezone cannot be determined, no characters
CDT
%%% sign%

*带有星号(*)的说明符是依赖于语言环境的。
注:黄色行表示C99引入的说明符和子说明符。从C99开始,还可以在百分比符号(%)和适当的说明符之间插入两个特定于语言环境的修饰符,以便在适用的情况下请求另一种可替换的格式:

ModifierMeaningApplies to
EUses the locale's alternative representation%Ec %EC %Ex %EX %Ey %EY
OUses 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++) 
无抛出保证:此函数从不抛出异常。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值