linux获取日期时间

使用gettimeofday(),没有系统调用的开销。

std::string getTime(void)
{
  timeval tv;
  tm * time;
  std::stringstream ss;

  if(gettimeofday(&tv, nullptr) == -1){
    throw errno;
  }
  time = localtime(&(tv.tv_sec));
  char systime[40];
  strftime(systime, 40, "%Y-%m-%d %H:%M:%S.", time);
 
  ss << systime << (int64_t)(tv.tv_usec / 1000);
 
  return ss.str();
}

 

posted @ 2020-05-02 23:49  wa小怪兽  阅读(577)  评论(0编辑  收藏  举报