#pragma once #include #include #include #ifdef __cplusplus extern "C" { namespace baseutils { #endif extern int64_t cg_gmtOff; extern void gmttime(uint64_t p_time, struct tm* p_dest); extern time_t getSystemUptime(); extern time_t getSystemStartTime(); extern time_t getProcessStartTime(pid_t p_pid=0); extern char const* time2str(char* po_dest, size_t p_destLen, uint64_t p_time, char const* p_format=nullptr); /*-------------------------------------------------------------------------------------------------- // _ ____ _ ___ __ __ // __ _ ___| |_ / ___|_ __ ___ | |_ / _ \ / _|/ _| // / _` |/ _ \ __| | _| '_ ` _ \| __| | | | |_| |_ // | (_| | __/ |_| |_| | | | | | | |_| |_| | _| _| // \__, |\___|\__|\____|_| |_| |_|\__|\___/|_| |_| // |___/ --------------------------------------------------------------------------------------------------*/ inline int64_t getGmtOff() { time_t t = time(nullptr); struct tm lt; localtime_r(&t, <); return((int32_t)lt.tm_gmtoff); } /*-------------------------------------------------------------------------------------------------- // // _ __ _____ __ // | '_ \ / _ \ \ /\ / / // | | | | (_) \ V V / // |_| |_|\___/ \_/\_/ // --------------------------------------------------------------------------------------------------*/ // Return the current local time in nanoseconds. inline uint64_t now() { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return((uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec); } /*-------------------------------------------------------------------------------------------------- // _ // __ _ _ __ ___ | |_ _ __ _____ __ // / _` | '_ ` _ \| __| '_ \ / _ \ \ /\ / / // | (_| | | | | | | |_| | | | (_) \ V V / // \__, |_| |_| |_|\__|_| |_|\___/ \_/\_/ // |___/ --------------------------------------------------------------------------------------------------*/ inline uint64_t gmtnow() { return(static_cast(static_cast(now()) + cg_gmtOff)); } #ifdef __cplusplus } } # include namespace baseutils { extern std::string time2str(uint64_t p_time, char const* p_format=nullptr); } #endif