c-plus-plus
  1. c-plus-plus-c-date-and-time

C++ Date and Time

C++ provides two standard libraries for handling dates and times: the <ctime> library and the <chrono> library. The former includes functions to work with the system clock and time since the Epoch, while the latter includes classes to represent durations and time points.

Syntax

<ctime>

#include <ctime>

time_t time (time_t* timer);
char* ctime (const time_t* timer);

<chrono>

#include <chrono>

std::chrono::time_point<std::chrono::system_clock> now =
    std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::cout << std::ctime(&now_c);

Example

<ctime>

#include <iostream>
#include <ctime>

int main() {
    // current date/time based on current system
    time_t now = time(0);
 
    // convert now to string form
    char* dt = ctime(&now);
 
    std::cout << "The local date and time is: " << dt << std::endl;
 
    // convert now to tm struct for UTC
    tm* utc_tm = gmtime(&now);
    dt = asctime(utc_tm);
    std::cout << "The UTC date and time is: " << dt << std::endl;
    
    return 0;
}

<chrono>

#include <iostream>
#include <chrono>
#include <ctime>

int main() {
    // current date/time based on current system
    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
    std::time_t now_c = std::chrono::system_clock::to_time_t(now);
    
    // convert now to string form
    char* dt = std::ctime(&now_c);
    std::cout << "The local date and time is: " << dt;
    
    return 0;
}

Output

<ctime>

The local date and time is: Tue Feb  8 13:59:51 2022

The UTC date and time is: Tue Feb  8 21:59:51 2022

<chrono>

The local date and time is: Tue Feb  8 13:59:51 2022

Explanation

In the first example, we use the <ctime> library to get the current system time and convert it to a string. We then use gmtime() to convert the system time to a tm struct in UTC format and convert it to a string using asctime().

In the second example, we use the <chrono> library to get the current system time and convert it to a string using std::ctime().

Use

C++ date and time functions are useful for any program that needs to keep track of time. Examples include logging, scheduling tasks, and measuring execution time.

Important Points

  • The <ctime> library provides functions to work with the system clock and time since the Epoch.
  • The <chrono> library provides classes to represent durations and time points.
  • <ctime> functions return dates and times as tm structs, while <chrono> functions return time points.
  • <chrono> functions are type-safe and can be used to perform arithmetic on durations.

Summary

C++ provides two standard libraries for handling dates and times: <ctime> and <chrono>. The former includes functions to work with the system clock and time since the Epoch, while the latter includes classes to represent durations and time points. These libraries are useful for any program that needs to keep track of time, including logging, scheduling tasks, and measuring execution time.

Published on: