Date and time parsing is the process of converting date and time strings into a format that can be processed by a program. In C++, this can be achieved using utilities provided by the <ctime> and <chrono> libraries.
- <ctime> provides traditional C-style date and time handling using the time_t data type.
- <chrono> offers a modern and type-safe approach to working with dates, times, and durations.
Parsing Date and Time Using <ctime>
The <ctime> library represents time using the time_t data type and provides functions to convert date-time strings into time values and vice versa.
Converting a Date-Time String to time_t
It is a function to parse a date or time string.
Syntax
time_t parseDateTime(const string& datetimeString, const string& format);
Where:
- datetimeString represents the date-time string to be parsed.
- format specifies the format of the date-time string.
- time_t is an arithmetic type used to represent calendar time.
Converting time_t to a Date-Time String
Function to format a time_t value into a date or time string.
Syntax
string formatDateTime(time_t timeValue, const string& format);
Where:
- timeValue is the time represented as a time_t value.
- format specifies the desired output format.
Example: Parsing and Formatting Date-Time Using <ctime>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
using namespace std;
time_t parseDateTime(const string& datetimeString, const string& format)
{
tm tmStruct = {};
istringstream ss(datetimeString);
ss >> get_time(&tmStruct, format.c_str());
return mktime(&tmStruct);
}
string formatDateTime(time_t timeValue, const string& format)
{
char buffer[80];
tm* timeInfo = localtime(&timeValue);
strftime(buffer, sizeof(buffer), format.c_str(), timeInfo);
return buffer;
}
int main()
{
string datetimeString = "2023-06-17 12:36:51";
string format = "%Y-%m-%d %H:%M:%S";
time_t parsedTime = parseDateTime(datetimeString, format);
cout << "Parsed Time: "
<< parsedTime << endl;
cout << "Formatted Time: "
<< formatDateTime(parsedTime, format);
return 0;
}
Output
Parsed Time: 1687005411 Formatted Time: 2023-06-17 12:36:51
Explanation: The date-time string is parsed into a tm structure using std::get_time(). The mktime() function converts it into a time_t value, while strftime() formats the time_t value back into a readable date-time string.
Parsing Date and Time Using <chrono>
The <chrono> library, introduced in C++11, provides a modern way to represent time using std::chrono::time_point.
Converting a Date-Time String to time_point
This approach uses std::get_time() to parse a date-time string and converts it into a std::chrono::system_clock::time_point object.
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.
Converting time_point to a Date-Time 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.
Example: Parsing and Formatting Date-Time Using <chrono>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
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
Explanation: The date-time string is first parsed into a tm structure and then converted to a std::chrono::system_clock::time_point. The time_point can later be converted back into a formatted string for display.