首页 > 编程语言 >C++数据格式化3 - 格式化时间区间(使用时长)

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

时间:2024-06-18 23:43:24浏览次数:12  
标签:std 格式化 string seconds text fmt C++ 区间 include

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。

标签:std,格式化,string,seconds,text,fmt,C++,区间,include
From: https://www.cnblogs.com/luoweifu/p/18255424

相关文章

  • C++数据格式化4 - 格式化时间戳
    1.关键词2.strfmt.h3.strfmt.cpp4.测试代码5.运行结果6.源码地址1.关键词C++数据格式化字符串处理std::string时间戳跨平台2.strfmt.h#pragmaonce#include<string>#include<cstdint>#include<sstream>#include<iomanip>#include"timeutil.h&quo......
  • C++
    求一个三位数题目描述求这样一个三位数,该三位数等于其每位数字的阶乘之和。即abc=a!+b!+c!(n!表示n的阶乘)输入无输出输出这个数亲密数对题目描述键盘输入N,N在2至2000之间,求2至N中的亲密数对,就是A的因子和等于B,B的因子和等于A,且A≠B。如48......
  • C++数据格式化2 - 将文件大小转换为人类易读的格式
    1.关键词2.strfmt.h3.strfmt.cpp4.测试代码5.运行结果6.源码地址1.关键词C++数据格式化字符串处理std::string文件大小跨平台2.strfmt.h#pragmaonce#include<string>#include<cstdint>#include<sstream>#include<iomanip>namespacecutl{......
  • C++数据格式化1 - uint转换成字符串 & double转换成字符串
    1.关键词2.strfmt.h3.strfmt.cpp4.测试代码5.运行结果6.源码地址1.关键词C++数据格式化字符串处理std::stringintdouble跨平台2.strfmt.h#pragmaonce#include<string>#include<cstdint>#include<sstream>#include<iomanip>namespacecutl{......
  • 跟我从零开始学C++(C++代码基础)2
    引言在上一章我们下载了学习C++的工具,VisualStudio编译器,也学习了一些简单基础的语法,知道了一些C++的相关的背景知识,还有C++的语法基础,我们这节课就来接着学习有关C++的内容。本章的内容有运算符和表达式,以及控制结构。编写我们的第一个代码Helloworld!不论是是学习什么......
  • C++PrimerPlus:第十三章类和继承:访问控制:protected
    第十三章类和继承:访问控制:protected提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加例如:访问控制:protected提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录第十三章类和继承:访问控制:protected前言一、访问控制:protected总结......
  • 理解 C++ 中的对象类型与绑定机制:静态绑定 vs 动态绑定
    静态绑定和动态绑定概念解释对象的静态类型:对象在声明时采用的类型,在编译期确定,无法更改。对象的动态类型:对象在运行期实际表现的类型,在运行期决定,对象的动态类型可以更改(通过多态和指针/引用的方式)。静态绑定:绑定的是对象的静态类型,某特性(比如函数调用)依赖于对象的静......
  • 《C++ Primer》导学系列:第 5 章 - 语句
    5.1简单语句5.1.1基本概念在C++中,简单语句是构成程序的基本单元。每个简单语句以分号(;)结束,可以是表达式、声明或空语句。5.1.2表达式语句表达式语句是由表达式构成的语句,通常用于计算、赋值和函数调用。示例代码#include<iostream>intmain(){inta=10;......
  • c++期末题库三(读程序写结果)
    1.#include<iostream>usingnamespacestd;classBC{public:BC(intn=100){val=n;cout<<"defaultcon."<<endl;}BC(BC&t){val=t.val;cout<<"Copycon."<<endl;}BC&operator=(BC&t){val=t.val;c......
  • VC++ gen uuid and time
    //ConsoleApplication2.cpp:Thisfilecontainsthe'main'function.Programexecutionbeginsandendsthere.//#define_CRT_SECURE_NO_WARNINGS#pragmacomment(lib,"rpcrt4.lib")#include<windows.h>#include<rpcdce.h>#......