[英]Can std::chrono::from_stream convert string to time_point with microseconds accuracy?
TLDR :
std::chrono::sys_time<std::chrono::microseconds> tTimePoint;
std::istringstream stream("2020-09-16 22:00:00.123456");
std::chrono::from_stream(stream, "%Y-%m-%d %H:%M:%S", tTimePoint);
我希望上面的代碼將.123456
解析為微秒。 但是,運行此tTimePoint
僅包含不包括亞秒的日期和時間。
更長:
我在Windows 10上使用帶有\\cpplatest標志的Visual Studio 2019 。 我有一個簡單的輸入,用戶可以在其中定義輸入的日期時間格式。
字符串可以看起來像2020-09-16T22:00:00.123456
或16.09.2020 22:00:00.123456
或任何其他有效的日期時間格式,只要有一種方法可以使用std::chrono的cppreference 中列出的限定符來描述它: :from_stream 。
根據參考資料和我以前使用std::format()
(使用相同格式)的經驗,我假設%S
也解析亞秒。
我也嘗試過std::chrono::parse
但這並沒有說明%S
會解析小數點后的亞秒。 我嘗試了各種不同的格式和日期時間格式,以確保此問題不會僅由於某些不規則的日期時間格式而發生。
這是cppreference文檔中的錯誤,這是 Visual Studio 實現中未完全實現的功能,還是(很可能)我只是簡單地遺漏了一些明顯的內容?
感謝您為我提供的任何幫助!
據我所知,VS2019 在使用from_stream
和time_point
時存在缺陷,其中應該使用亞秒。 VS2019 庫中有一個地方應該處理它 - 但它永遠不會進入這種情況。
幸運的是,如果您使用duration
而不是time_point
,它會起作用,因此您可以解決這個問題:
#include <chrono>
#include <sstream>
#include <iostream>
int main() {
std::istringstream stream("2020-09-16 22:00:00.123456");
std::chrono::sys_time<std::chrono::microseconds> tTimePoint;
// extract all but the second:
std::chrono::from_stream(stream, "%Y-%m-%d %H:%M:", tTimePoint);
std::chrono::microseconds micros;
// extract the second:
std::chrono::from_stream(stream, "%S", micros);
// add the duration to the time_point
tTimePoint += micros;
std::cout << tTimePoint << '\n';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.