#ifndef TIME_VITO_H
#define TIME_VITO_H
#ifdef _WIN32
#include<windows.h>
#else
#include <pthread.h>
#endif
#include<iostream>
using namespace std;
class TimeVito
{
public:
TimeVito();
~TimeVito();
std::string nowTime();
long long getSeconds(); //秒
long long getMilliSeconds(); //毫秒
long long getMicroSeconds(); //微秒
long long getNanoSeconds(); //纳秒
};
#endif
#include "TimeVito.h"
#ifdef _WIN32
#include<time.h>
#else
#include <unistd.h>
#include <time.h>
#endif
TimeVito::TimeVito()
{
}
TimeVito::~TimeVito()
{
}
std::string TimeVito::nowTime()
{
#ifdef _WIN32
uint64_t intervals;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
intervals = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
intervals -= 116444736000000000;
time_t tmp = (time_t)(intervals / 10000000); //秒
tm* _tm = localtime(&tmp);
char szlocaltime[20] = {0};
snprintf(szlocaltime, 20, "%04d-%02d-%02d-%02d:%02d:%02d",
_tm->tm_year + 1900,
_tm->tm_mon + 1,
_tm->tm_mday,
_tm->tm_hour,
_tm->tm_min,
_tm->tm_sec);
return std::string(szlocaltime);
#else
struct timespec tp = { 0, 0 };
clock_gettime(CLOCK_REALTIME, &tp);
tm* _tm = localtime(&tp.tv_sec);
char szlocaltime[20] = { 0 };
snprintf(szlocaltime, 20, "%04d-%02d-%02d-%02d:%02d:%02d",
_tm->tm_year + 1900,
_tm->tm_mon + 1,
_tm->tm_mday,
_tm->tm_hour,
_tm->tm_min,
_tm->tm_sec);
return std::string(szlocaltime);
#endif
}
//秒
long long TimeVito::getSeconds()
{
#ifdef _WIN32
uint64_t intervals;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
intervals = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
intervals -= 116444736000000000;
return (intervals / 10000000); //秒
#else
struct timespec tp = { 0, 0 };
clock_gettime(CLOCK_REALTIME, &tp);
return tp.tv_sec;
#endif
}
//毫秒
long long TimeVito::getMilliSeconds()
{
#ifdef _WIN32
uint64_t intervals;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
intervals = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
intervals -= 116444736000000000;
return (intervals / 10000); //毫秒
#else
struct timespec tp = { 0, 0 };
clock_gettime(CLOCK_REALTIME, &tp);
return tp.tv_sec*1000;
#endif
}
//微秒
long long TimeVito::getMicroSeconds()
{
#ifdef _WIN32
uint64_t intervals;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
intervals = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
intervals -= 116444736000000000;
return intervals /10;
#else
struct timespec tp = { 0, 0 };
clock_gettime(CLOCK_REALTIME, &tp);
return tp.tv_sec * 1000 *1000 + tp.tv_nsec/1000;
#endif
}
//纳秒
long long TimeVito::getNanoSeconds()
{
#ifdef _WIN32
uint64_t intervals;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
intervals = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
intervals -= 116444736000000000;
return intervals*100;
#else
struct timespec tp = { 0, 0 };
clock_gettime(CLOCK_REALTIME, &tp);
return tp.tv_sec * 1000 * 1000 *1000 + tp.tv_nsec;
#endif
}
void test_time()
{
TimeVito tm;
std::string ret = tm.nowTime();
long long sec = tm.getSeconds();
long long secMi = tm.getMilliSeconds();
long long secMcr = tm.getMicroSeconds();
long long secNa = tm.getNanoSeconds();
std::cout << ret << endl << sec << endl << secMi << endl << secMcr << endl << secNa << endl << endl;
}
int main()
{
test_time();
return 0;
}