How do I parse timestamps with from_stream in C++?

In C++, parsing timestamps can be done using the `` library along with format specifiers. The `from_stream` function allows for converting a string representation of a timestamp into a `std::chrono` type, enabling easier date and time manipulations.
C++, timestamps, from_stream, chrono, parsing dates, date-time manipulation

#include <chrono>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;
using namespace std::chrono;

int main() {
    string timestamp = "2023-10-03 14:30:00"; // Example timestamp string
    std::tm tm = {};
    std::istringstream ss(timestamp);
    
    // Parsing the timestamp
    ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
    
    if (ss.fail()) {
        std::cerr << "Parsing failed" << std::endl;
        return 1;
    }
    
    // Converting to time_point
    auto timePoint = system_clock::from_time_t(std::mktime(&tm));
    
    // Output the parsed timestamp
    time_t time = system_clock::to_time_t(timePoint);
    std::cout << "Parsed timestamp: " << std::ctime(&time);

    return 0;
}
    

C++ timestamps from_stream chrono parsing dates date-time manipulation