/* * @Author: yangzilong * @Date: 2021-12-17 17:45:43 * @Last Modified by: yangzilong * @Email: yangzilong@objecteye.com * @Description: */ #ifndef __OLFIELD_UTILS_TIMER_HPP__ #define __OLFIELD_UTILS_TIMER_HPP__ #include #include #include #include #include #include #include using namespace std; namespace helpers { namespace timer { template static inline long long get_timestamp() { auto now = std::chrono::system_clock::now(); return std::chrono::duration_cast(now.time_since_epoch()).count(); } static long long get_cur_time_ms(){ chrono::time_point tpMicro = chrono::time_point_cast(chrono::system_clock::now()); return tpMicro.time_since_epoch().count(); } static std::string get_date(bool with_time = false) { std::chrono::system_clock::time_point t = std::chrono::system_clock::now(); auto as_time_t = std::chrono::system_clock::to_time_t(t); struct tm tm; #if defined(WIN32) || defined(_WINDLL) localtime_s(&tm, &as_time_t); // win api thread safe, but std::localtime can't thread safe. #else localtime_r(&as_time_t, &tm); // linux api thread safe. #endif char buf[256]{0}; if (with_time) snprintf(buf, sizeof(buf), "%04d%02d%02d_%02d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); else snprintf(buf, sizeof(buf), "%04d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); return std::string(buf); } } // namespace timer } // namespace helpers #endif // __OLFIELD_UTILS_TIMER_HPP__