using namespace std;
time_t seconds = time(NULL);
timeinfo = localtime(&seconds);
cout << "Seconds since the epoch: " << seconds << endl;
cout << "Local time: " << asctime(timeinfo) << endl;
Here I'm using three functions and one stuct from the C++ standard ctime library to get the time and display it. The time function gets the number of seconds since January 1, 1970 (the start of the Unix epoch) from the operating system. The localtime function converts the time in seconds to a tm structure representing the calendar date and time. Finally, the asctime function converts the tm stucture to a human-readable string format.
Compile the program above using g++, then run it to see the following output.
Naturally, you'll see different results depending on when you run the executable.
Seconds since the epoch: 1234712674
Local time: Sun Feb 15 10:44:34 2009