Get Unix timestamp with C++

后端 未结 7 594
南旧
南旧 2020-12-02 20:00

How do I get a uint unix timestamp in C++? I\'ve googled a bit and it seems that most methods are looking for more convoluted ways to represent time. Can\'t I j

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 20:09

    As this is the first result on google and there's no C++20 answer yet, here's how to use std::chrono to do this:

    #include 
    
    //...
    
    using namespace std::chrono;
    int64_t timestamp = duration_cast(system_clock::now().time_since_epoch()).count();
    

    In versions of C++ before 20, system_clock's epoch being Unix epoch is a de-facto convention, but it's not standardized. If you're not on C++20, use at your own risk.

提交回复
热议问题