#pragma pack(push,1) /* 在这中间定义的结构体,已单字节对齐 */ #pragma pack(pop)
/************************************************************************ 函数功能: 将时间time_t转化为YYYY-MM-DD hh:mm:ss 输入参数: time_t tTime -- 给定时间的标准time_t格式 输出参数: string& strTime -- 字符串 返回值 : 说明 : ************************************************************************/ void time2string(time_t tTime, string& strTime) { struct tm tmTime; localtime_s(&tmTime, &tTime); char cTime[100] = {'\n'}; _snprintf_s(cTime, sizeof(cTime)-1, "%04d-%02d-%02d %02d:%02d:%02d", tmTime.tm_year + 1900, tmTime.tm_mon + 1, tmTime.tm_mday, tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec); strTime = cTime; }
/* 用于tcp传输时间结构体 */ typedef struct tagTimePack { unsigned short nYear; unsigned char nMonth; unsigned char nDay; unsigned char nSer; unsigned char nHour; unsigned char nMinute; unsigned char nSecond; std::string toString() { char buff[32] = {'\0'}; _snprintf_s(buff, sizeof(buff)-1, "%04d-%02d-%02d %02d:%02d:%02d", nYear, nMonth, nDay, nHour, nMinute, nSecond); std::string str(buff); return str; } } TimePack, * LPTimePack;
/************************************************************************ 函数功能: 将时间的time_t的表示,转换为自定义的TimePack格式 输入参数: time_t tTime -- 给定时间的标准time_t格式 输出参数: const TimePack& timePack -- 给定时间的自定义TimePack格式 返回值 : 说明 : ************************************************************************/ void DPC::time2pack(time_t tTime, TimePack& timePack) { struct tm tmTime; localtime_s(&tmTime, &tTime); timePack.nYear = tmTime.tm_year + 1900; timePack.nMonth = tmTime.tm_mon + 1; timePack.nDay = tmTime.tm_mday; timePack.nHour = tmTime.tm_hour; timePack.nMinute = tmTime.tm_min; timePack.nSecond = tmTime.tm_sec; };
/************************************************************************ 函数功能: 将时间的自定义的TimePack表示,转换为标准的time_t格式 输入参数: const TimePack& timePack -- 给定时间的自定义TimePack格式 输出参数: time_t& tTime -- 时间的标准time_t格式 返回值 : 说明 : ************************************************************************/ void DPC::pack2time(const TimePack& timePack, time_t& tTime) { tm mytm; mytm.tm_year = timePack.nYear - 1900; mytm.tm_mon = timePack.nMonth - 1; mytm.tm_mday = timePack.nDay; mytm.tm_hour = timePack.nHour; mytm.tm_min = timePack.nMinute; mytm.tm_sec = timePack.nSecond; // 如果在循环再频繁被调用,可考虑使用 dcs_mktime tTime = mktime(&mytm); };
//************************************************************************* // 函数名称: OleTimeToTimet // 返 回 值: time_t // 参 数: const COleDateTime & oleTime // 函数说明: COleDatetime转time_t //************************************************************************* time_t OleTimeToTimet(const COleDateTime& oleTime) { if (COleDateTime::valid != oleTime.GetStatus()){ return 0; } SYSTEMTIME systime; oleTime.GetAsSystemTime(systime); struct tm atm; atm.tm_sec = systime.wSecond; atm.tm_min = systime.wMinute; atm.tm_hour = systime.wHour; atm.tm_mday = systime.wDay; atm.tm_mon = systime.wMonth - 1; atm.tm_year = systime.wYear - 1900; atm.tm_isdst = -1; return mktime(&atm); }
/************************************************************************ 函数功能: 将时间的time_t的表示,转换为string格式 输入参数: time_t tTime -- 给定时间的标准time_t格式 输出参数: string& strTime -- 返回值 : 说明 : 格式:YYYYMMDDHHMMSSmmm ************************************************************************/ void DPC::get_local_time(string& strTime) { SYSTEMTIME st; GetLocalTime(&st); char cTime[100] = { '\0' }; _snprintf_s(cTime, sizeof(cTime) - 1, "%04d%02d%02d%02d%02d%02d%03d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); strTime = cTime; }