Skip to content

Commit 542fcb8

Browse files
committed
add cthread, cdatetime, cstring
1 parent 3e08972 commit 542fcb8

18 files changed

+471
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
*~
3+
*.user

CPPUtility/CPPUtility.pro

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
TARGET = CPPUtility
2+
TEMPLATE = lib
3+
CONFIG -= qt
4+
CONFIG += c++11
5+
6+
unix
7+
{
8+
CONFIG(debug, debug|release) {
9+
DESTDIR = $$PWD/../build/debug
10+
} else {
11+
DESTDIR = $$PWD/../build/release
12+
}
13+
}
14+
15+
HEADERS += \
16+
cthread.h \
17+
cdatetime.h \
18+
cstring.h
19+
20+
SOURCES += \
21+
cthread.cpp \
22+
cdatetime.cpp \
23+
cstring.cpp

CPPUtility/cdatetime.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "cdatetime.h"
2+
3+
#include <string.h>
4+
#include <time.h>
5+
#include <chrono>
6+
7+
bool CDateTime::GetCurrentDateTime(std::string &strtime, std::string strformat)
8+
{
9+
long timestamp = std::time(nullptr);
10+
return GetTimeAsString(strtime, strformat, timestamp);
11+
}
12+
13+
bool CDateTime::GetTimeAsString(std::string &strtime, std::string strformat, long timestamp)
14+
{
15+
char buffer[30];
16+
memset(buffer, 0, sizeof(buffer));
17+
18+
std::chrono::seconds cseconds(timestamp);
19+
std::chrono::system_clock::time_point sctimepoint(cseconds);
20+
std::time_t ttime = std::chrono::system_clock::to_time_t(sctimepoint);
21+
std::tm tmres;
22+
localtime_r(&ttime, &tmres);
23+
std::strftime(buffer, 30, strformat.data(), &tmres);
24+
25+
strtime = std::string(buffer);
26+
return true;
27+
}
28+
29+
bool CDateTime::GetTimeFromString(std::string strtime, long &timestamp)
30+
{
31+
int nyear, nmon, nday, nhou, nmin, nsec;
32+
33+
sscanf(strtime.data(), "%d-%d-%dT%d:%d:%d", &nyear, &nmon, &nday, &nhou, &nmin, &nsec);
34+
std::tm stimeinfo;
35+
stimeinfo.tm_year = nyear-1900;
36+
stimeinfo.tm_mon = nmon-1;
37+
stimeinfo.tm_mday = nday;
38+
stimeinfo.tm_hour = nhou;
39+
stimeinfo.tm_min = nmin;
40+
stimeinfo.tm_sec = nsec;
41+
42+
timestamp = std::mktime(&stimeinfo);
43+
return true;
44+
}
45+
46+
int CDateTime::GetDiffDay(long starttime, long endtime)
47+
{
48+
double diffsec = std::difftime(endtime, starttime);
49+
int diffday = diffsec / (24 * 3600);
50+
return diffday;
51+
}

CPPUtility/cdatetime.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef CDATETIME_H
2+
#define CDATETIME_H
3+
4+
#include <string>
5+
6+
namespace CDateTime {
7+
/**
8+
* @brief GetCurrentDateTime transform current timestamp into strtime according to strformat
9+
* @param strtime formatted string
10+
* @param strformat format, referring to format of 'std::strftime()'
11+
* @return
12+
*/
13+
bool GetCurrentDateTime(std::string &strtime, std::string strformat);
14+
15+
/**
16+
* @brief GetTimeAsString transform timestamp into strtime according to strformat
17+
* @param strtime formatted string
18+
* @param strformat format, referring to format of 'std::strftime()'
19+
* @param timestamp given time
20+
* @return
21+
*/
22+
bool GetTimeAsString(std::string &strtime, std::string strformat, long timestamp);
23+
24+
/**
25+
* @brief GetTimeFromString transform strtime into timestamp according to strformat
26+
* @param strtime given string of time, "yyyy-mm-ddThh:mm:ss"
27+
* @param timestamp requested time
28+
* @return
29+
*/
30+
bool GetTimeFromString(std::string strtime, long &timestamp);
31+
32+
/**
33+
* @brief GetDiffDay calculate different day from starttime to endtime
34+
* @param starttime start timestamp
35+
* @param endtime end timestamp
36+
* @return
37+
*/
38+
int GetDiffDay(long starttime, long endtime);
39+
40+
} //namespace CDateTime
41+
42+
#endif // CDATETIME_H

CPPUtility/cstring.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "cstring.h"
2+
3+
#ifdef __linux__
4+
#include <strings.h>
5+
#endif
6+
7+
#include <stdio.h>
8+
9+
int CString::StrCmpCaseInsensitive(const char *str1, const char *str2)
10+
{
11+
int ret = 0;
12+
#ifdef __linux__
13+
ret = strcasecmp(str1, str2);
14+
#else
15+
ret = stricmp(str1, str2);
16+
#endif
17+
return ret;
18+
}
19+
20+
int CString::NumberToString(char *str, int num, const char * format)
21+
{
22+
str = new char[20]();
23+
return sprintf(str, format, num);
24+
}
25+
26+
int CString::StringToNumber(const char *str, int num, const char *format)
27+
{
28+
return sscanf(str, format, num);
29+
}

CPPUtility/cstring.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef CSTRING_H
2+
#define CSTRING_H
3+
4+
#include <stdio.h>
5+
6+
#define MYLOG(fmt, ...) printf("File %s line %d: " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
7+
8+
namespace CString
9+
{
10+
int StrCmpCaseInsensitive(const char *str1, const char *str2);
11+
int NumberToString(char *str, int num, const char *format = "%d");
12+
int StringToNumber(const char *str, int num, const char *format = "%d");
13+
} //namespace CString
14+
15+
#endif // CSTRING_H

CPPUtility/cthread.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "cthread.h"
2+
3+
4+
#include <iostream>
5+
6+
CThread::CThread()
7+
: m_isRunning(false)
8+
{
9+
10+
}
11+
12+
CThread::~CThread()
13+
{
14+
if(m_thread.joinable())
15+
{
16+
m_thread.join();
17+
}
18+
}
19+
20+
void CThread::start()
21+
{
22+
23+
#ifndef _POSIX_MONOTONIC_CLOCK
24+
std::cout << "error" << std::endl;
25+
#endif
26+
27+
m_startmutex.lock();
28+
m_thread = std::thread(&CThread::thread, this);
29+
m_thread.detach();
30+
}
31+
32+
bool CThread::isRunning()
33+
{
34+
return m_isRunning;
35+
}
36+
37+
bool CThread::isFinished()
38+
{
39+
return !m_isRunning;
40+
}
41+
42+
bool CThread::wait(unsigned long time)
43+
{
44+
std::unique_lock<std::mutex> locker(m_startmutex);
45+
std::chrono::seconds cseconds(time);
46+
std::chrono::system_clock::time_point sctimepoint(cseconds);
47+
bool bWait = m_mutex.try_lock_until(sctimepoint);
48+
if(bWait)
49+
m_mutex.unlock();
50+
return bWait;
51+
}
52+
53+
void CThread::sleep(unsigned long sec)
54+
{
55+
std::this_thread::sleep_for(std::chrono::seconds(sec));
56+
}
57+
58+
void CThread::finished()
59+
{
60+
}
61+
62+
void CThread::thread()
63+
{
64+
m_startmutex.unlock();
65+
std::unique_lock<std::timed_mutex> locker(m_mutex);
66+
m_isRunning = true;
67+
run();
68+
// finish
69+
finished();
70+
m_isRunning = false;
71+
}

CPPUtility/cthread.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef CTHREAD_H
2+
#define CTHREAD_H
3+
4+
#include <thread>
5+
#include <mutex>
6+
#include <condition_variable>
7+
8+
class CThread
9+
{
10+
public:
11+
CThread();
12+
~CThread();
13+
14+
// 开启线程
15+
void start();
16+
17+
// 线程是否运行
18+
bool isRunning();
19+
bool isFinished();
20+
21+
bool wait(unsigned long time = -1);
22+
static void sleep(unsigned long sec);
23+
24+
protected:
25+
/**
26+
* @brief run a pure function to be executed in thread body
27+
*/
28+
virtual void run() = 0;
29+
virtual void finished();
30+
31+
private:
32+
void thread();
33+
34+
private:
35+
bool m_isRunning;
36+
37+
std::thread m_thread;
38+
std::mutex m_startmutex;
39+
std::timed_mutex m_mutex;
40+
};
41+
42+
#endif // CTHREAD_H

UtilityTest/UtilityTest.pro

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
TARGET = UtilityTest
2+
TEMPLATE = lib
3+
CONFIG -= qt
4+
CONFIG += c++11
5+
6+
INCLUDEPATH += \
7+
$$PWD/../CPPUtility
8+
9+
unix
10+
{
11+
CONFIG(debug, debug|release) {
12+
DESTDIR = $$PWD/../build/debug
13+
} else {
14+
DESTDIR = $$PWD/../build/release
15+
}
16+
}
17+
18+
HEADERS += \
19+
cthreadtest.h \
20+
cstringtest.h \
21+
cdatetimetest.h
22+
23+
SOURCES += \
24+
cthreadtest.cpp \
25+
cstringtest.cpp \
26+
cdatetimetest.cpp

UtilityTest/cdatetimetest.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "cdatetimetest.h"
2+
3+
#include <chrono>
4+
#include <iostream>
5+
6+
#include "cdatetime.h"
7+
8+
void CDateTimeTest::Test()
9+
{
10+
// get current time
11+
std::string curtime;
12+
CDateTime::GetCurrentDateTime(curtime, "%FT%T");
13+
std::cout << "CDateTimeTest::Test--curtime=[" << curtime << "]" << std::endl;
14+
15+
// get request parameters
16+
std::string starttime = "2010-11-11T19:46:17";
17+
std::string endtime = "2010-11-12T19:46:17";
18+
19+
// get seconds_since_epoch for starttime and endtime
20+
long scount=-1, ecount=-1;
21+
CDateTime::GetTimeFromString(starttime, scount);
22+
CDateTime::GetTimeFromString(endtime, ecount);
23+
std::cout << "CDateTimeTest::Test--scount=" << scount << ";ecount=" << ecount << std::endl;
24+
25+
std::string tableName;
26+
std::chrono::system_clock::time_point stimepoint = std::chrono::system_clock::from_time_t(scount);
27+
28+
int diffday = CDateTime::GetDiffDay(scount, ecount);
29+
for(int i=-1; i<=diffday && diffday>=0; i++)
30+
{
31+
tableName.clear();
32+
std::chrono::hours oneday(i*24);
33+
std::chrono::system_clock::time_point itimepoint = stimepoint + oneday;
34+
time_t itime = std::chrono::system_clock::to_time_t(itimepoint);
35+
CDateTime::GetTimeAsString(tableName, "S%G%m%d", itime);
36+
37+
std::cout << "CDateTimeTest::Test--table name: " << tableName << std::endl;
38+
}
39+
40+
long timestamp = 1499258297;
41+
std::string stimetamsp;
42+
CDateTime::GetTimeAsString(stimetamsp, "%FT%T", timestamp);
43+
std::cout << "CDateTimeTest::Test--stimestamp=" << stimetamsp << std::endl;
44+
}

UtilityTest/cdatetimetest.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef CDATETIMETEST_H
2+
#define CDATETIMETEST_H
3+
4+
class CDateTimeTest
5+
{
6+
public:
7+
CDateTimeTest(){Test();}
8+
~CDateTimeTest(){}
9+
10+
private:
11+
void Test();
12+
};
13+
14+
#endif // CDATETIMETEST_H

UtilityTest/cstringtest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "cstringtest.h"
2+
3+
#include <iostream>
4+
5+
#include "cstring.h"
6+
7+
void CStringTest::Test()
8+
{
9+
std::cout << "CStringTimeTest::Test-- " << CString::StrCmpCaseInsensitive("dec", "DEC") << std::endl;
10+
std::cout << "CStringTimeTest::Test-- " << CString::StrCmpCaseInsensitive("dec", "eEC") << std::endl;
11+
std::cout << "CStringTimeTest::Test-- " << CString::StrCmpCaseInsensitive("eec", "DEC") << std::endl;
12+
MYLOG("%s", "CStringTest::Hello!");
13+
}

UtilityTest/cstringtest.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef CSTRINGTEST_H
2+
#define CSTRINGTEST_H
3+
4+
class CStringTest
5+
{
6+
public:
7+
CStringTest(){Test();}
8+
~CStringTest(){}
9+
10+
private:
11+
void Test();
12+
};
13+
14+
#endif // CSTRINGTEST_H

0 commit comments

Comments
 (0)