本篇文章是对C++中获取UTC时间精确到微秒的实现进行了详细的分析介绍,需要的朋友参考下
下面先给出C++实现代码:
#define UTC_TIME_STAMP_H_
#include
#include
#includ
来源gao!daima.com搞$代!码网
e
#if !defined(_WINSOCK2API_) && !defined(_WINSOCKAPI_)
struct timeval
{
long tv_sec;
long tv_usec;
};
#endif
static int gettimeofday(struct timeval* tv)
{
union {
long long ns100;
FILETIME ft;
} now;
GetSystemTimeAsFileTime (&now.ft);
tv->tv_usec = (long) ((now.ns100 / 10LL) % 1000000LL);
tv->tv_sec = (long) ((now.ns100 – 116444736000000000LL) / 10000000LL);
return (0);
}
//获取1970年至今UTC的微妙数
static time_t TimeConversion::GetUtcCaressing()
{
timeval tv;
gettimeofday(&tv);
return ((time_t)tv.tv_sec*(time_t)1000000+tv.tv_usec);
}
#endif
接下来给出使用方法:
timeval tv;
gettimeofday(&tv);
或者直接调用:GetUtcCaressing();
最后说明:本文代码在vs2008与VS2010下都进行了测试,可放心使用
附录:本文同时给出UTC时间秒级UTC获取方法代码:
struct tm *p;
time(&timep);
p=localtime(&timep);
timep = mktime(p);
printf(“%d\n”,timep);
以上就是C++中获取UTC时间精确到微秒的实现代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!