126 lines
3.5 KiB
C++
126 lines
3.5 KiB
C++
#include <iostream>
|
|
#include <chrono>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <ctime>
|
|
#include "common/inc/json.hpp"
|
|
#include "datacenter/bizService/F_Tool.h"
|
|
typedef unsigned char BYTE;
|
|
void Tool::hexstr_to_str(void *dst, const void *src, int src_len)
|
|
{
|
|
int i;
|
|
BYTE low, high, ch;
|
|
BYTE *dst_ptr = (BYTE *) dst;
|
|
const BYTE *src_ptr = (const BYTE *) src;
|
|
|
|
for (i = 0; i < src_len / 2; i++)
|
|
{
|
|
ch = *src_ptr++;
|
|
if (ch >= '0' && ch <= '9')
|
|
high = ch - '0';
|
|
else if (ch >= 'A' && ch <= 'F')
|
|
high = ch - 'A' + 10;
|
|
else if (ch >= 'a' && ch <= 'f')
|
|
high = ch - 'a' + 10;
|
|
else
|
|
break;
|
|
ch = *src_ptr++;
|
|
if (ch >= '0' && ch <= '9')
|
|
low = ch - '0';
|
|
else if (ch >= 'A' && ch <= 'F')
|
|
low = ch - 'A' + 10;
|
|
else if (ch >= 'a' && ch <= 'f')
|
|
low = ch - 'a' + 10;
|
|
else
|
|
break;
|
|
*dst_ptr++ = high * 16 + low; //hex
|
|
}
|
|
}
|
|
|
|
void Tool::str_to_hexstr(void *dst, const void *src, int src_len)
|
|
{
|
|
int i;
|
|
BYTE low, high, ch;
|
|
char *dst_ptr = (char *) dst;
|
|
const char *src_ptr = (const char *) src;
|
|
const char *str = "0123456789abcdef";
|
|
|
|
for (i = 0; i < src_len; i++)
|
|
{
|
|
ch = *src_ptr++;
|
|
high = (ch >> 4) & 0x0f;
|
|
low = ch & 0x0f;
|
|
*dst_ptr++ = str[high];
|
|
*dst_ptr++ = str[low];
|
|
}
|
|
}
|
|
int Tool::encode_blob(void *dst, int max_len, const void *src, int src_len)
|
|
{
|
|
int dst_len = 0;
|
|
|
|
// may be changed to base64 encode
|
|
if (max_len >= src_len * 2 + 1)
|
|
{
|
|
dst_len = src_len * 2;
|
|
str_to_hexstr(dst, src, src_len);
|
|
}
|
|
((BYTE *) dst)[dst_len] = 0x00;
|
|
return dst_len;
|
|
}
|
|
|
|
int Tool::decode_blob(void *dst, int max_len, const void *src)
|
|
{
|
|
// may be changed to base64 decode
|
|
int src_len, dst_len = 0;
|
|
|
|
src_len = strlen((char *) src);
|
|
std::cout << "decode_blob src len "<< src_len<<std::endl;
|
|
if (max_len >= src_len / 2)
|
|
{
|
|
dst_len = src_len / 2;
|
|
hexstr_to_str(dst, src, src_len);
|
|
}
|
|
return dst_len;
|
|
}
|
|
void Tool::topic_split(const std::string& str, char delimiter,std::vector<std::string>& topic) {
|
|
std::string token; // 临时存储每个子字符串
|
|
std::istringstream tokenStream(str); // 使用字符串流处理输入字符串
|
|
// 按分隔符逐个读取子字符串
|
|
while (std::getline(tokenStream, token, delimiter)) {
|
|
topic.push_back(token); // 将子字符串添加到结果中
|
|
}
|
|
|
|
}
|
|
std::string Tool::now_to_string() {
|
|
// 获取系统时钟时间点
|
|
auto time_now = std::chrono::system_clock::now();
|
|
|
|
// 将时间点转成time_t类型的值 (秒)
|
|
std::time_t tt = std::chrono::system_clock::to_time_t(time_now);
|
|
|
|
// 转换成tm结构体形式
|
|
struct tm* ptm = gmtime(&tt); // 或者 localtime(&tt) 如果想要本地时间
|
|
ptm->tm_hour += 8; //utc -> cst
|
|
// 计算出毫秒部分
|
|
long milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
time_now.time_since_epoch()
|
|
).count() % 1000;
|
|
|
|
char buffer[40];
|
|
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", ptm);
|
|
|
|
// 添加上毫秒和时区信息
|
|
sprintf(buffer + strlen(buffer), ".%03ld+0800", milliseconds);
|
|
|
|
return std::string(buffer);
|
|
}
|
|
time_t Tool::string_to_now(std::string stamp) {
|
|
struct tm tm={};
|
|
std::istringstream ss(stamp);
|
|
ss >> std::get_time(&tm,"%Y-%m-%dT%H:%M:%S");
|
|
if(ss.fail()){
|
|
return -1;
|
|
}
|
|
return mktime(&tm);
|
|
} |