我得到了std::vector<std::string>一個字串格式的時間戳串列(假設我們有一個現成的)std::vector<std::string> = {"12:27:37.740002", "19:37:17.314002", "20:00:07.140902",...}。沒有日期,沒有時區。將這些字串決議為某種 C 型別(std::chrono::time_point?)以便稍后能夠執行一些比較和排序的更好方法是什么。
例如:比較值,從"20:00:07.140902"和決議值,從 決議"20:00:07.000000"。
C 17 沒問題,但我不能使用任何第三方庫(Boost、Date 等)。保持微秒精度至關重要。
uj5u.com熱心網友回復:
您可以完全使用 C 標準庫功能構建此功能。要決議字串,請使用 std::regex。對于時間相關的資料型別使用 std::chrono
例子 :
#include <stdexcept>
#include <regex>
#include <chrono>
#include <iostream>
auto parse_to_timepoint(const std::string& input)
{
// setup a regular expression to parse the input string
// https://regex101.com/
// each part between () is a group and will end up in the match
// [0-2] will match any character from 0 to 2 etc..
// [0-9]{6} will match exactly 6 digits
static const std::regex rx{ "([0-2][0-9]):([0-5][0-9]):([0-5][0-9])\\.([0-9]{6})" };
std::smatch match;
if (!std::regex_search(input, match, rx))
{
throw std::invalid_argument("input string is not a valid time string");
}
// convert each matched group to the corresponding value
// note match[0] is the complete matched string by the regular expression
// we only need the groups which start at index 1
const auto& hours = std::stoul(match[1]);
const auto& minutes = std::stoul(match[2]);
const auto& seconds = std::stoul(match[3]);
const auto& microseconds = std::stoul(match[4]);
// build up a duration
std::chrono::high_resolution_clock::duration duration{};
duration = std::chrono::hours(hours);
duration = std::chrono::minutes(minutes);
duration = std::chrono::seconds(seconds);
duration = std::chrono::microseconds(microseconds);
// then return a time_point (note this will not help you with correctly handling day boundaries)
// since there is no date in the input string
return std::chrono::high_resolution_clock::time_point{ duration };
}
int main()
{
std::string input1{ "20:00:07.140902" };
std::string input2{ "20:00:07.000000" };
auto tp1 = parse_to_timepoint(input1);
auto tp2 = parse_to_timepoint(input2);
std::cout << "start time = " << ((tp1 < tp2) ? input1 : input2) << "\n";
std::cout << "end time = " << ((tp1 >= tp2) ? input1 : input2) << "\n";
return 0;
}
uj5u.com熱心網友回復:
我不明白為什么這不起作用。使用std::chrono::from_stream決議字串轉換為一個時間點,那么就比較兩個時間點。
但是,我現在一直在使用 Visual Studio 2022 17.0.2(社區版)嘗試它,但它無法將字串決議為tp.
有此答案由特德Lyngmo在談論決議與亞秒秒時的一個錯誤(固定在VS2022 17.0.3)。我不得不說,他的解決方案在我的 VS2022 中對我也不起作用。
無論如何,您可能想嘗試一下。
#include <chrono>
#include <iomanip> // boolalpha
#include <iostream> // cout
#include <sstream> // istringstream
#include <string>
auto parse_string_to_tp(const std::string& str)
{
std::istringstream iss{ str };
std::chrono::sys_time<std::chrono::microseconds> tp{};
std::chrono::from_stream(iss, "%H:%M:%S", tp); // or simply "%T"
return tp;
}
int main()
{
const std::string str1{ "12:27:37.740002" };
const std::string str2{ "13:00:00.500000" };
auto tp1{ parse_string_to_tp(str1) };
auto tp2{ parse_string_to_tp(str2) };
std::cout << "tp1 < tp2: " << std::boolalpha << (tp1 < tp2) << "\n";
std::cout << "tp2 < tp1: " << std::boolalpha << (tp2 < tp1) << "\n";
}
編輯:如果您只使用持續時間而不是時間點,它會起作用:
#include <chrono>
#include <iomanip> // boolalpha
#include <iostream> // cout
#include <sstream> // istringstream
#include <string>
auto parse_string_to_duration(const std::string& str)
{
std::istringstream iss{ str };
std::chrono::microseconds d{};
std::chrono::from_stream(iss, "%T", d);
return d;
}
int main()
{
const std::string str1{ "12:27:37.740002" };
const std::string str2{ "23:39:48.500000" };
auto d1{ parse_string_to_duration(str1) };
auto d2{ parse_string_to_duration(str2) };
std::cout << "d1 < d2: " << std::boolalpha << (d1 < d2) << "\n";
std::cout << "d2 < d1: " << std::boolalpha << (d2 < d1) << "\n";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370715.html
