小编Ben*_*ews的帖子

使用 std::chrono::from_stream() 解析时间格式“DD/MM/YYYY at hh:mm:ss”及其他格式

我目前正在尝试解析日志文件中列出的有关实验开始时间的一些信息。读取文件中的重要信息(例如列标题、开始时间、测量之间的时间)后,使用<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 …
Run Code Online (Sandbox Code Playgroud)

c++ c++-chrono c++20 visual-c++-2019

5
推荐指数
1
解决办法
1079
查看次数

标签 统计

c++ ×1

c++-chrono ×1

c++20 ×1

visual-c++-2019 ×1