调整时间获取方法

This commit is contained in:
gb 2022-11-08 11:21:50 +08:00
parent 4bef595693
commit fb52bb4e99
3 changed files with 54 additions and 4 deletions

View File

@ -372,11 +372,9 @@ extern "C"
} }
std::string current_time(void) std::string current_time(void)
{ {
char buf[40]; char buf[40] = { 0 };
time_t t = time(NULL);
tm* l = localtime(&t);
sprintf(buf, "%d-%02d-%02d %02d:%02d:%02d", l->tm_year + 1900, l->tm_mon + 1, l->tm_mday, l->tm_hour, l->tm_min, l->tm_sec); get_current_time(buf);
return buf; return buf;
} }

View File

@ -382,6 +382,48 @@ extern "C"
return g_unk_statu; return g_unk_statu;
} }
void get_current_time(char* tmbuf, struct tm* t)
{
#if defined(WIN32) || defined(_WIN64)
static long bias = -1;
static bool as_start = true;
if (bias == -1)
{
_get_timezone(&bias);
as_start = hg_log::ini_get("time", "adjust") != "no";
}
#endif
time_t now = time(nullptr);
struct tm* l = localtime(&now);
#if defined(WIN32) || defined(_WIN64)
long after = 0;
if (as_start && _get_timezone(&after) == 0 &&
after != bias)
{
now += bias;
l = localtime(&now);
}
#endif
if (t)
*t = *l;
if (tmbuf)
sprintf(tmbuf, "%04d-%02d-%02d %02d:%02d:%02d", l->tm_year + 1900, l->tm_mon + 1, l->tm_mday, l->tm_hour, l->tm_min, l->tm_sec);
}
void get_current_time_w(wchar_t* tmbuf, struct tm* t)
{
struct tm tmp = { 0 }, * l = &tmp;
get_current_time(nullptr, l);
if (t)
*t = *l;
if (tmbuf)
swprintf(tmbuf, L"%04d-%02d-%02d %02d:%02d:%02d", l->tm_year + 1900, l->tm_mon + 1, l->tm_mday, l->tm_hour, l->tm_min, l->tm_sec);
}
} }

View File

@ -453,6 +453,16 @@ extern "C"{
// Function: 图像状态名称 // Function: 图像状态名称
const char* hg_scanner_image_statu_name(int img_statu); const char* hg_scanner_image_statu_name(int img_statu);
// Function: 获取当前时间字符串
//
// Parameters: tmbuf - 用于接收时间字符串不小于40个字符长度
//
// t - 用于接收原始的时间结构
//
// Return: void
void get_current_time(char* tmbuf, struct tm* t = nullptr);
void get_current_time_w(wchar_t* tmbuf, struct tm* t = nullptr);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif