Skip to content

Commit 69d0aa8

Browse files
committed
opt(util): 使用GetUtcSeconds(),GetUtcMilliseconds(),GetUtcMicroseconds()替代长的函数
1 parent 16c5ff9 commit 69d0aa8

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

modules/base/defines.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,8 @@
100100
#define UNUSED_VAR(x) (void)(x)
101101
#endif
102102

103+
#ifndef DEPRECATED
104+
#define DEPRECATED __attribute__((deprecated))
105+
#endif
106+
103107
#endif //TBOX_BASE_DEFINES_H_20171030

modules/trace/sink_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TEST(Sink, MultiThread) {
9898

9999
auto test_func = [&ts] (const std::string name) {
100100
for (int i = 0; i < 1000; ++i) {
101-
ts.commitRecord(name.c_str(), "A", 100, util::GetCurrentMicrosecondsFrom1970(), 10);
101+
ts.commitRecord(name.c_str(), "A", 100, util::GetUtcMicroseconds(), 10);
102102
}
103103
};
104104

modules/util/timestamp.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,61 @@
2424
namespace tbox {
2525
namespace util {
2626

27-
uint32_t GetCurrentSecondsFrom1970()
27+
uint32_t GetUtcSeconds()
2828
{
2929
struct timeval tv;
3030
struct timezone tz;
31-
gettimeofday(&tv, &tz);
31+
32+
if (gettimeofday(&tv, &tz) != 0)
33+
return 0;
3234

3335
return tv.tv_sec;
3436
}
3537

36-
uint64_t GetCurrentMillisecondsFrom1970()
38+
uint32_t GetCurrentSecondsFrom1970() { return GetUtcSeconds(); }
39+
40+
uint64_t GetUtcMilliseconds()
3741
{
3842
struct timeval tv;
3943
struct timezone tz;
40-
gettimeofday(&tv, &tz);
44+
45+
if (gettimeofday(&tv, &tz) != 0)
46+
return 0;
4147

4248
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
4349
}
4450

45-
uint64_t GetCurrentMicrosecondsFrom1970()
51+
uint64_t GetCurrentMillisecondsFrom1970() { return GetUtcMilliseconds(); }
52+
53+
uint64_t GetUtcMicroseconds()
4654
{
4755
struct timeval tv;
4856
struct timezone tz;
49-
gettimeofday(&tv, &tz);
57+
58+
if (gettimeofday(&tv, &tz) != 0)
59+
return 0;
5060

5161
return tv.tv_sec * 1000000 + tv.tv_usec;
5262
}
5363

54-
std::string GetUtcTimeString(uint32_t sec)
64+
uint64_t GetCurrentMicrosecondsFrom1970() { return GetUtcMicroseconds(); }
65+
66+
bool GetUtc(uint32_t &sec, uint64_t &usec)
5567
{
56-
time_t ts_sec = sec;
68+
struct timeval tv;
69+
struct timezone tz;
70+
71+
if (gettimeofday(&tv, &tz) != 0)
72+
return false;
73+
74+
sec = tv.tv_sec;
75+
usec = tv.tv_usec;
76+
return true;
77+
}
78+
79+
std::string GetUtcTimeString(uint32_t utc_sec)
80+
{
81+
time_t ts_sec = utc_sec;
5782
struct tm tm;
5883
gmtime_r(&ts_sec, &tm);
5984

@@ -63,9 +88,11 @@ std::string GetUtcTimeString(uint32_t sec)
6388
return timestamp_str;
6489
}
6590

66-
std::string GetLocalTimeString(uint32_t sec)
91+
std::string GetUtcTimeString() { return GetUtcTimeString(GetUtcSeconds()); }
92+
93+
std::string GetLocalTimeString(uint32_t utc_sec)
6794
{
68-
time_t ts_sec = sec;
95+
time_t ts_sec = utc_sec;
6996
struct tm tm;
7097
localtime_r(&ts_sec, &tm);
7198

@@ -75,5 +102,7 @@ std::string GetLocalTimeString(uint32_t sec)
75102
return timestamp_str;
76103
}
77104

105+
std::string GetLocalTimeString() { return GetLocalTimeString(GetUtcSeconds()); }
106+
78107
}
79108
}

modules/util/timestamp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,29 @@ namespace tbox {
2727
namespace util {
2828

2929
//! 获取当前的时间戳,精确到秒
30+
uint32_t GetUtcSeconds();
3031
uint32_t GetCurrentSecondsFrom1970();
3132

3233
//! 获取当前的时间戳,精确到毫秒
34+
uint64_t GetUtcMilliseconds();
3335
uint64_t GetCurrentMillisecondsFrom1970();
3436

3537
//! 获取当前的时间戳,精确到微秒
38+
uint64_t GetUtcMicroseconds();
3639
uint64_t GetCurrentMicrosecondsFrom1970();
3740

41+
//! 获取当前的时间戳,秒 + 微秒
42+
bool GetUtc(uint32_t &sec, uint64_t &usec);
43+
3844
//! 获取指定时间戳的0时区时间字串
3945
std::string GetUtcTimeString(uint32_t utc_sec);
46+
//! 获取当前时间戳的0时区时间字串
47+
std::string GetUtcTimeString();
4048

4149
//! 获取指定时间戳的本地时间字串
4250
std::string GetLocalTimeString(uint32_t utc_sec);
51+
//! 获取当前时间戳的本地时间字串
52+
std::string GetLocalTimeString();
4353

4454
}
4555
}

0 commit comments

Comments
 (0)