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 move(ss.str());
}

猜你喜欢

转载自www.cnblogs.com/HadesBlog/p/12820315.html
今日推荐