1 Star 0 Fork 0

lugang/cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TimeStamp.h 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
lugang51 提交于 2023-12-21 22:03 +08:00 . init file
//
// Created by lester on 2023/12/21.
//
#ifndef TEST_TIMESTAMP_H
#define TEST_TIMESTAMP_H
#include <sys/time.h>
class TimeStamp
{
private:
static const int32_t U_SECONDS = 1000 * 1000;
public:
int64_t u_seconds_{};
explicit TimeStamp(uint64_t u_seconds):u_seconds_(u_seconds){}
static TimeStamp Now(){
struct timeval tv{};
gettimeofday(&tv,nullptr);
int64_t seconds = tv.tv_sec;
return TimeStamp(seconds * U_SECONDS + tv.tv_usec);
}
std::string Format() const{
char buf[64] = {0};
auto seconds = static_cast<time_t>(u_seconds_ / U_SECONDS);
struct tm tm_time{};
gmtime_r(&seconds, &tm_time);
auto microseconds = static_cast<int>(u_seconds_ % U_SECONDS);
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
microseconds);
return buf;
}
};
#endif //TEST_TIMESTAMP_H
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lugang0218/cpp.git
git@gitee.com:lugang0218/cpp.git
lugang0218
cpp
cpp
master

搜索帮助