vtime.hpp

//vov
#ifndef VTIME_HPP
#define VTIME_HPP

#include <cstdio>
#include <ctime>
#include <string>
#include <chrono>
#include <sys/time.h>

static const std::string vTimeStamp() {
  time_t tt=time(NULL);
  struct tm *stm=localtime(&tt);
  char tmp[50];
  sprintf(tmp, "%04d%02d%02d%02d%02d%02d",
    1900 + stm->tm_year,
    1 + stm->tm_mon,
    stm->tm_mday,
    stm->tm_hour,
    stm->tm_min,
    stm->tm_sec);
  std::string res(tmp);
  return res;
}

static const std::string vTimeStampMilli()
std::string vTimeStampMilli()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    struct tm* ptr_tm = std::localtime(&tv.tv_sec);

    std::stringstream ss;
    ss << std::setfill('0');
    ss << ptr_tm->tm_year + 1900 << "-";
    ss << std::setw(2) << ptr_tm->tm_mon + 1 << "-";
    ss << std::setw(2) << ptr_tm->tm_mday << " ";

    ss << std::setw(2) << ptr_tm->tm_hour << ":";
    ss << std::setw(2) << ptr_tm->tm_min << ":";
    ss << std::setw(2) << ptr_tm->tm_sec << ".";
    ss << std::setw(3) << tv.tv_usec / 1000;

    return ss.str();
}      

static const std::chrono::steady_clock::time_point vTimePoint() {
    return std::chrono::steady_clock::now();
}

static double vTimePointDiff(std::chrono::steady_clock::time_point t1,
    std::chrono::steady_clock::time_point t2) {
    return std::chrono::duration_cast<std::chrono::duration<double>>(t2-t1).count()*1000;
}

#endif
posted @ 2019-04-23 19:50  JohnRed  阅读(147)  评论(0)    收藏  举报