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

news/2024/9/28 6:48:12
  • 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。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/47026.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

2024嵌入式大作业

2024年上海交通大学嵌入式课程大作业 本学年是嵌入式第一次上升到4学分,即在原本的2学分的理论课程之上增设了2学分的实验课,因而成为了一门大课。 不同老师之间大作业的要求不同,所以我把我们班的实验要求罗列出来,并谈一谈我的实现方案,希望能够作为前人的智慧,起到抛砖…

【Playwright+Python】系列教程(一)环境搭建及脚本录制

前言 看到这个文章,有的同学会说: 六哥,你为啥不早早就写完python系列的文章。 因为有徒弟需要吧,如果你也想学自学,那这篇文章,可以说是我们结缘一起学习的开始吧! 如果对你有用,建议收藏和转发! Playwright是什么? 微软开源自动化测试工具Playwright,支持主流浏览…

苹果CMS 阿里云OSS插件

直接下载插件上传到CMS的addons目录解压点击启用插件点击配置插件配置完毕后进入系统菜单>附件参数配置保存方式改为阿里云OSS即可需要插件直接联系我 :vx:qianjingchuangqi本文来自博客园,作者:ikay,转载请注明原文链接:https://www.cnblogs.com/ikay/p/18255405

PPT使用技巧

PPT使用说明: 查看版本:账户 撤回次数: 自动保存: 图片压缩:(ppt图片默认是压缩的),只针对单个ppt文件 字体嵌入:解决不同电脑导致字体显示的不一样。 ppt多格式导出:如视频、图片、图片型的ppt 参考线: 默认字体: 默认样式:设置所有图形的样式清除占位符:…

点云分割网络PointConv

PDF:《PointConv: Deep Convolutional Networks on 3D Point Clouds》 CODE: https://github.com/DylanWusee/pointconv 一、大体内容 PointConv是一种在非均匀采样下对3D点云进行卷积的运算,可以用来构建深度卷积网络,其将卷积核视为由权重函数和密度函数组成的三维点的局部…

算法金 | 一个强大的算法模型:t-SNE !!

大侠幸会,在下全网同名「算法金」0 基础转 AI 上岸,多个算法赛 Top「日更万日,让更多人享受智能乐趣」t-SNE(t-Distributed Stochastic Neighbor Embedding)是一种用于降维和数据可视化的非线性算法。它被广泛应用于图像处理、文本挖掘和生物信息学等领域,特别擅长处理高…

软件测试理论基础Part2

测试用例设计 等价类划分法有效等价类满足需求的无效等价类不满足需求的等价类划分方法操作步骤明确需求 确定有效和无效等价类 编写测试用例边界值划分法 边界范围上点处在边界上的点(正好等于)离点离上点最近的点内点范围内的点开区间,闭区间[开始值,结束值] - 闭区间,包含开…