C++数据格式化4 - 格式化时间戳
1. 关键词
C++ 数据格式化 字符串处理 std::string 时间戳 跨平台
2. strfmt.h
#pragma once
#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>
#include "timeutil.h"
namespace cutl
{
/**
* @brief Format a timestamp to a human-readable string with a given format.
*
* @param second the timestamp in seconds.
* @param local whether to use local time or UTC time, default is local time.
* If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
* @param fmt the format of the formatted string. useage like std::put_time, see https://en.cppreference.com/w/cpp/io/manip/put_time
* @return std::string the formatted string.
*/
std::string fmt_timestamp(uint64_t second, bool local, const std::string &fmt);
/**
* @brief Format a timestamp to a human-readable string.
*
* @param second the timestamp in seconds.
* @param local whether to use local time or UTC time, default is local time.
* If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
* @return std::string the formatted string.
*/
std::string fmt_timestamp_s(uint64_t second, bool local = true);
/**
* @brief Format a timestamp to a human-readable string.
*
* @param ms the timestamp in milliseconds.
* @param local whether to use local time or UTC time, default is local time.
* If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
* @return std::string the formatted string.
*/
std::string fmt_timestamp_ms(uint64_t ms, bool local = true);
/**
* @brief Format a timestamp to a human-readable string.
*
* @param us the timestamp in microseconds.
* @param local whether to use local time or UTC time, default is local time.
* If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
* @return std::string the formatted string.
*/
std::string fmt_timestamp_us(uint64_t us, bool local = true);
} // namespace cutl
3. strfmt.cpp
#include <sstream>
#include <iomanip>
#include <bitset>
#include "strfmt.h"
namespace cutl
{
// https://blog.csdn.net/u010087712/article/details/50731222
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();
}
// 格式化时间戳,second单位:秒
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);
}
} // namespace cutl
4. 测试代码
#include "common.hpp"
#include "strfmt.h"
void TestFormatTimestamp()
{
PrintSubTitle("TestFormatTimestamp");
// timestamp
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。