#include <codecvt> #include <boost/filesystem.hpp> #include <boost/filesystem/path.hpp> #include <boost/date_time/posix_time/posix_time.hpp> std::string time_to_string(const boost::posix_time::ptime& time) { boost::posix_time::time_facet* facet = new boost::posix_time::time_facet(); facet->format("%Y_%m_%d %H_%M_%S"); std::stringstream stream; stream.imbue(std::locale(std::locale::classic(), facet)); stream << time; return stream.str(); } boost::filesystem::path executable_path() { TCHAR path[MAX_PATH] = { 0 }; GetModuleFileName(GetModuleHandle(NULL), path, MAX_PATH); return boost::filesystem::path(path).remove_filename(); } std::string convertToUtf8(const std::wstring& str) { std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter; return converter.to_bytes(str); } std::wstring convertToUtf16(const std::string& str) { std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter; return converter.from_bytes(str); } bool isNum(std::string strInfo) { try { unsigned long data = boost::lexical_cast<UINT64>(strInfo); } catch (boost::exception& e) { return false; } return true; } std::string GetUUID() { boost::uuids::uuid uuid = boost::uuids::random_generator()(); return boost::uuids::to_string(uuid); } UINT64 GetCurTime() { return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); } std::string GetLocalFormatTime() { char buf[512] = { 0 }; using namespace std::chrono; const time_t& curTime = system_clock::to_time_t(system_clock::now()); tm* localTime = localtime(&curTime); sprintf_s(buf, "%d-%d-%d %d:%d:%d", localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday, localTime->tm_hour, localTime->tm_min, localTime->tm_sec); return buf; } std::wstring Utf8ToUnicode(const std::string& utf8) { LPCCH ptr = utf8.c_str(); int size = MultiByteToWideChar(CP_UTF8, 0, ptr, -1, NULL, NULL); std::wstring wstrRet(size, 0); int len = MultiByteToWideChar(CP_UTF8, 0, ptr, -1, (LPWSTR)wstrRet.c_str(), size); return wstrRet; } std::string Utf8ToAnsi(const std::string& utf8) { std::wstring wstrTemp = Utf8ToUnicode(utf8); LPCWCH ptr = wstrTemp.c_str(); int size = WideCharToMultiByte(CP_ACP, 0, ptr, -1, NULL, 0, NULL, NULL); std::string strRet(size-1, 0); int len = WideCharToMultiByte(CP_ACP, 0, ptr, -1, (LPSTR)strRet.c_str(), size, NULL, NULL); return strRet; } void log(const std::string& str) { std::ofstream file((executable_path() / "error.log").c_str(), std::ios::app); file << time_to_string(boost::posix_time::second_clock::universal_time()) << ' ' << str << std::endl; } std::string ws2s(const std::wstring& str) { int tm1 = GetCurTime(); char* pElementText; int iTextLen; // wide char to multi char iTextLen = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL); pElementText = new char[iTextLen + 1]; memset((void*)pElementText, 0, sizeof(char)* (iTextLen + 1)); ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL); std::string strText; strText = pElementText; delete[] pElementText; int tm2 = GetCurTime() - tm1; return strText; }