我目前正在嘗試決議日志檔案中列出的有關實驗開始時間的一些資訊。在讀入檔案重要資訊后,例如列標題、開始時間、測量間隔時間,使用<regex>.
我正在嘗試使用該std::chrono::from_stream(...)函式將格式為“DD/MM/YYYY at hh:mm:ss”的字串決議為一個字串std::chrono::time_point示例:
2021 年 8 月 3 日 09:37:25
目前我正在嘗試使用以下函式嘗試從提供的字串構建持續時間以進行決議 & 一個字串來決議它,然后將其轉換為 time_point 以便我可以控制使用的時鐘:
#include <chrono>
#include <string>
#include <sstream>
#include <iostream>
using nano = std::chrono::duration<std::uint64_t, std::nano>;
template <typename Duration>
Duration TimeFormat(const std::string& str,
const std::string& fmt,
const Duration& default_val)
{
Duration dur;
std::stringstream ss{ str };
std::chrono::from_stream(ss, fmt.c_str(), dur);
/*
from_stream sets the failbit of the input stream if it fails to parse
any part of the input format string or if it receives any contradictory
information.
*/
if (ss.good())
{
std::cout << "Successful parse!" << std::endl;
std::cout << dur.count() << std::endl;
return dur;
}
else
{
std::cout << "Failed parse!" << std::endl;
std::cout << dur.count() << std::endl;
return default_val;
}
}
int main()
{
/*
The file is already read in, and regex matches the correct line from the log file and a
format pattern from a related config file.
*/
/*
Two different lines in the log file give:
- str1 = test start time.
- str2 = time between each measurement.
*/
std::string str1("08/03/2021 at 09:37:25"), str2("00:00:05");
std::string fmt1("%d/%m/%Y at %H:%M:%S"), fmt2("%H:%M:%S");
auto test1 = TimeFormat<nano>(str1, fmt1, nano::zero());
/*
--> "Failed parse!" & test1.count() = 14757395258967641292
A little research indicates that this is what VS initializes variables to
in debug mode. If run in release mode test1.count() = 0 in my tests.
*/
auto test2 = TimeFormat<nano>(str2, fmt2, nano::zero());
/*
--> "Failed parse!" & test2.count() = 5000000000 (5 billion nanoseconds)
Chose nanoseconds because it also has to handle windows file times which are measured
relative to 01/01/1601 in hundreds of nanoseconds. Might be worth pointing out.
What's weird is that it fails even though the value it reads is correct.
*/
/*
... Convert to a time_point after this,
e.g auto t1 = std::chrono::time_point<std::chrono::high_resolution_clock, nano>(test1);
*/
}
可以在此處找到 from_stream 的 MS 檔案。在 from_stream 檔案之后提供有關不同格式字符的詳細資訊。
uj5u.com熱心網友回復:
ss.is_good() ?
這是您問題中的型別 o 還是 Visual Studio std::lib 中的擴展?
我猜它是一個 o 型,你的意思是ss.good()......
該good()成員函式檢查是否所有狀態標志都關閉:
failbitbadbiteofbit
eofbit特別是通常并不意味著“錯誤”。它只是意味著決議到達了流的末尾。您將“流結束”解釋為決議錯誤。
而是檢查failbit或badbit。使用fail()成員函式最容易做到這一點。
if (!ss.fail())
...
更新
知道為什么它仍然不會通過第一個字串嗎?
如果它是 VS 實作中的錯誤,或者 C 規范中的錯誤,或者兩者中的錯誤,我都不是 100% 肯定的。無論哪種方式,它都不會回傳您的期望。
對我來說(使用C 20 chrono 預覽庫),第一次決議成功并回傳
34645000000000
如果以 hh:mm:ss.ffffffffff 格式列印,則為:
09:37:25.000000000
也就是說,只有時間部分對回傳值有貢獻。這顯然不是你想要的。您的第一個測驗似乎打算決議 a time_point,而不是 a duration。
這是一個稍微重寫的程式,我認為它可以滿足您的要求,time_point在第一個測驗中決議 a ,在第二個測驗中決議 a duration:
#include <chrono>
#include <string>
#include <sstream>
#include <iostream>
using nano = std::chrono::duration<std::uint64_t, std::nano>;
template <typename TimeType>
TimeType TimeFormat(const std::string& str,
const std::string& fmt,
const TimeType& default_val)
{
TimeType dur;
std::stringstream ss{ str };
std::chrono::from_stream(ss, fmt.c_str(), dur);
/*
from_stream sets the failbit of the input stream if it fails to parse
any part of the input format string or if it receives any contradictory
information.
*/
if (!ss.fail())
{
std::cout << "Successful parse!" << std::endl;
std::cout << dur << std::endl;
return dur;
}
else
{
std::cout << "Failed parse!" << std::endl;
std::cout << dur << std::endl;
return default_val;
}
}
int main()
{
std::string str1("08/03/2021 at 09:37:25"), str2("00:00:05");
std::string fmt1("%d/%m/%Y at %H:%M:%S"), fmt2("%H:%M:%S");
auto test1 = TimeFormat(str1, fmt1, std::chrono::sys_time<nano>{});
auto test2 = TimeFormat(str2, fmt2, nano::zero());
}
對我來說,這個輸出:
Successful parse!
2021-03-08 09:37:25.000000000
Successful parse!
5000000000ns
如果想要以自紀元以??來的納秒為單位的第一次測驗的輸出,那么可以dur time_point使用dur.time_since_epoch(). 這將輸出:
1615196245000000000ns
uj5u.com熱心網友回復:
對不起,我花了很長時間才回復你,我去了健身房,完全忘記了我問的問題!is_good() 是一個錯字,我的錯。在我的編輯器中注意到它后忘記更改示例代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/337807.html
標籤:C C 20 计时码表 视觉-c -2019
上一篇:從C 中的排序陣列中洗掉重復項
