Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

1. 关键词

C++ 数据格式化 字符串处理 std::string 时间戳 跨平台

2. strfmt.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#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. 测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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. 运行结果

1
2
3
4
5
6
----------------------------------------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。

推荐阅读
C++时间处理3-格式化时间戳 C++时间处理3-格式化时间戳 C++时间处理1-获取当前时间戳 C++时间处理1-获取当前时间戳 C++ 时间处理7-日期时间类 C++ 时间处理7-日期时间类

评论