#ifndef _Utils_H_ #define _Utils_H_ #include #include #include #include #include #include #include //#include "Crypto.h" using namespace std; class Utils { public: static string toStr(int v); static string toStr(float v, int precision = 2); static string toStr(double v, int precision = 2); static int toInt(const std::string& str); static float toFloat(const string& str); static double toDouble(const string& str); static int64_t hexbinToInt(const string& str, bool swap = false); static int hexstrToInt(const string& str); static string from_hex_text(string s); static string to_hex_text(string s, string d = " "); template static void append_bytes(vector& dest, const T& src, int len) { int start = static_cast(dest.size()); dest.resize(start + len); memcpy_s(&dest[start], len, &src, len); } static int64_t timeNow(); static int64_t timeNowMS(); static int64_t timeNowDate(); static string timeNowStr(std::string fmt = "%Y-%m-%d %H:%M:%S"); static string timeNowStrMS(std::string fmt = "%Y-%m-%d %H:%M:%S"); static string timeStr(int64_t ts, std::string fmt = "%Y-%m-%d %H:%M:%S"); static int64_t time(string dt="", int zone = 0); static string time_to_string(int64_t dt, std::string fmt="%Y-%m-%d %H:%M:%S"); static bool time_string_to_tm(string dt, std::tm& t); std::string duration_str(int64_t t) { auto h = t / 3600; auto m = (t % 3600) / 60; auto s = (t % 3600) % 60; std::string str = std::to_string(s) + "秒"; if (m > 0 || h > 0) { str = std::to_string(m) + "分" + str; } if (h > 0) { str = std::to_string(h) + "小时" + str; } return str; } static unsigned short crc16(unsigned char* data, unsigned int len); static string Utils::gbkToUtf8(string s); static string Utils::utf8ToGbk(string s); static void sleep_ms(int ms); static int random(int min, int max); static void split(string buf, string c, vector& res); }; class TimeTick { public: int64_t tickMS_ = 0; TimeTick() { tickMS_ = Utils::timeNowMS(); } bool elapse(int64_t ms, bool reset = true) { auto tick_now = Utils::timeNowMS(); bool res = tick_now - tickMS_ > ms; if (res && reset) { tickMS_ = tick_now; } return res; } void reset() { tickMS_ = Utils::timeNowMS(); } }; #endif // !!! _Utils_H_