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。
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签