您的位置:首页 > 编程语言 > C语言/C++

C++中获取UTC时间精确到微秒的实现代码

2013-05-16 16:31 836 查看

#ifndef UTC_TIME_STAMP_H_
#define UTC_TIME_STAMP_H_

#include <windows.h>
#include <sys/timeb.h>
#include <time.h>

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UTC时间 精确 微秒