C++ Standard Library C++ STL Library

C++ <ctime> - time_t Type



The C++ <ctime> time_t type is an alias of a fundamental arithmetic type capable of representing times, as returned by time() function.

It is generally implemented as an integral value representing the number of seconds holding the number of seconds elapsed since 00:00, Jan 1 1970 UTC (i.e., a unix timestamp). Although libraries may use a different representation of time.

In the <ctime> header file, it is defined as follows:

typedef /* unspecified */ time_t;              

Example:

The example below shows the usage of time_t type.

#include <iostream>
#include <ctime>
using namespace std;
 
int main (){
  time_t t = 3600;
    
  cout<<"Time elapsed since the epoch began: "
      <<t<<" seconds\n";

  //displaying the time
  cout<<asctime(gmtime(&t));

  return 0;
}

The output of the above code will be:

Time elapsed since the epoch began: 3600 seconds
Thu Jan  1 01:00:00 1970

❮ C++ <ctime> Library