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:
- <ctime>: This header provides functions and types to work with date and time values including parsing and formatting.
- <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++
#include <ctime>
#include <iostream>
using namespace std;
time_t parseDateTime(const char* datetimeString, const char* format)
{
struct tm tmStruct;
strptime(datetimeString, format, &tmStruct;);
return mktime(&tmStruct;);
}
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++
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
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;));
}
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.
Frequently Asked Questions (FAQs)
Q1. What is the purpose of date and time parsing in C++?
Answer:
The Date and time parsing is used to convert date and time information provided as strings into data types that can be manipulated and processed within a program. This is essential for the tasks like handling user input.
Q2. What is the difference between <ctime> and std::chrono?
Answer:
The <ctime> is an older C-style library that provides functions and types for the working with dates and times. It works with time_t and struct tm data types.
Q3. What are the common date and time format specifiers used in C++?
Answer:
The Common format specifiers include %Y for year (e.g., 2023), %m for the month (e.g., 07)%d for the day (e.g., 25) %H for the hour (e.g., 12) %M for the minute (e.g., 34) and %S for the second (e.g., 56).
Related Articles:
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...