简体   繁体   English

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

[英]Parse time format "DD/MM/YYYY at hh:mm:ss" & others using std::chrono::from_stream()

I'm currently trying to parse some info about the start time of an experiment as listed in a log file.我目前正在尝试解析日志文件中列出的有关实验开始时间的一些信息。 After reading in the file important info, eg column titles, start time, time between measurements, is parsed using <regex> .在读入文件重要信息后,例如列标题、开始时间、测量间隔时间,使用<regex>进行解析。

I'm trying to use the std::chrono::from_stream(...) function to parse a string with the format "DD/MM/YYYY at hh:mm:ss" into a std::chrono::time_point , example of a string:我正在尝试使用std::chrono::from_stream(...)函数将格式为 "DD/MM/YYYY at hh:mm:ss" 的字符串解析为std::chrono::time_point ,字符串示例:

08/03/2021 at 09:37:25 2021 年 8 月 3 日 09:37:25

At the moment I'm attempting this using the following function which attempts to construct a duration from a provided string to parse & a string to parse it with, then converting that to a time_point so I have control over the clock used:目前,我正在尝试使用以下函数尝试从提供的字符串构建持续时间以进行解析和解析它的字符串,然后将其转换为 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.is_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);
    */
}

The MS documentation for from_stream can be found here .可以在此处找到 from_stream 的 MS 文档。 With details about different format characters just after the from_stream docs.在 from_stream 文档之后提供有关不同格式字符的详细信息。

ss.is_good() ? ss.is_good() ?

Is that a type-o in your question or an extension in the Visual Studio std::lib?这是您问题中的类型 o 还是 Visual Studio std::lib 中的扩展?

I'm going to guess it is a type-o and that you meant ss.good() ...我猜它是一个 o 型,你的意思是ss.good() ...

The good() member function checks if all state flags are off: good()成员函数检查是否所有状态标志都关闭:

  • failbit
  • badbit
  • eofbit

eofbit in particular often does not mean "error".特别是eofbit通常并不意味着“错误”。 It simply means that the parsing reached the end of the stream.它只是意味着解析到达了流的末尾。 You are interpreting "end of stream" as a parsing error.您将“流结束”解释为解析错误。

Instead check failbit or badbit .而是检查failbitbadbit This is most easily done with the fail() member function .使用fail()成员函数最容易做到这一点。

if (!ss.fail())
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM