From 71185ab318997749dfcc314585479581dcb1289d Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Fri, 26 May 2023 16:31:26 +0800 Subject: [PATCH] add chrono misc --- general/src/function/glotime.cpp | 105 +++++++++++++++++++++++++++ general/src/function/glotime.h | 41 +++++++++++ test/src/chrono/CMakeLists.txt | 13 ++++ test/src/chrono/test.cpp | 119 +++++++++++++++++++++++++++++++ 4 files changed, 278 insertions(+) create mode 100644 general/src/function/glotime.cpp create mode 100644 general/src/function/glotime.h create mode 100644 test/src/chrono/CMakeLists.txt create mode 100644 test/src/chrono/test.cpp diff --git a/general/src/function/glotime.cpp b/general/src/function/glotime.cpp new file mode 100644 index 0000000..66e674e --- /dev/null +++ b/general/src/function/glotime.cpp @@ -0,0 +1,105 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +constexpr int64_t SEC = 1000000; +constexpr int64_t MIN = SEC * 60; +constexpr int64_t HOUR = MIN * 60; +constexpr int64_t DAY = HOUR * 24; + +// 返回当前时间作为 格林威治(GMT)时间 距离 GMT时间 1970-1-1 00:00:00 的微秒数 + +int64_t get_time(){ + chrono::system_clock clock; + return chrono::duration_cast( + clock.now().time_since_epoch()).count(); +} + +int64_t get_time_us_greenwich() +{ + chrono::system_clock clock; + return chrono::duration_cast( + clock.now().time_since_epoch()).count(); +} + +// 返回当前时间作为 本地(北京)时间 距离 GMT时间 1970-1-1 00:00:00 的微秒数,等于get_gmtime_us加8小时 +int64_t get_localtime_us() +{ + return get_time_us_greenwich() + HOUR * 8; +} + +// 格林威治时间的微秒数格式化成本地时间字符串 +string gmtime2localstr(int64_t time_us, const string& fmt="%Y-%m-%d %H:%M:%S") +{ + stringstream ss; + time_t t = time_us / SEC; + auto tm = std::localtime(&t); + ss << std::put_time(tm, fmt.c_str()); + return ss.str(); +} + +// 本地时间字符串解析成格林威治时间的微秒数 +int64_t localstr2gmtime(const std::string& s, const std::string& fmt="%Y-%m-%d %H:%M:%S") +{ + stringstream ss; + ss << s; + struct tm tm; + ss >> std::get_time(&tm, fmt.c_str()); + return (int64_t)mktime(&tm) * SEC; +} + +// 字符串转微秒数,不考虑时区 +std::string time2strgm(int64_t time_us, const std::string& fmt="%Y-%m-%d %H:%M:%S") +{ + stringstream ss; + time_t t = time_us / SEC; + auto tm = std::gmtime(&t); + ss << std::put_time(tm, fmt.c_str()); + return ss.str(); +} + +std::string time2strlocal(int64_t time_us,const std::string &fmt="%Y-%m-%d %H:%M:%S"){ + stringstream ss; + time_t t = time_us / SEC; + auto tm = std::localtime(&t); + ss << std::put_time(tm, fmt.c_str()); + return ss.str(); +} + + +// 微秒数转字符串,不考虑时区 +int64_t str2time(const std::string& s, const std::string& fmt="%Y-%m-%d %H:%M:%S") +{ + stringstream ss; + ss << s; + struct tm tm; + ss >> std::get_time(&tm, fmt.c_str()); + return (int64_t)mktime(&tm) * SEC + HOUR * 8; // 这里 + 8 HOURs是因为mktime内部考虑了时区,我期望有一个gmmktime函数,但是标准库似乎并没有 +} + +void time_add(){ + using std::chrono::system_clock; + std::chrono::duration > one_day (1); + system_clock::time_point today = system_clock::now(); + system_clock::time_point tomorrow = today + one_day; + std::time_t tt; + + tt = system_clock::to_time_t ( today ); + std::cout << "today is: " << ctime(&tt); + + tt = system_clock::to_time_t ( tomorrow ); + std::cout << "tomorrow will be: " << ctime(&tt); +} \ No newline at end of file diff --git a/general/src/function/glotime.h b/general/src/function/glotime.h new file mode 100644 index 0000000..eb6a658 --- /dev/null +++ b/general/src/function/glotime.h @@ -0,0 +1,41 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +constexpr int64_t SEC = 1000000; +constexpr int64_t MIN = SEC * 60; +constexpr int64_t HOUR = MIN * 60; +constexpr int64_t DAY = HOUR * 24; + +// 返回当前时间作为 格林威治(GMT)时间 距离 GMT时间 1970-1-1 00:00:00 的微秒数 + +int64_t get_time(); +int64_t get_time_us_greenwich(); +// 返回当前时间作为 本地(北京)时间 距离 GMT时间 1970-1-1 00:00:00 的微秒数,等于get_gmtime_us加8小时 +int64_t get_localtime_us(); +// 格林威治时间的微秒数格式化成本地时间字符串 +string gmtime2localstr(int64_t time_us, const string& fmt="%Y-%m-%d %H:%M:%S"); +// 本地时间字符串解析成格林威治时间的微秒数 +int64_t localstr2gmtime(const std::string& s, const std::string& fmt="%Y-%m-%d %H:%M:%S"); + +// 字符串转微秒数,不考虑时区 +std::string time2strgm(int64_t time_us, const std::string& fmt="%Y-%m-%d %H:%M:%S"); +std::string time2strlocal(int64_t time_us,const std::string &fmt="%Y-%m-%d %H:%M:%S"); + +// 微秒数转字符串,不考虑时区 +int64_t str2time(const std::string& s, const std::string& fmt="%Y-%m-%d %H:%M:%S"); + +void time_add(); \ No newline at end of file diff --git a/test/src/chrono/CMakeLists.txt b/test/src/chrono/CMakeLists.txt new file mode 100644 index 0000000..5f9b520 --- /dev/null +++ b/test/src/chrono/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.12) +project(cpp11) +add_definitions(-std=c++11) + +message("current dir" ${CMAKE_CURRENT_SOURCE_DIR}) +# set(CMAKE_CXX_FLAGS "-fno-elide-constructors") +message(info ${SOURCE}) +link_directories("./third/lib") + + +add_executable(chrono_test test.cpp ) +include_directories("./third/include") + diff --git a/test/src/chrono/test.cpp b/test/src/chrono/test.cpp new file mode 100644 index 0000000..caf16cc --- /dev/null +++ b/test/src/chrono/test.cpp @@ -0,0 +1,119 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +constexpr int64_t SEC = 1000000; +constexpr int64_t MIN = SEC * 60; +constexpr int64_t HOUR = MIN * 60; +constexpr int64_t DAY = HOUR * 24; + +// 返回当前时间作为 格林威治(GMT)时间 距离 GMT时间 1970-1-1 00:00:00 的微秒数 + +int64_t get_time(){ + chrono::system_clock clock; + return chrono::duration_cast( + clock.now().time_since_epoch()).count(); +} + +int64_t get_time_us_greenwich() +{ + chrono::system_clock clock; + return chrono::duration_cast( + clock.now().time_since_epoch()).count(); +} + +// 返回当前时间作为 本地(北京)时间 距离 GMT时间 1970-1-1 00:00:00 的微秒数,等于get_gmtime_us加8小时 +int64_t get_localtime_us() +{ + return get_time_us_greenwich() + HOUR * 8; +} + +// 格林威治时间的微秒数格式化成本地时间字符串 +string gmtime2localstr(int64_t time_us, const string& fmt="%Y-%m-%d %H:%M:%S") +{ + stringstream ss; + time_t t = time_us / SEC; + auto tm = std::localtime(&t); + ss << std::put_time(tm, fmt.c_str()); + return ss.str(); +} + +// 本地时间字符串解析成格林威治时间的微秒数 +int64_t localstr2gmtime(const std::string& s, const std::string& fmt="%Y-%m-%d %H:%M:%S") +{ + stringstream ss; + ss << s; + struct tm tm; + ss >> std::get_time(&tm, fmt.c_str()); + return (int64_t)mktime(&tm) * SEC; +} + +// 字符串转微秒数,不考虑时区 +std::string time2strgm(int64_t time_us, const std::string& fmt="%Y-%m-%d %H:%M:%S") +{ + stringstream ss; + time_t t = time_us / SEC; + auto tm = std::gmtime(&t); + ss << std::put_time(tm, fmt.c_str()); + return ss.str(); +} + +std::string time2strlocal(int64_t time_us,const std::string &fmt="%Y-%m-%d %H:%M:%S"){ + stringstream ss; + time_t t = time_us / SEC; + auto tm = std::localtime(&t); + ss << std::put_time(tm, fmt.c_str()); + return ss.str(); +} + + +// 微秒数转字符串,不考虑时区 +int64_t str2time(const std::string& s, const std::string& fmt="%Y-%m-%d %H:%M:%S") +{ + stringstream ss; + ss << s; + struct tm tm; + ss >> std::get_time(&tm, fmt.c_str()); + return (int64_t)mktime(&tm) * SEC + HOUR * 8; // 这里 + 8 HOURs是因为mktime内部考虑了时区,我期望有一个gmmktime函数,但是标准库似乎并没有 +} + +void time_add(){ + using std::chrono::system_clock; + std::chrono::duration > one_day (1); + system_clock::time_point today = system_clock::now(); + system_clock::time_point tomorrow = today + one_day; + std::time_t tt; + + tt = system_clock::to_time_t ( today ); + std::cout << "today is: " << ctime(&tt); + + tt = system_clock::to_time_t ( tomorrow ); + std::cout << "tomorrow will be: " << ctime(&tt); +} + +void time_format(){ + +} + +int main() +{ + std::cout<<"greenwill time: "<