【发布时间】:2011-10-23 11:49:23
【问题描述】:
我需要计算从午夜开始的毫秒数,写了代码但似乎有什么问题。
time_t t;
time_t rawtime;
char buff[256] ={0};
struct timeval tv;
struct timezone tz;
struct tm *tma;
gettimeofday(&tv, &tz);
tma=localtime(&tv.tv_sec);
static char* months[] = {"JAN", "FEB", "MAR", "APR", "MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
sprintf(buff,"%02d %s %d 00:00:00",tma->tm_mday, months[tma->tm_mon], tma->tm_year + 1900);
struct tm tm1;
strptime(buff, "%d %b %Y %H:%M:%S", &tm1);
tm1.tm_isdst = -1;
t = mktime(&tm1);
time ( &rawtime );
time_t milSecFromMidNight = (rawtime - t)*1000 + tv.tv_usec/1000;
似乎有些时间以毫秒为单位存在差异。谁能指出来?
【问题讨论】:
-
当你说差异时,你看到了什么问题?
-
有时它的毫秒数低于预期
标签: c++ c unix unix-timestamp