Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Parsing Strings into Time | Formatting and Converting Time
C++ Dates and Times

bookParsing Strings into Time

Parsing date and time strings is a common task when dealing with user input, data files, or external APIs. In C++, you can convert a formatted string into a struct tm using the std::get_time manipulator. This allows you to take a string like "2023-06-25 16:45:00" and turn it into a structured representation that C++ time functions can use for further processing.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425
#include <iostream> #include <iomanip> #include <sstream> #include <ctime> int main() { std::string datetime_str = "2023-06-25 16:45:00"; std::tm tm = {}; std::istringstream ss(datetime_str); ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); if (ss.fail()) std::cout << "Parsing failed.\n"; else { std::cout << "Year: " << (tm.tm_year + 1900) << "\n"; std::cout << "Month: " << (tm.tm_mon + 1) << "\n"; std::cout << "Day: " << tm.tm_mday << "\n"; std::cout << "Hour: " << tm.tm_hour << "\n"; std::cout << "Minute: " << tm.tm_min << "\n"; std::cout << "Second: " << tm.tm_sec << "\n"; } }

The std::get_time manipulator reads formatted date and time information from an input stream into a struct tm. You provide a format string that matches the input, such as "%Y-%m-%d %H:%M:%S" for a typical timestamp. After attempting to parse, you should check for errors by testing the stream's fail() state. If the parsing is successful, the struct tm fields are filled with the corresponding values; otherwise, you need to handle the error gracefully.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> #include <iomanip> #include <sstream> #include <ctime> int main() { std::string invalid_datetime_str = "2023-06-99 25:61:61"; std::tm tm = {}; std::istringstream ss(invalid_datetime_str); ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); if (ss.fail()) std::cout << "Error: Failed to parse date/time string.\n"; else std::cout << "Parsed date/time successfully.\n"; }

When parsing date and time strings, always validate the resulting struct tm fields after parsing. Even if parsing succeeds, values such as months greater than 12 or hours above 23 may indicate invalid input. Always check for logical correctness, not just parsing success, to ensure your application handles dates and times reliably.

std::get_time is locale-dependent. The format string you use must match both the input string and the locale settings of the stream. Always specify the format carefully to avoid unexpected parsing errors.

question mark

What is the purpose of std::get_time in C++?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show me an example of how to use std::get_time in C++?

What are some common mistakes to avoid when parsing dates and times?

How do I handle different date and time formats with std::get_time?

bookParsing Strings into Time

Sveip for å vise menyen

Parsing date and time strings is a common task when dealing with user input, data files, or external APIs. In C++, you can convert a formatted string into a struct tm using the std::get_time manipulator. This allows you to take a string like "2023-06-25 16:45:00" and turn it into a structured representation that C++ time functions can use for further processing.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425
#include <iostream> #include <iomanip> #include <sstream> #include <ctime> int main() { std::string datetime_str = "2023-06-25 16:45:00"; std::tm tm = {}; std::istringstream ss(datetime_str); ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); if (ss.fail()) std::cout << "Parsing failed.\n"; else { std::cout << "Year: " << (tm.tm_year + 1900) << "\n"; std::cout << "Month: " << (tm.tm_mon + 1) << "\n"; std::cout << "Day: " << tm.tm_mday << "\n"; std::cout << "Hour: " << tm.tm_hour << "\n"; std::cout << "Minute: " << tm.tm_min << "\n"; std::cout << "Second: " << tm.tm_sec << "\n"; } }

The std::get_time manipulator reads formatted date and time information from an input stream into a struct tm. You provide a format string that matches the input, such as "%Y-%m-%d %H:%M:%S" for a typical timestamp. After attempting to parse, you should check for errors by testing the stream's fail() state. If the parsing is successful, the struct tm fields are filled with the corresponding values; otherwise, you need to handle the error gracefully.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> #include <iomanip> #include <sstream> #include <ctime> int main() { std::string invalid_datetime_str = "2023-06-99 25:61:61"; std::tm tm = {}; std::istringstream ss(invalid_datetime_str); ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); if (ss.fail()) std::cout << "Error: Failed to parse date/time string.\n"; else std::cout << "Parsed date/time successfully.\n"; }

When parsing date and time strings, always validate the resulting struct tm fields after parsing. Even if parsing succeeds, values such as months greater than 12 or hours above 23 may indicate invalid input. Always check for logical correctness, not just parsing success, to ensure your application handles dates and times reliably.

std::get_time is locale-dependent. The format string you use must match both the input string and the locale settings of the stream. Always specify the format carefully to avoid unexpected parsing errors.

question mark

What is the purpose of std::get_time in C++?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
some-alt