Parsing time strings with std::chrono

Working with time in C++ can sometimes be tricky, especially when it comes to parsing time strings. Thankfully, the <chrono> library provides us with the necessary tools to parse and manipulate time in a convenient and efficient way. In this blog post, we will explore how to parse time strings using the std::chrono library.

Table of Contents

Introduction

The <chrono> library in C++ provides a set of type-safe and efficient classes for working with time-related operations. Among these classes, the std::chrono::duration and std::chrono::time_point allow us to represent and manipulate time durations and points in a generic and flexible manner.

Parsing Time Strings

To parse time strings, we first need to define a format for our time string. The format should match the expected format of the input time string. For example, if our time string is in the format “HH:MM:SS”, then our format would be “%H:%M:%S”. Here, the “%” symbols denote the placeholders for the different components of the time string.

Once the format is defined, we can use the std::get_time function from the <iomanip> library to parse the time string into appropriate components. The std::get_time function takes a std::tm structure and a time string with the specified format as input, and parses the time string into the std::tm structure.

After parsing the time string into the std::tm structure, we can use the std::chrono::duration and std::chrono::time_point classes to convert the parsed components into a duration or a point in time. This allows us to perform various operations on the parsed time, such as addition, subtraction, or comparison.

Example Code

Let’s consider an example where we want to parse a time string in the format “HH:MM:SS” into a std::chrono::time_point. Here’s an example code snippet that demonstrates how to achieve this using the <chrono> library:

#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>

std::chrono::time_point<std::chrono::system_clock> parseTime(const std::string& timeString)
{
    std::tm tm{};
    std::istringstream ss(timeString);
    ss >> std::get_time(&tm, "%H:%M:%S");

    auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
    return tp;
}

int main()
{
    std::string timeString = "12:34:56";
    auto timePoint = parseTime(timeString);

    std::cout << "Time Point: " << std::chrono::duration_cast<std::chrono::seconds>(timePoint.time_since_epoch()).count() << " seconds since the epoch.\n";

    return 0;
}

In this code, we define a parseTime function that takes a time string as input. Inside the function, we create a std::tm structure to store the parsed time components. We use std::istringstream to read the time string into the std::tm structure using std::get_time with the specified format “%H:%M:%S”. Finally, we convert the parsed time to a std::chrono::time_point using std::chrono::system_clock::from_time_t and return it.

In the main function, we call the parseTime function with a time string “12:34:56”. We then calculate the number of seconds since the epoch by casting the time point to std::chrono::seconds and printing the result.

Conclusion

In this blog post, we have explored how to parse time strings using the std::chrono library in C++. We saw how to define a format for the time string, and how to use the std::get_time function from the <iomanip> library to parse the time string into a std::tm structure. We also learned how to convert the parsed time into a std::chrono::time_point using std::chrono::system_clock::from_time_t.

Parsing time strings with std::chrono allows us to efficiently and accurately work with time in C++, making it a powerful tool for time-related operations in our applications.

#references: