C++ std::chrono: Parsing Strings with from_stream and parse

2025-09-25

Many people expect to find a dedicated function for parsing date/time strings (like "2023-10-27 15
30
00") directly within std::chrono, but the standard library doesn't provide one. This is one of the most common "troubles" people run into with this library—the expectation of a feature that isn't there.

So, how do you parse date and time strings in modern C++? The C++20 standard introduced a fantastic and more flexible way to do this using std::chrono::from_stream. This is a much more robust and type-safe approach.

Here's a breakdown of how it works and a simple example

The from_stream function (which you call on a std::chrono object) reads a formatted date/time from an input stream, like a std::stringstream. The key is using the I/O manipulator std::chrono::parse, which is the part that does the actual formatting. Yes, it's confusingly named, but std::chrono::parse is a stream manipulator, not a standalone function.

Let's look at an example. Imagine you want to parse the string "2025-09-25 14
20
53".

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

int main() {
    // A string to parse
    std::string time_str = "2025-09-25 14:20:53";

    // Create a string stream to read from
    std::stringstream ss{time_str};

    // This is the cool part!
    // We declare a std::chrono::local_seconds variable
    std::chrono::local_seconds time_point;

    // Use the stream manipulator std::chrono::parse to read the string
    // The format specifier "%F %T" tells it how to interpret the string
    ss >> std::chrono::parse("%F %T", time_point);

    // Check if the parsing was successful
    if (ss.fail()) {
        std::cout << "Error parsing the string!" << std::endl;
        return 1;
    }

    // Now you have a valid time point!
    // We can print it out to verify
    std::cout << "Successfully parsed: " << time_point << std::endl;

    return 0;
}

The most frequent issue you'll encounter with std::chrono::parse (the stream manipulator) is using the wrong format specifier. The format string must exactly match the input string. A single mismatch in a character, space, or specifier will cause the parsing to fail silently.

For example, if your string is "2025-09-25" but your format specifier is "%F %T", the parsing will fail because the time part is missing.

%F is a shorthand for %Y-%m-%d (year-month-day).

%T is a shorthand for %H:%M:%S (hour
minute
second).

If your string is "25/09/2025", you'll need to use %d/%m/%Y as your format specifier.


cpp



The C++ std::is_rvalue_reference Explained with Examples

Let's dive into some of the common issues and how to deal with them.First, a quick refresher. std::is_rvalue_reference is a type trait in the C++ standard library


A Guide to C++ std::deque::push_front: Pitfalls and Efficient Alternatives

std::deque::push_front is a function that adds a new element to the beginning of a std::deque container. The deque (double-ended queue) is designed for efficient insertion and deletion at both ends


Why You Should Never Call unlock() Directly on std::shared_timed_mutex

The std::shared_timed_mutex is a type of reader-writer lock that allows multiple threads to read the protected resource simultaneously



How to Correctly Use std::lognormal_distribution in C++

The std::lognormal_distribution class in C++ doesn't have a public member function or variable named m. Instead, it uses two parameters to define the distribution's shape


How to Use C++ std::latch Safely and Effectively

First, let's quickly review what a latch is. Think of a latch as a one-time-use gate. It's initialized with a counter. Threads can "arrive" at the latch and decrement this counter


Exploring std::make_pair: Common Issues and Modern Alternatives

std::make_pair is a utility function in C++ that helps you create a std::pair object. The magic of std::make_pair is that it uses template argument deduction


How to Fix C++ Destructor Issues: A Friendly Guide

Think of a destructor like a cleanup crew for an object. When an object is created, it's given some memory and resources


A Friendly Guide to C++'s std::format_error Exception

std::format_error is part of the modern C++ formatting utility (available since C++20 with <format>), which is designed to be much safer and more efficient than older methods like printf and even std::stringstream


std::fpos Explained: Pitfalls and Safe Alternatives for Stream Seeking

std::fpos is a class template designed to hold an absolute position in a stream. Think of it as a bookmark in a book; it tells you exactly where you are


Mastering C++ Preprocessor Diagnostics: #error and #pragma

Let's break down the common pitfalls and look at some great alternatives.The #error directive is straightforward it forces the compiler to stop compilation and display a specified message