1. 关键词
C++ 数据格式化 字符串处理 std::string 时间戳 跨平台
2. strfmt.h
| #pragma once |
| |
| #include <string> |
| #include <cstdint> |
| #include <sstream> |
| #include <iomanip> |
| #include "timeutil.h" |
| |
| namespace cutl |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| std::string fmt_timestamp(uint64_t second, bool local, const std::string &fmt); |
| |
| |
| |
| |
| |
| |
| |
| |
| std::string fmt_timestamp_s(uint64_t second, bool local = true); |
| |
| |
| |
| |
| |
| |
| |
| |
| std::string fmt_timestamp_ms(uint64_t ms, bool local = true); |
| |
| |
| |
| |
| |
| |
| |
| |
| std::string fmt_timestamp_us(uint64_t us, bool local = true); |
| } |
3. strfmt.cpp
| #include <sstream> |
| #include <iomanip> |
| #include <bitset> |
| #include "strfmt.h" |
| |
| namespace cutl |
| { |
| |
| std::string fmt_timestamp(uint64_t second, bool local, const std::string &fmt) |
| { |
| std::time_t t(second); |
| struct tm datetime; |
| if (local) |
| { |
| datetime = localtime_security(t); |
| } |
| else |
| { |
| datetime = gmtime_security(t); |
| } |
| |
| std::stringstream ss; |
| ss << std::put_time(&datetime, fmt.c_str()); |
| return ss.str(); |
| } |
| |
| |
| std::string fmt_timestamp_by_unit(uint64_t t, timeunit unit, bool local) |
| { |
| uint64_t s = 0; |
| std::string extension; |
| switch (unit) |
| { |
| case timeunit::s: |
| s = t; |
| break; |
| case timeunit::ms: |
| { |
| s = t / THOUSAND; |
| auto ms = t % THOUSAND; |
| extension += "." + fmt_uint(ms, 3); |
| } |
| break; |
| case timeunit::us: |
| { |
| s = t / MILLION; |
| auto us = t % MILLION; |
| extension += "." + fmt_uint(us, 6); |
| } |
| break; |
| default: |
| break; |
| } |
| |
| std::string fmt("%Y-%m-%d %H:%M:%S"); |
| auto time_str = fmt_timestamp(s, local, fmt); |
| time_str += extension; |
| return time_str; |
| } |
| |
| std::string fmt_timestamp_s(uint64_t t, bool local) |
| { |
| return fmt_timestamp_by_unit(t, timeunit::s, local); |
| } |
| |
| std::string fmt_timestamp_ms(uint64_t t, bool local) |
| { |
| return fmt_timestamp_by_unit(t, timeunit::ms, local); |
| } |
| |
| std::string fmt_timestamp_us(uint64_t t, bool local) |
| { |
| return fmt_timestamp_by_unit(t, timeunit::us, local); |
| } |
| } |
4. 测试代码
| #include "common.hpp" |
| #include "strfmt.h" |
| |
| void TestFormatTimestamp() |
| { |
| PrintSubTitle("TestFormatTimestamp"); |
| |
| auto curTimeS = cutl::timestamp(cutl::timeunit::s); |
| auto curTimeMS = cutl::timestamp(cutl::timeunit::ms); |
| auto curTimeUS = cutl::timestamp(cutl::timeunit::us); |
| std::cout << "current datetime s: " << cutl::fmt_timestamp_s(curTimeS) << std::endl; |
| std::cout << "current datetime s in UTC: " << cutl::fmt_timestamp(curTimeS, false, "%Y/%m/%d %H:%M:%S") << std::endl; |
| std::cout << "current datetime ms: " << cutl::fmt_timestamp_ms(curTimeMS) << std::endl; |
| std::cout << "current datetime ms in UTC: " << cutl::fmt_timestamp_ms(curTimeMS, false) << std::endl; |
| std::cout << "current datetime us: " << cutl::fmt_timestamp_us(curTimeUS) << std::endl; |
| } |
5. 运行结果
| ----------------------------------------TestFormatTimestamp----------------------------------------- |
| current datetime s: 2024-06-18 23:22:46 |
| current datetime s in UTC: 2024/06/18 15:22:46 |
| current datetime ms: 2024-06-18 23:22:46.363 |
| current datetime ms in UTC: 2024-06-18 15:22:46.363 |
| current datetime us: 2024-06-18 23:22:46.363835 |
6. 源码地址
更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战