將時間戳字符串轉換為“HH:MM:SS.microseconds”格式的建議

psb

我得到了std::vector<std::string>一個字符串格式的時間戳列表(假設我們有一個現成的std::vector<std::string> = {"12:27:37.740002", "19:37:17.314002", "20:00:07.140902",...}沒有日期,沒有時區。將這些字符串解析為某種 C++ 類型(std::chrono::time_point?)以便稍後能夠執行一些比較和排序的更好方法是什麼

例如:比較值,從"20:00:07.140902"解析值,從 解析"20:00:07.000000"

C++17 沒問題,但我不能使用任何第三方庫(Boost、Date 等)。保持微秒精度至關重要。

佩皮恩·克萊默

您可以完全使用 C++ 標準庫功能構建此功能。要解析字符串,請使用 std::regex。對於時間相關的數據類型使用 std::chrono

例子 :

#include <stdexcept>
#include <regex>
#include <chrono>
#include <iostream>

auto parse_to_timepoint(const std::string& input)
{
    // setup a regular expression to parse the input string
    // https://regex101.com/
    // each part between () is a group and will end up in the match
    // [0-2] will match any character from 0 to 2 etc..
    // [0-9]{6} will match exactly 6 digits
    static const std::regex rx{ "([0-2][0-9]):([0-5][0-9]):([0-5][0-9])\\.([0-9]{6})" };
    std::smatch match;

    if (!std::regex_search(input, match, rx))
    {
        throw std::invalid_argument("input string is not a valid time string");
    }

    // convert each matched group to the corresponding value
    // note match[0] is the complete matched string by the regular expression
    // we only need the groups which start at index 1
    const auto& hours = std::stoul(match[1]);
    const auto& minutes = std::stoul(match[2]);
    const auto& seconds = std::stoul(match[3]);
    const auto& microseconds = std::stoul(match[4]);
    
    // build up a duration
    std::chrono::high_resolution_clock::duration duration{};
    duration += std::chrono::hours(hours);
    duration += std::chrono::minutes(minutes);
    duration += std::chrono::seconds(seconds);
    duration += std::chrono::microseconds(microseconds);

    // then return a time_point (note this will not help you with correctly handling day boundaries)
    // since there is no date in the input string
    return std::chrono::high_resolution_clock::time_point{ duration };
}

int main()
{
    std::string input1{ "20:00:07.140902" };
    std::string input2{ "20:00:07.000000" };

    auto tp1 = parse_to_timepoint(input1);
    auto tp2 = parse_to_timepoint(input2);

    std::cout << "start time = " << ((tp1 < tp2) ? input1 : input2) << "\n";
    std::cout << "end time = " << ((tp1 >= tp2) ? input1 : input2) << "\n";

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何將格式 yyyy-mm-ddThh:mm:ss 轉換為 unix 時間戳?

如何將當前日期時間與 hh:mm:ss 轉換為 python 中的時間戳?

如何在 SNOWFLAKE 中將長口頭日期時間轉換為時間戳(YYYY-MM-DD HH:MM:SS)

格式时间:hh:mm:ss

dd-MM-yyyy hh:mm:ss 格式的字符串到 SQL Server 中的日期時間

SQL:以微秒為單位將持續時間轉換為 DD:HH:MM:SS

如何在Java中以字符串格式获取当前时间戳?“ yyyy.MM.dd.HH.mm.ss”

如何在 Progress SQL 中將整數(時間)轉換為 HH:MM:SS::00

正則表達式從字符串中以 HH:MM:SS 格式提取時間

將 GMT 日期字符串轉換為 PST 日期字符串並將輸出格式設置為 'mm/dd/yyyy - hh:mi'

MomentJS - 將 YYYYMMDD 和 HH:MM 轉換為 YYYY-MM-DDThh:mm:ss

Youtube API hh:mm:ss格式的seekTo()

HH:MM:SS 格式的 SQLite 时差计算

更改时间格式HH:MM:SS

与格式 HH:mm:ss 不匹配

如何將日期字符串“yyyy-mm-ddThh:mm:ssZ”轉換為時間戳

如何验证yyyy-mm-dd hh:mm:ss格式

以dd / mm / yyyy hh:mm:ss格式转换日期对象

无法将日期字符串转换为 YYYY-MM-DDTHH:MM:SS-HH:MM 格式

hh:mm:ss 格式的两个时间戳的区别

以[HH:MM:SS]格式更新文档中的时间戳?

Java格式yyyy-MM-dd'T'HH:mm:ss.SSSz到yyyy-mm-dd HH:mm:ss

将时间格式“dd/MM/yyyy hh:mm:ss”转换为“yyyy-MM-ddThh:mm:ss.SSSz”

如何比较格式为HH:MM:SS的两个时间字符串?

JavaScript秒以时间格式设置为hh:mm:ss的字符串

将HH:MM:SS(AM / PM)的字符串时间格式转换为秒

如何使用HH:MM:SS将difftime对象格式化为字符串

如何在C ++中将chrono :: seconds转换为HH:MM:SS格式的字符串?

在Google BigQuery(Standard SQL)中将HH:MM:SS格式的字符串转换为秒