linux c 获取当前日期时间

struct tm nowtime;
struct timeval tv;
unsigned char time_now[128];
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec,&nowtime);

sprintf(time_now,"%d-%d-%d %d:%d:%d.%03d ",
nowtime.tm_year+1900,
nowtime.tm_mon+1,
nowtime.tm_mday,
nowtime.tm_hour,
nowtime.tm_min,
nowtime.tm_sec,
(int)(tv.tv_usec/1000)
);
printf("current time is %s\n",time_now);

posted on 2022-05-11 17:28  DuoRuaiMi4567  阅读(334)  评论(0编辑  收藏  举报