Get Unix Timestamp with C ++

How do I get the uint unix timestamp in C ++? I was looking a bit googled, and it seems that most methods are looking for more confusing ways of representing time. Can't I get it as uint ?

+55
c ++ unix timestamp uint
May 16 '11 at 2:24
source share
7 answers

time() is the simplest function - seconds from the era. Linux manpage here .

The cppreference page linked above gives this example :

 #include <ctime> #include <iostream> int main() { std::time_t result = std::time(nullptr); std::cout << std::asctime(std::localtime(&result)) << result << " seconds since the Epoch\n"; } 
+61
May 16 '11 at 2:26
source share
 #include<iostream> #include<ctime> int main() { std::time_t t = std::time(0); // t is an integer type std::cout << t << " seconds since 01-Jan-1970\n"; return 0; } 
+35
May 16 '11 at 2:28
source share

The most common tip is wrong, you cannot just rely on time() . This is used for relative time: ISO C ++ does not indicate that 1970-01-01T00:00Z is time_t(0)

To make matters worse, you also cannot easily understand this. Of course, you can find the calendar date time_t(0) with gmtime , but what are you going to do if it's 2000-01-01T00:00Z ? How many seconds were between 1970-01-01T00:00Z and 2000-01-01T00:00Z ? This, of course, is not a multiple of 60, due to leap seconds.

+15
May 17 '11 at 8:15
source share
 #include <iostream> #include <sys/time.h> using namespace std; int main () { unsigned long int sec= time(NULL); cout<<sec<<endl; } 
+5
May 16 '11 at 2:29
source share

Windows uses different units of time and time: see Convert Windows Filetime to second in Unix / Linux

What std :: time () returns on Windows is not yet known to me (; -))

+4
Nov 06 '13 at
source share

I created a global definition with additional information:

 #include <iostream> #include <ctime> #include <iomanip> #define INFO std::cout << std::put_time(std::localtime(&time_now), "%y-%m-%d %OH:%OM:%OS") << " [INFO] " << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") >> " #define ERROR std::cout << std::put_time(std::localtime(&time_now), "%y-%m-%d %OH:%OM:%OS") << " [ERROR] " << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") >> " static std::time_t time_now = std::time(nullptr); 

Use it as follows:

 INFO << "Hello world" << std::endl; ERROR << "Goodbye world" << std::endl; 

Output Example:

 16-06-23 21:33:19 [INFO] src/main.cpp(main:6) >> Hello world 16-06-23 21:33:19 [ERROR] src/main.cpp(main:7) >> Goodbye world 

Put these lines in the header file. I find this very useful for debugging etc.

+2
Jun 24 '16 at 2:37
source share

Since this is the first result on Google, and there is no C ++ 11 answer yet, here's how to use std :: chrono for this:

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

Please note that this answer does not guarantee that the era will come on 01/01/1970, but in practice this is very likely.

+1
Jul 29 '19 at 14:54
source share



All Articles