#include "stdafx.h"
|
#include "ToolUnits.h"
|
#include <chrono>
|
|
|
CToolUnits::CToolUnits()
|
{
|
}
|
|
|
CToolUnits::~CToolUnits()
|
{
|
}
|
|
std::string CToolUnits::timeToString(ULONGLONG time)
|
{
|
ULONGLONG time1, time2;
|
time1 = time / 1000000000;
|
time2 = time % 1000000000;
|
|
char buffer1[256], buffer[128];
|
struct tm timeinfo;
|
time_t t = time_t(time1);
|
localtime_s(&timeinfo, &t);
|
strftime(buffer, 128, "%Y-%m-%d %H:%M:%S", &timeinfo);
|
sprintf_s(buffer1, 256, "%s.%lld", buffer, time2);
|
return std::string(buffer1);
|
}
|
|
std::string CToolUnits::timeToString2(ULONGLONG time)
|
{
|
ULONGLONG time1;
|
time1 = time / 1000;
|
|
char buffer[256];
|
struct tm timeinfo;
|
time_t t = time_t(time1);
|
localtime_s(&timeinfo, &t);
|
strftime(buffer, 128, "%Y-%m-%d %H:%M:%S", &timeinfo);
|
return std::string(buffer);
|
}
|
|
std::string CToolUnits::timeToString3(ULONGLONG time)
|
{
|
ULONGLONG time1;
|
int ms;
|
time1 = time / 1000;
|
ms = time % 1000;
|
|
char buffer1[256], buffer[128];
|
struct tm timeinfo;
|
time_t t = time_t(time1);
|
localtime_s(&timeinfo, &t);
|
strftime(buffer, 128, "%Y-%m-%d %H:%M:%S", &timeinfo);
|
sprintf_s(buffer1, 256, "%s.%03d", buffer, ms);
|
return std::string(buffer1);
|
}
|
|
ULONGLONG CToolUnits::stringToTime(const char* pszTime)
|
{
|
struct tm tm;
|
|
memset(&tm, 0, sizeof(tm));
|
sscanf_s(pszTime, "%d-%d-%d %d:%d:%d",
|
&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
|
&tm.tm_hour, &tm.tm_min, &tm.tm_sec);
|
tm.tm_year -= 1900;
|
tm.tm_mon--;
|
|
return mktime(&tm) * 1000;
|
}
|
|
ULONGLONG CToolUnits::getTimestamp()
|
{
|
auto now = std::chrono::system_clock::now();
|
auto duration_in_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
|
uint64_t timestamp = duration_in_milliseconds.count();
|
return timestamp;
|
}
|
|
void CToolUnits::createDir(const char* pszDir)
|
{
|
CString strDir = pszDir;
|
int lastIndex = 0;
|
int index = strDir.Find(_T("\\"), lastIndex);
|
while (index > 0) {
|
CString strTempDir = strDir.Left(index);
|
CreateDirectory(strTempDir, NULL);
|
|
lastIndex = index + 1;
|
index = strDir.Find(_T("\\"), lastIndex);
|
}
|
CreateDirectory(strDir, NULL);
|
}
|
|
CString& CToolUnits::floatToString1(float value, CString& strOut)
|
{
|
strOut.Format(_T("%.1f"), value);
|
return strOut;
|
}
|
|
CString& CToolUnits::floatToString3(float value, CString& strOut)
|
{
|
strOut.Format(_T("%.3f"), value);
|
return strOut;
|
}
|
|
|
BOOL CToolUnits::copyTextToClipboard(CWnd* pWnd, const CString& strText)
|
{
|
if (OpenClipboard(pWnd->GetSafeHwnd())) {
|
EmptyClipboard();
|
|
HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (strText.GetLength() + 1) * sizeof(TCHAR));
|
if (hglbCopy == NULL) {
|
CloseClipboard();
|
return FALSE;
|
}
|
|
LPTSTR lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
|
strcpy_s(lptstrCopy, strText.GetLength()+1, strText);
|
GlobalUnlock(hglbCopy);
|
SetClipboardData(CF_TEXT, hglbCopy);
|
CloseClipboard();
|
GlobalFree(hglbCopy);
|
return TRUE;
|
}
|
|
return FALSE;
|
}
|
|
std::string CToolUnits::getCurrentExePath() {
|
char path[MAX_PATH];
|
GetModuleFileName(NULL, path, MAX_PATH);
|
std::string exePath(path);
|
return exePath.substr(0, exePath.find_last_of("\\/"));
|
}
|
|
bool CToolUnits::isFile(const std::string& path) {
|
DWORD attributes = GetFileAttributes(path.c_str());
|
return (attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY));
|
}
|
|
bool CToolUnits::isDirectory(const std::string& path) {
|
DWORD attributes = GetFileAttributes(path.c_str());
|
return (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY));
|
}
|