嵌入式取时间格式串文件名

root@MYD-AM335x test# date
Mon Apr 10 11:17:45 UTC 2017
root@MYD-AM335x test# ./testtime
20170410111748root@MYD-AM335x test#

#include <stdio.h>
#include <string.h>
#include <time.h>


static void GetTime(struct tm *newtime)
{
if(NULL == newtime)
{
return ;
}

struct tm *newtimeTemp=NULL;
time_t aclock;

aclock=time(NULL);
newtimeTemp=localtime(&aclock);
newtimeTemp->tm_year += 1900;
newtimeTemp->tm_mon += 1;
memcpy(newtime,newtimeTemp,sizeof(struct tm));

return ;
}

main()
{
struct tm CurTime;
char buf[20] = {0};

memset(&CurTime, 0x0,sizeof( CurTime ));
(void)GetTime(&CurTime);

sprintf(buf,"%4d%02d%02d%02d%02d%02d",CurTime.tm_year,CurTime.tm_mon,CurTime.tm_mday,CurTime.tm_hour,CurTime.tm_min,CurTime.tm_sec);
printf("%s",buf);
}

posted on 2017-04-10 11:19  xihong  阅读(200)  评论(0编辑  收藏  举报

导航