码迷,mamicode.com
首页 > 其他好文 > 详细

vtime.hpp

时间:2019-04-23 21:12:49      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:clock   mda   str   def   get   mil   const   diff   define   

//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() {
  struct timeval tv;
  struct tm * time_ptr;
  gettimeofday(&tv, NULL);
  time_ptr = std::localtime(&tv.tv_sec);
  char accT[128];
  std::sprintf(accT, "%d-%02d-%02d %02d:%02d:%02d.%.04ld",
  time_ptr->tm_year + 1900,
  time_ptr->tm_mon + 1,
  time_ptr->tm_mday,
  time_ptr->tm_hour,
  time_ptr->tm_min,
  time_ptr->tm_sec,
  tv.tv_usec);
  std::string ret(accT);
  return ret;
}

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

vtime.hpp

标签:clock   mda   str   def   get   mil   const   diff   define   

原文地址:https://www.cnblogs.com/smallredness/p/10758540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!