Open In App

Date and Time Parsing in C++

Last Updated : 22 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The Date and time parsing is a common task in programming especially when dealing with user input or data from external sources. C++ provides various utilities and libraries to handle date and time parsing efficiently. Some of the most commonly used libraries for date and time parsing in C++ are:

  1. <ctime>: This header provides functions and types to work with date and time values including parsing and formatting.
  2. <chrono>: This header provides facilities to deal with the duration time points and clocks. It is part of the C++11 standard and provides a more modern and type-safe approach to handling date and time operations.

Parsing in <ctime>

The C++ <ctime> is inherited from C language and primarily uses the time_t data type to represent time.

1. Converting date-time string to time_t type.

It is a function to parse a date or time string.

Syntax:

time_t parseDateTime(const char* datetimeString, const char* format);

Here,

  • time_t: It is an arithmetic type that is used to represent time in C++.
  • parseDateTime: User-defined name of our function.
  • dateTimeString: Parameter which represents the current date and time in human-readable form.
  • format: The fashion in which dateTimeString is represented.

2. Converting time_t arithmetic type to string

Function to format a time_t value into a date or time string.

Syntax:

string formatDateTime(time_t time, const char* format)

Here,

  • string: return type of
  • time: It is time in time_t format.
  • datetimeString: String representing date and time.
  • format: format of the datetime string.

C++ Program:

C++
// C++ Program to implement Date and Time parsing using
// <ctime>
#include <ctime>
#include <iostream>
using namespace std;

// function to parse a date or time string.
time_t parseDateTime(const char* datetimeString, const char* format)
{
    struct tm tmStruct;
    strptime(datetimeString, format, &tmStruct);
    return mktime(&tmStruct);
}

// Function to format a time_t value into a date or time string.
string DateTime(time_t time, const char* format)
{
    char buffer[90];
    struct tm* timeinfo = localtime(&time);
    strftime(buffer, sizeof(buffer), format, timeinfo);
    return buffer;
}
int main()
{
    const char* datetimeString = "2023-06-17 12:36:51";
    const char* format = "%Y-%m-%d %H:%M:%S";
    time_t parsedTime = parseDateTime(datetimeString, format);
    string formattedTime = DateTime(parsedTime, format);
    cout << "Parsed Time--> " << parsedTime << endl;
    cout << "Formatted Time--> " << formattedTime << endl;
    return 0;
}

Output
Parsed Time--> 1687005411
Formatted Time--> 2023-06-17 12:36:51

Parsing in <chrono>

The <chrono> library was introduced in C++11 and provides a modern solution for time manipulation in C++.

1. Converting date-time string to time_point representation.

It is a function to parse a date or time string.

Syntax:

chrono::system_clock::time_point parseDateTime(const std::string& datetimeString, const std::string& format)

Here,

  • chrono::system_clock::time_point: It represents time at an instance.
  • parseDateTime: User-defined name of our function.
  • dateTimeString: Parameter which represents the current date and time in human-readable form.
  • format: The fashion in which dateTimeString is represented.

2. Converting time_point representation to string

Function to format a time_point into a date or time string.

Syntax:

string formatDateTime(const chrono::system_clock::time_point& timePoint, const std::string& format);

Here,

  • string: return type of
  • time: It is time in time_t format.
  • datetimeString: String representing date and time.
  • format: format of the datetime string.

Implementation

C++
// C++ Program to implement Date and Time Parsing using
// chrono
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;

// function to parse a date or time string.
chrono::system_clock::time_point GFG(const string& datetimeString, const string& format)
{
    tm tmStruct = {};
    istringstream ss(datetimeString);
    ss >> get_time(&tmStruct, format.c_str());
    return chrono::system_clock::from_time_t(
        mktime(&tmStruct));
}

// Function to format a time_t value into a date or time string.
string DateTime(const chrono::system_clock::time_point& timePoint,
         const string& format)
{
    time_t time
        = chrono::system_clock::to_time_t(timePoint);
    tm* timeinfo = localtime(&time);
    char buffer[70];
    strftime(buffer, sizeof(buffer), format.c_str(),
             timeinfo);
    return buffer;
}
int main()
{
    const string datetimeString = "2023-05-22 12:24:52";
    const string format = "%Y-%m-%d %H:%M:%S";
    chrono::system_clock::time_point parsedTime
        = GFG(datetimeString, format);
    string formattedTime = DateTime(parsedTime, format);
    cout << "Parsed Time---> "
         << chrono::system_clock::to_time_t(parsedTime)
         << endl;
    cout << "Formatted Time---> " << formattedTime << endl;
    return 0;
}

Output
Parsed Time---> 1684758292
Formatted Time---> 2023-05-22 12:24:52

Conclusion

The Date and time parsing in C++ can be accomplished using either the <ctime> library or the std::chrono library. <ctime> approach works with time_t values. while the std::chrono approach utilizes std::chrono::system_clock::time_point. Both approaches allow parsing and formatting date and time strings with the custom formats.


Next Article
Article Tags :
Practice Tags :

Similar Reads