当前位置: 技术问答>linux和unix
Unix平台上用C语言写LOG文件时,当前时间需要精确到毫秒级,请给个能执行的代码...
来源: 互联网 发布时间:2015-06-05
本文导语: 同题目 | void GetLocalTime(unsigned int* puTime, unsigned int* puExVal) { struct timeval tv; struct timezone tz; struct tm* p; char szTime[11]; gettimeofday(&tv, &tz); p = localtime(&tv.tv_sec); sprintf(szTime, "%2d%02d%02d...
同题目
|
void GetLocalTime(unsigned int* puTime, unsigned int* puExVal)
{
struct timeval tv;
struct timezone tz;
struct tm* p;
char szTime[11];
gettimeofday(&tv, &tz);
p = localtime(&tv.tv_sec);
sprintf(szTime, "%2d%02d%02d%02d%02d",
(1 + p->tm_mon),
(p->tm_mday),
(p->tm_hour),
(p->tm_min),
(p->tm_sec));
*puTime = atoi(szTime);
*puExVal = tv.tv_usec;
}
{
struct timeval tv;
struct timezone tz;
struct tm* p;
char szTime[11];
gettimeofday(&tv, &tz);
p = localtime(&tv.tv_sec);
sprintf(szTime, "%2d%02d%02d%02d%02d",
(1 + p->tm_mon),
(p->tm_mday),
(p->tm_hour),
(p->tm_min),
(p->tm_sec));
*puTime = atoi(szTime);
*puExVal = tv.tv_usec;
}
|
#include
|
#include
#include
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf(“tv_sec; %dn”, tv,.tv_sec) ;
printf(“tv_usec; %dn”,tv.tv_usec);
printf(“tz_minuteswest; %dn”, tz.tz_minuteswest);
printf(“tz_dsttime, %dn”,tz.tz_dsttime);
}
这个可以看到微秒 和上面补充一下 凑凑数吧
#include
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf(“tv_sec; %dn”, tv,.tv_sec) ;
printf(“tv_usec; %dn”,tv.tv_usec);
printf(“tz_minuteswest; %dn”, tz.tz_minuteswest);
printf(“tz_dsttime, %dn”,tz.tz_dsttime);
}
这个可以看到微秒 和上面补充一下 凑凑数吧