SoFunction
Updated on 2025-03-02

std::get_time and std::put_time in c++

std::get_time and std::put_time

In C++, std::get_time and std::put_time are two practical functions that deal with date and time formatting. They provide an easy way to parse and format datetime strings. They all belong to<iomanip>header file.

They are operators of std::istream and std::ostream, so they do not have constructors in the traditional sense. They are used to format and parse date and time, but themselves do not involve creating objects directly.

std::get_time

std::get_timeis a stream operator that parses datetime strings from streams.

It is actually a function template defined in the header file.

template<typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& get_time(std::basic_istream<CharT, Traits>& is, std::tm* t, const CharT* fmt);
  • is: Input stream (for example, std::istringstream).
  • t: pointer to the structure std::tm, used to store the parsed date and time.
  • fmt: Date-time format string that specifies how input strings are parsed.

usage

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;iomanip&gt;
#include &lt;ctime&gt;

int main() {
    std::string datetime_str = "2024-08-14 15:30:00";
    std::tm tm = {};
    std::istringstream ss(datetime_str);

    // parse date and time    ss &gt;&gt; std::get_time(&amp;tm, "%Y-%m-%d %H:%M:%S");

    if (()) {
        std::cerr &lt;&lt; "Error parsing date and time" &lt;&lt; std::endl;
        return 1;
    }

    // Output analysis results    std::cout &lt;&lt; "Year: " &lt;&lt; tm.tm_year + 1900 &lt;&lt; std::endl;
    std::cout &lt;&lt; "Month: " &lt;&lt; tm.tm_mon + 1 &lt;&lt; std::endl;
    std::cout &lt;&lt; "Day: " &lt;&lt; tm.tm_mday &lt;&lt; std::endl;
    std::cout &lt;&lt; "Hour: " &lt;&lt; tm.tm_hour &lt;&lt; std::endl;
    std::cout &lt;&lt; "Minute: " &lt;&lt; tm.tm_min &lt;&lt; std::endl;
    std::cout &lt;&lt; "Second: " &lt;&lt; tm.tm_sec &lt;&lt; std::endl;

    return 0;
}
  • std::get_time(&tm, "%Y-%m-%d %H:%M:%S"): parses the datetime_str string and fills the result into the std::tm structure. Format string "%Y-%m-%d %H:%M:%S" describes the date-time format of the input string.
  • tm.tm_year: It stores the year starting from 1900, so you need to add 1900 to get the actual year.
  • tm.tm_mon: It stores a month starting from 0, so you need to add 1 to get the actual month.

std::put_time

std::put_timeis another stream operator for formatting the std::tm structure into a string.

It is also a function template defined in the header file.

template<typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& put_time(std::basic_ostream<CharT, Traits>& os, const std::tm* t, const CharT* fmt);
  • os: Output stream (for example, std::ostringstream).
  • t: A pointer to the std::tm structure, used to format date and time.
  • fmt: Date and time format string, used to specify the format of the output.

usage

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;iomanip&gt;
#include &lt;ctime&gt;

int main() {
    std::tm tm = {};
    tm.tm_year = 2024 - 1900; // The year begins in 1900    tm.tm_mon = 8 - 1; // Month starts from 0    tm.tm_mday = 14;
    tm.tm_hour = 15;
    tm.tm_min = 30;
    tm.tm_sec = 0;

    std::ostringstream ss;

    ss &lt;&lt; std::put_time(&amp;tm, "%Y-%m-%d %H:%M:%S");

    std::cout &lt;&lt; "Formatted date and time: " &lt;&lt; () &lt;&lt; std::endl;

    return 0;
}
  • std::put_time(&tm, "%Y-%m-%d %H:%M:%S"): Format the date and time information in the std::tm structure into a string, in the format "%Y-%m-%d %H:%M:%S".
  • (): Get the formatted date and time string.

These two operators are used for date and time parsing and formatting, respectively, but they do not involve constructors that directly create objects. They are stream operation functions provided by the C++ standard library, which handles dates and times through stream operators.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.