C++資料格式化3 - 格式化時間區間(使用時長)

陌尘(MoChen)發表於2024-06-18
  • 1. 關鍵詞
  • 2. strfmt.h
  • 3. strfmt.cpp
  • 4. 測試程式碼
  • 5. 執行結果
  • 6. 原始碼地址

1. 關鍵詞

關鍵字:

C++ 資料格式化 字串處理 std::string 時間區間 跨平臺

應用場景:

想對一個時間區間(如用時:2000s)進行格式化,轉化成人類易讀的時分秒的格式。

2. strfmt.h

#pragma once

#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>
#include "timeutil.h"

namespace cutl
{
    /**
     * @brief Format a time duration to a human-readable string.
     *
     * @param seconds the duration in seconds.
     * @return std::string the formatted string.
     */
    std::string fmt_timeduration_s(uint64_t seconds);
    /**
     * @brief Format a time duration to a human-readable string.
     *
     * @param microseconds the duration in microseconds.
     * @return std::string the formatted string.
     */
    std::string fmt_timeduration_ms(uint64_t microseconds);
    /**
     * @brief Format a time duration to a human-readable string.
     *
     * @param nanoseconds the duration in nanoseconds.
     * @return std::string the formatted string.
     */
    std::string fmt_timeduration_us(uint64_t nanoseconds);
} // namespace cutl

3. strfmt.cpp

#include <sstream>
#include <iomanip>
#include <bitset>
#include "strfmt.h"

namespace cutl
{
    std::string fmt_timeduration_s(uint64_t seconds)
    {
        std::string text;
        if (seconds > ONE_DAY)
        {
            uint64_t day = seconds / ONE_DAY;
            text += std::to_string(day) + "d:";
        }

        if (seconds > ONE_HOUR)
        {
            uint64_t hour = (seconds % ONE_DAY) / ONE_HOUR;
            text += fmt_uint(hour, 2) + "h:";
        }

        if (seconds > ONE_MIN)
        {
            uint64_t min = (seconds % ONE_HOUR) / ONE_MIN;
            text += fmt_uint(min, 2) + "m:";
        }

        uint64_t sec = (seconds % ONE_MIN);
        text += fmt_uint(sec, 2) + "s";

        return text;
    }

    std::string fmt_timeduration_ms(uint64_t microseconds)
    {
        auto s = microseconds / THOUSAND;
        auto ms = microseconds % THOUSAND;
        auto text = fmt_timeduration_s(s);
        text += "." + fmt_uint(ms, 3) + "ms";
        return text;
    }

    std::string fmt_timeduration_us(uint64_t nanoseconds)
    {
        auto s = nanoseconds / MILLION;
        auto ms = nanoseconds % MILLION;
        auto text = fmt_timeduration_s(s);
        text += "." + fmt_uint(ms, 6) + "us";
        return text;
    }
} // namespace cutl

4. 測試程式碼

#include "common.hpp"
#include "strfmt.h"

void TestFormatDurationTime()
{
    PrintSubTitle("TestFormatDurationTime");

    // 180100345), "2d:2h:1m:40s:345ms"
    std::cout << "duration1: " << cutl::fmt_timeduration_s(180100) << std::endl;
    std::cout << "duration2: " << cutl::fmt_timeduration_ms(180100345) << std::endl;
    std::cout << "duration3: " << cutl::fmt_timeduration_us(180100345678) << std::endl;
}

5. 執行結果

---------------------------------------TestFormatDurationTime---------------------------------------
duration1: 2d:02h:01m:40s
duration2: 2d:02h:01m:40s.345ms
duration3: 2d:02h:01m:40s.345678us

6. 原始碼地址

更多詳細程式碼,請檢視本人寫的C++ 通用工具庫: common_util, 本專案已開源,程式碼簡潔,且有詳細的文件和Demo。

相關文章