C++Utils
来源:互联网 发布:新的淘宝店铺如何推广 编辑:程序博客网 时间:2024/05/22 05:10
头文件Utils.h
#ifndef __UTILS__#define __UTILS__//timeLONG SystemTimeToTime_t(SYSTEMTIME& st);void Time_tToSystemTime(time_t t, LPSYSTEMTIME pst);void TimeUtcToLocal(LPSYSTEMTIME lpUtcTime, LPSYSTEMTIME lpSpecificLocalTime);void TimeLocalToUtc(LPSYSTEMTIME lpSpecificLocalTime, LPSYSTEMTIME lpUtcTime);time_t StringtimeToTime_t(std::string strTime);std::string StringtimeLocalToUTC(string strLocaltime);std::string SystemtimeLocalToUTC(SYSTEMTIME& sysTime);std::string StringtimeUTCToLocal(string strLocaltime);std::string SystimeToString(SYSTEMTIME& sysTime);//system time >>> 20170406T18:55:00//char setvoid TurnToUtf8(string& strTurned);BOOL IsTextUTF8(char* str, ULONG length);//compare functionbool IsLessThan_String(std::string strParam1, std::string strParam2);bool CompareWithString(std::string strParam1, std::string strParam2);bool CompareWithStringNoCase(const char* szParam1, const char* szParam2);bool IsContainString(const char* strSource, const char* strTest);//std::string StringFormat_(const char* format, ...);#endif
源文件 Utils.cpp
#include "stdafx.h"#include "Utils.h"LONG SystemTimeToTime_t(SYSTEMTIME& st){ FILETIME ft; SystemTimeToFileTime(&st, &ft); ULARGE_INTEGER ui; ui.LowPart = ft.dwLowDateTime; ui.HighPart = ft.dwHighDateTime; LONG tmm = (long)((LONGLONG)(ui.QuadPart - 116444736000000000) / 10000000); return tmm;}std::string LocaltimeToISO8601(time_t time){ char buf[32]; strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", gmtime(&time)); std::string iso8601_time_str(buf); return iso8601_time_str;}std::string ISO8601ToLocaltime(const std::string& time){ struct std::tm time_struct; std::istringstream ss(time); ss >> std::get_time(&time_struct, "%Y-%m-%dT%H:%M:%S"); std::time_t time_unix = _mkgmtime(&time_struct); if (time_unix == -1) { return ""; } char current_time[32]; strftime(current_time, sizeof(current_time), "%Y-%m-%d %H:%M:%S", localtime(&time_unix)); std::string local_time_str(current_time); return local_time_str;}void Time_tToSystemTime(time_t t, LPSYSTEMTIME pst){ FILETIME ft; LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000; ft.dwLowDateTime = (DWORD)ll; ft.dwHighDateTime = (DWORD)(ll >> 32); FileTimeToSystemTime(&ft, pst);}void TimeUtcToLocal(LPSYSTEMTIME lpUtcTime, LPSYSTEMTIME lpSpecificLocalTime){ FILETIME ftLocal = { 0 }, ftUtc = { 0 }; SystemTimeToFileTime(lpUtcTime, &ftUtc); FileTimeToLocalFileTime(&ftUtc, &ftLocal); FileTimeToSystemTime(&ftLocal, lpSpecificLocalTime);}void TimeLocalToUtc(LPSYSTEMTIME lpSpecificLocalTime, LPSYSTEMTIME lpUtcTime){ FILETIME ftLocal = { 0 }, ftUtc = { 0 }; SystemTimeToFileTime(lpSpecificLocalTime, &ftLocal); LocalFileTimeToFileTime(&ftLocal, &ftUtc); FileTimeToSystemTime(&ftUtc, lpUtcTime);}time_t StringtimeToTime_t(std::string strTime){ struct std::tm time_struct; std::istringstream ss(strTime); ss >> std::get_time(&time_struct, "%Y-%m-%dT%H:%M:%S"); if (ss.fail()) return 0; return _mkgmtime(&time_struct);}std::string StringtimeLocalToUTC(string strLocaltime){ SYSTEMTIME sysTime; sscanf(strLocaltime.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d", &sysTime.wYear, &sysTime.wMonth, &sysTime.wDay, &sysTime.wHour, &sysTime.wMinute, &sysTime.wSecond); TimeLocalToUtc(&sysTime, &sysTime); std::ostringstream os; os << setw(4) << setfill('0') << sysTime.wYear << "-" << setw(2) << setfill('0') << sysTime.wMonth << "-" << setw(2) << setfill('0') << sysTime.wDay << "T" << setw(2) << setfill('0') << sysTime.wHour << ":" << setw(2) << setfill('0') << sysTime.wMinute << ":" << setw(2) << setfill('0') << sysTime.wSecond; std::string strUtctime = os.str(); os.clear(); return std::move(strUtctime);}std::string SystemtimeLocalToUTC(SYSTEMTIME& sysTime){ TimeLocalToUtc(&sysTime, &sysTime); std::ostringstream os; os << setw(4) << setfill('0') << sysTime.wYear << "-" << setw(2) << setfill('0') << sysTime.wMonth << "-" << setw(2) << setfill('0') << sysTime.wDay << "T" << setw(2) << setfill('0') << sysTime.wHour << ":" << setw(2) << setfill('0') << sysTime.wMinute << ":" << setw(2) << setfill('0') << sysTime.wSecond; std::string strUtctime = os.str(); os.clear(); return std::move(strUtctime);}std::string StringtimeUTCToLocal(string strLocaltime){ SYSTEMTIME sysTime; sscanf(strLocaltime.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d", &sysTime.wYear, &sysTime.wMonth, &sysTime.wDay, &sysTime.wHour, &sysTime.wMinute, &sysTime.wSecond); TimeUtcToLocal(&sysTime, &sysTime); std::ostringstream os; os << setw(4) << setfill('0') << sysTime.wYear << "-" << setw(2) << setfill('0') << sysTime.wMonth << "-" << setw(2) << setfill('0') << sysTime.wDay << "T" << setw(2) << setfill('0') << sysTime.wHour << ":" << setw(2) << setfill('0') << sysTime.wMinute << ":" << setw(2) << setfill('0') << sysTime.wSecond; std::string strUtctime = os.str(); os.clear(); return std::move(strUtctime);}std::string SystimeToString(SYSTEMTIME& sysTime){ std::ostringstream os; os << setw(4) << setfill('0') << sysTime.wYear << "-" << setw(2) << setfill('0') << sysTime.wMonth << "-" << setw(2) << setfill('0') << sysTime.wDay << "T" << setw(2) << setfill('0') << sysTime.wHour << ":" << setw(2) << setfill('0') << sysTime.wMinute << ":" << setw(2) << setfill('0') << sysTime.wSecond; std::string strUtctime = os.str(); os.clear(); return std::move(strUtctime);}void TurnToUtf8(string& strTurned){ if (IsTextUTF8((char*)strTurned.c_str(), strlen(strTurned.c_str()))) { WCHAR *strSrc = NULL; //CHAR *szRes = NULL; int i = MultiByteToWideChar(CP_UTF8, 0, strTurned.c_str(), -1, NULL, 0); strSrc = new WCHAR[i + 1]; MultiByteToWideChar(CP_UTF8, 0, strTurned.c_str(), -1, strSrc, i); i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL); //szRes = new CHAR[i + 1]; WideCharToMultiByte(CP_ACP, 0, strSrc, -1, (LPSTR)strTurned.c_str(), i, NULL, NULL); if (strSrc != NULL) { delete[] strSrc; strSrc = NULL; } //strCamera }}BOOL IsTextUTF8(char* str, ULONG length){ ULONG i; DWORD nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节 UCHAR chr; BOOL bAllAscii = TRUE; //如果全部都是ASCII, 说明不是UTF-8 for (i = 0; i < length; i++) { chr = *(str + i); if ((chr & 0x80) != 0) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx bAllAscii = FALSE; if (nBytes == 0) //如果不是ASCII码,应该是多字节符,计算字节数 { if (chr >= 0x80) { if (chr >= 0xFC && chr <= 0xFD) nBytes = 6; else if (chr >= 0xF8) nBytes = 5; else if (chr >= 0xF0) nBytes = 4; else if (chr >= 0xE0) nBytes = 3; else if (chr >= 0xC0) nBytes = 2; else { return FALSE; } nBytes--; } } else //多字节符的非首字节,应为 10xxxxxx { if ((chr & 0xC0) != 0x80) { return FALSE; } nBytes--; } } if (nBytes > 0) { return FALSE; } if (bAllAscii) //如果全部都是ASCII, 说明不是UTF-8 { return FALSE; } return TRUE;}bool CompareWithString(std::string strParam1, std::string strParam2){ if (strParam1 == strParam2) { return true; } else { return false; }}bool IsLessThan_String(std::string strParam1, std::string strParam2){ if (strParam1 <= strParam2) { return true; } else { return false; }}bool CompareWithStringNoCase(const char* szParam1, const char* szParam2){ if (_stricmp(szParam1, szParam2) == 0) { return true; } else { return false; }}bool IsContainString(const char* strSource, const char* strTest){ if (strstr(strSource, strTest) != nullptr) { return true; } else { return false; }}std::string StringFormat_(const char* format, ...){ std::string strTemp; if (format) { va_list vl; va_start(vl); size_t length = ::_vscprintf(format, vl) + 1; vector<char> vec(length, '\0'); int iWrite;#ifdef __STDC_WANT_SECURE_LIB__ iWrite = _vsnprintf_s(&vec[0], length, length, format, vl);#else iWrite = vsnprintf(&vec[0], length, format, vl);#endif if (iWrite > 0) { strTemp = &vec[0]; } va_end(vl); } return std::move(strTemp);}
0 0
- C++Utils
- UTILS
- Utils
- utils
- utils
- Utils
- Utils
- Utils
- utils
- Utils
- Utils
- Utils
- UTILS
- Utils
- Utils
- Utils
- Utils
- utils
- 映射关系接口Map
- Error Curves(三分)uvalive 5009 求下凸 (最大值的最小值)
- JDBC小结
- Java 8中的Map类
- 让边绑定更加清晰:网络可视化中融合绘制方式的探索(Towards Unambiguous Edge Bundling: Investigating Confluent Drawings for Net
- C++Utils
- LinkedHashMap类-和插入顺序强相关
- 携程测试笔试题
- 能够读写属性文件的集合类-Properties
- 判断一个wifi热点是否连接成功
- orcale 常用功能语句
- Java 8中的HashMap类和Hashtable类
- [资源] 优秀博客和常用插件
- Invocation of init method failed; nested exception is java.lang.NoSuchMethod