64,648
社区成员
发帖
与我相关
我的任务
分享
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
struct time
{
char ctime[19];
}T[5];
char current[19];
char getTime()
{
timespec time;
clock_gettime(CLOCK_REALTIME, &time); //获取相对于1970到现在的秒数
tm nowTime;
localtime_r(&time.tv_sec, &nowTime);
sprintf(current, "%04d-%02d-%02d-%02d:%02d:%02d", nowTime.tm_year + 1900, nowTime.tm_mon+1, nowTime.tm_mday, nowTime.tm_hour, nowTime.tm_min, nowTime.tm_sec);
}
int main()
{
for(int i=0;i<5;++i)
{
memset(current,0,sizeof(char)*19);
getTime();
strcpy(T[i].ctime,current);
printf("t[%d], ctime=%s, currtime=%s\n",i,T[i].ctime,current);
sleep(2);
}
printf("t[0]=%s\nt[1]=%s\nt[2]=%s\nt[3]=%s\nt[4]=%s\n",T[0].ctime,T[1].ctime,T[2].ctime,T[3].ctime,T[4].ctime);
return 0;
}