2025-05-19 09:54:33 +08:00
|
|
|
|
#ifndef _Utils_H_
|
|
|
|
|
|
#define _Utils_H_
|
|
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
|
|
//#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<typename T>
|
|
|
|
|
|
static void append_bytes(vector<unsigned char>& dest, const T& src, int len)
|
|
|
|
|
|
{
|
|
|
|
|
|
int start = static_cast<int>(dest.size());
|
|
|
|
|
|
dest.resize(start + len);
|
|
|
|
|
|
memcpy_s(&dest[start], len, &src, len);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-08 19:34:12 +08:00
|
|
|
|
// 获取当前时间的时间戳(秒)
|
2025-09-04 19:31:04 +08:00
|
|
|
|
static int64_t time(std::string s="");
|
2025-08-20 19:00:22 +08:00
|
|
|
|
// 获取当前时间的格式字符串
|
2025-09-04 19:31:04 +08:00
|
|
|
|
static string timeStr(int64_t ts=0, std::string fmt = "%Y-%m-%d %H:%M:%S");
|
2025-09-08 19:34:12 +08:00
|
|
|
|
// 获取当前日期的时间戳(秒)
|
2025-08-20 19:00:22 +08:00
|
|
|
|
static int64_t date();
|
|
|
|
|
|
// 获取当前日期的格式字符串
|
2025-09-04 19:31:04 +08:00
|
|
|
|
static string dateStr(int64_t ts = 0, std::string fmt = "%Y-%m-%d %H:%M:%S");
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
static string timeStrMS(std::string fmt = "%Y-%m-%d %H:%M:%S");
|
|
|
|
|
|
//static string timeStr(int64_t ts, std::string fmt = "%Y-%m-%d %H:%M:%S");
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
//static int64_t time(string dt="", int zone = 0);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
//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);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2025-09-10 20:10:51 +08:00
|
|
|
|
static string gbkToUtf8(string s);
|
|
|
|
|
|
static string utf8ToGbk(string s);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
|
|
|
|
|
static void sleep_ms(int ms);
|
|
|
|
|
|
|
|
|
|
|
|
static int random(int min, int max);
|
|
|
|
|
|
|
|
|
|
|
|
static void split(string buf, string c, vector<string>& res);
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
|
|
|
|
|
static std::string readFile(std::string filename);
|
|
|
|
|
|
|
|
|
|
|
|
static std::string toHexStr(int64_t val);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TimeTick
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
int64_t tickMS_ = 0;
|
|
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
TimeTick(int t=0)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
if (t !=0) { tickMS_ = Utils::time(); }
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool elapse(int64_t ms, bool reset = true)
|
|
|
|
|
|
{
|
2025-08-20 19:00:22 +08:00
|
|
|
|
auto tick_now = Utils::time();
|
2025-05-19 09:54:33 +08:00
|
|
|
|
bool res = tick_now - tickMS_ > ms;
|
|
|
|
|
|
if (res && reset)
|
|
|
|
|
|
{
|
|
|
|
|
|
tickMS_ = tick_now;
|
|
|
|
|
|
}
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
|
|
{
|
2025-08-20 19:00:22 +08:00
|
|
|
|
tickMS_ = Utils::time();
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // !!! _Utils_H_
|