簡體   English   中英

std::chrono::from_stream 可以以微秒精度將字符串轉換為 time_point 嗎?

[英]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.12345616.09.2020 22:00:00.123456或任何其他有效的日期時間格式,只要有一種方法可以使用std::chronocppreference 中列出的限定符來描述它: :from_stream

根據參考資料和我以前使用std::format() (使用相同格式)的經驗,我假設%S也解析亞秒。

我也嘗試過std::chrono::parse但這並沒有說明%S會解析小數點后的亞秒。 我嘗試了各種不同的格式和日期時間格式,以確保此問題不會僅由於某些不規則的日期時間格式而發生。

這是cppreference文檔中的錯誤,這是 Visual Studio 實現中未完全實現的功能,還是(很可能)我只是簡單地遺漏了一些明顯的內容?

感謝您為我提供的任何幫助!

據我所知,VS2019 在使用from_streamtime_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM