您的位置:首页 > 编程语言 > C语言/C++

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

2012-04-23 14:36 399 查看
函数: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 50
int 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
;//提取其中的时间
strcpy(c,ctime(&test));//先将其赋值给c
for(int i=11; i<=18;i++)//取第12位到第19位,注意,第一位为 i = 0;
printf("%c",c[i]);//打印时间
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: