首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >c++ format 函数包含_C语言库函数之strftime()详解

c++ format 函数包含_C语言库函数之strftime()详解

作者头像
用户7886150
修改2021-02-18 10:38:42
修改2021-02-18 10:38:42
1.4K0
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: C++ strftime()

原函数:

 size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)

 参数

 str -- 是C字符串复制到目标数组的指针。maxsize -- 是给 str 要复制的字符的最大数目。format -- 是C字符串,其中包含常规字符和特殊格式说明符的任意组合。

 格式说明符是函数相应的值代替 tm 中指定的时间来表示,格式说明符:

 timeptr -- 是为tm结构的指针,包含一个日历时间分解成其组成部分,如下图所示:

 struct tm { 

 int tm_sec; /* seconds, range 0 to 59 */

  int tm_min; /* minutes, range 0 to 59 */ 

 int tm_hour; /* hours, range 0 to 23 */ 

 int tm_mday; /* day of the month, range 1 to 31 */ 

 int tm_mon; /* month, range 0 to 11 */ 

 int tm_year; /* The number of years since 1900 */ 

 int tm_wday; /* day of the week, range 0 to 6 */ 

 int tm_yday; /* day in the year, range 0 to 365 */ 

 int tm_isdst; /* daylight saving time */

  };

 返回值

 如果C字符串适合大小的字符(包括终止空字符)小于总数的字符复制到 str(不包括终止空字符),否则返回0。

 如何使用strftime() 函数:

 #include 

 #include 

 int main () { 

 time_t rawtime; 

 struct tm *info; 

 char buffer[80]; 

 time( &rawtime ); 

 info = localtime( &rawtime ); 

 strftime(buffer,80,"%x - %I:%M%p", info); 

 printf("Formatted date & time : %s ", buffer ); 

 return(0); 

 }

 编译和运行上面的程序,产生如下结果:

 Formatted date & time : 19/14/11 - 07:40PM

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档