C++数据格式化3 - 格式化时间区间(使用时长)

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。

本文由博客一文多发平台 OpenWrite 发布!

  • 24
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌尘(MoChen)

爱打赏的人技术成长更开哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值