How to get time in seconds since epoch in C++

2 Answers

0 votes
#include <iostream>
#include <chrono>

int main() {
    auto seconds_since_epoch = std::chrono::duration_cast<std::chrono::seconds>(
                           std::chrono::system_clock::now().time_since_epoch()).count();

    std::cout << "seconds since epoch: " << seconds_since_epoch; 
    
    return 0;
}
 
 
 
 
/*
run:
 
seconds since epoch: 1621175494
 
*/

 



answered May 16, 2021 by avibootz
0 votes
#include <iostream>
#include <sys/time.h>

int main() {
    struct timeval time_now{};
    
    gettimeofday(&time_now, nullptr);

    std::cout << "seconds since epoch: " << time_now.tv_sec;
    
    return 0;
}
 
 
 
 
/*
run:
 
seconds since epoch: 1621175789

*/

 



answered May 16, 2021 by avibootz

Related questions

2 answers 147 views
147 views asked Feb 10, 2023 by avibootz
2 answers 457 views
1 answer 125 views
2 answers 125 views
125 views asked Feb 10, 2023 by avibootz
1 answer 137 views
...