Skip to content

Commit

Permalink
feat(util):1.8.1, 添加GetLocalTimeString()与GetUtcTimeString()函数
Browse files Browse the repository at this point in the history
  • Loading branch information
hevake committed Mar 17, 2024
1 parent 03cfafb commit f0f00ad
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/util/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ TEST_CPP_SRC_FILES = \
checksum_test.cpp \
crc_test.cpp \
execute_cmd_test.cpp \
timestamp_test.cpp \

TEST_LDFLAGS := $(LDFLAGS) -ltbox_base -ldl

Expand Down
24 changes: 24 additions & 0 deletions modules/util/timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ uint64_t GetCurrentMillisecondsFrom1970()
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

std::string GetUtcTimeString(uint32_t sec)
{
time_t ts_sec = sec;
struct tm tm;
gmtime_r(&ts_sec, &tm);

char timestamp_str[20];
strftime(timestamp_str, sizeof(timestamp_str), "%F %H:%M:%S", &tm);

return timestamp_str;
}

std::string GetLocalTimeString(uint32_t sec)
{
time_t ts_sec = sec;
struct tm tm;
localtime_r(&ts_sec, &tm);

char timestamp_str[20];
strftime(timestamp_str, sizeof(timestamp_str), "%F %H:%M:%S", &tm);

return timestamp_str;
}

}
}

Expand Down
10 changes: 10 additions & 0 deletions modules/util/timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@
#define TBOX_UTIL_TIMESTAMP_H

#include <cstdint>
#include <string>

namespace tbox {
namespace util {

//! 获取当前的时间戳,精确到秒
uint32_t GetCurrentSecondsFrom1970();

//! 获取当前的时间戳,精确到毫秒
uint64_t GetCurrentMillisecondsFrom1970();

//! 获取指定时间戳的0时区时间字串
std::string GetUtcTimeString(uint32_t utc_sec);

//! 获取指定时间戳的本地时间字串
std::string GetLocalTimeString(uint32_t utc_sec);

}
}

Expand Down
33 changes: 33 additions & 0 deletions modules/util/timestamp_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2024 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include <gtest/gtest.h>

#include "timestamp.h"

namespace tbox {
namespace util {

TEST(Timestamp, GetUtcTimeString)
{
EXPECT_EQ(GetUtcTimeString(0), "1970-01-01 00:00:00");
}

}
}
2 changes: 1 addition & 1 deletion version.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TBOX版本号
TBOX_VERSION_MAJOR := 1
TBOX_VERSION_MINOR := 8
TBOX_VERSION_REVISION := 0
TBOX_VERSION_REVISION := 1

0 comments on commit f0f00ad

Please sign in to comment.