C++ 获取时间戳

获取 unix 时间戳

std::time_t result = std::time(nullptr);

或者:

    const auto p1 = std::chrono::system_clock::now();
 
    std::cout << "seconds since epoch: "
              << std::chrono::duration_cast<std::chrono::seconds>(
                   p1.time_since_epoch()).count() << '\n';

time_t转换为int或者string :
std::to_string((int)std::time(nullptr))
参考:
Get Unix timestamp with C++

获取格式化时间戳字符串(string)

  auto t = std::time(nullptr);
  std::ostringstream os;
  os << std::put_time(std::localtime(&t), "%Y_%m_%dT%H_%M_%S%z%Z");
  std::string timestamp = os.str();
posted @ 2022-11-03 23:20  略略略——  阅读(1637)  评论(0编辑  收藏  举报