C++时间戳实现

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值