C/C++中关于时间的函数 ctime()

来源:互联网 发布:http 数据库 编辑:程序博客网 时间:2024/05/16 04:52

函数:ctime (const time_t * timer);

作用,将time_t型的时间转换为易读的string,转换后的string长度为25. 其格式为:

Www Mmm dd hh:mm:ss yyyy

但是有时只想提取其中的时间(hh:mm:ss),于是我写了下面一个小程序,用来提取其中的时间:

#include <stdio.h>#include <time.h>#include <string.h>#define N 50int main(){time_t test;test = time(NULL);//抓取现在的时间 ——目前距离1970-01-01-00:00:00所经历的秒数 printf("now is %s\n",ctime(&test));//将上面的秒数转换为人们易读的时间表达方式 ,并在末尾换行 printf("length of ctime = %d\n",strlen(ctime(&test)));//ctime()所产生的字符串的长度 char c[N];//提取其中的时间 strcpy(c,ctime(&test));//先将其赋值给c for(int i=11; i<=18;i++)//取第12位到第19位,注意,第一位为 i = 0; printf("%c",c[i]);//打印时间 printf("\n");return 0;}



原创粉丝点击