Skip to content

Commit 4888223

Browse files
committed
Updated README. Fixed warnings by lgtm, added unit tests for datetime formats
1 parent d86c12d commit 4888223

File tree

6 files changed

+165
-16
lines changed

6 files changed

+165
-16
lines changed

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,36 +87,42 @@ std::string sFilename = WsjcppCore::doNormalizePath(".//../bin/some/../file.txt"
8787
static std::string getCurrentDirectory();
8888
```
8989

90-
### currentTime_milliseconds
90+
### getCurrentTimeInMilliseconds
9191

9292
```
93-
static long currentTime_milliseconds();
93+
static long getCurrentTimeInMilliseconds();
9494
```
9595

96-
### currentTime_seconds
96+
### getCurrentTimeInSeconds
9797

9898
```
99-
static long currentTime_seconds();
99+
static long getCurrentTimeInSeconds();
100100
```
101101

102-
### currentTime_forFilename
102+
### getCurrentTimeForFilename
103103

104104
```
105-
static std::string currentTime_forFilename();
105+
static std::string getCurrentTimeForFilename();
106106
```
107107

108-
### currentTime_logformat
108+
will be like this: `?`
109+
110+
### getCurrentTimeForLogFormat
109111

110112
```
111-
static std::string currentTime_logformat();
113+
static std::string getCurrentTimeForLogFormat();
112114
```
113115

114-
### threadId
116+
will be like this: `2020-09-17 02:22:40.755`
117+
118+
### getThreadId
115119

116120
```
117-
static std::string threadId();
121+
static std::string getThreadId();
118122
```
119123

124+
will be like this: `0x00007fa9c6a96740`
125+
120126
### formatTimeForWeb
121127

122128
```

src/wsjcpp_core.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ std::string WsjcppCore::getCurrentTimeForLogFormat() {
394394
nTimeStart = nTimeStart / 1000;
395395

396396
std::time_t tm_ = long(nTimeStart);
397-
// struct tm tstruct = *localtime(&tm_);
398-
struct tm tstruct = *gmtime ( &tm_ );
397+
struct tm tstruct;
398+
gmtime_r(&tm_, &tstruct);
399399

400400
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
401401
// for more information about date/time format
@@ -421,8 +421,8 @@ std::string WsjcppCore::getThreadId() {
421421
std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) {
422422
std::time_t tm_ = long(nTimeInSec);
423423
// struct tm tstruct = *localtime(&tm_);
424-
struct tm tstruct = *gmtime ( &tm_ );
425-
424+
struct tm tstruct;
425+
gmtime_r(&tm_, &tstruct);
426426

427427
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
428428
// for more information about date/time format
@@ -439,7 +439,8 @@ std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) {
439439
std::string WsjcppCore::formatTimeForFilename(long nTimeInSec) {
440440
std::time_t tm_ = long(nTimeInSec);
441441
// struct tm tstruct = *localtime(&tm_);
442-
struct tm tstruct = *gmtime ( &tm_ );
442+
struct tm tstruct;
443+
gmtime_r(&tm_, &tstruct);
443444

444445
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
445446
// for more information about date/time format
@@ -454,7 +455,8 @@ std::string WsjcppCore::formatTimeUTC(int nTimeInSec) {
454455
// datetime
455456
std::time_t tm_ = long(nTimeInSec);
456457
// struct tm tstruct = *localtime(&tm_);
457-
struct tm tstruct = *gmtime ( &tm_ );
458+
struct tm tstruct;
459+
gmtime_r(&tm_, &tstruct);
458460

459461
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
460462
// for more information about date/time format

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_file_permissions
6969
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_file_permissions.cpp")
7070
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_string_padding.h")
7171
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_string_padding.cpp")
72+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_date_time_format.h")
73+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_date_time_format.cpp")
7274

7375
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
7476

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "unit_test_date_time_format.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
#include <regex>
5+
6+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestDateTimeFormat)
7+
8+
UnitTestDateTimeFormat::UnitTestDateTimeFormat()
9+
: WsjcppUnitTestBase("UnitTestDateTimeFormat") {
10+
}
11+
12+
// ---------------------------------------------------------------------
13+
14+
bool UnitTestDateTimeFormat::doBeforeTest() {
15+
// nothing
16+
return true;
17+
}
18+
19+
// ---------------------------------------------------------------------
20+
21+
void UnitTestDateTimeFormat::executeTest() {
22+
23+
// format like '2020-09-17 02:22:40.755'
24+
{
25+
std::string sNow = WsjcppCore::getCurrentTimeForLogFormat();
26+
std::string sRegexp = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}$";
27+
std::regex rx(sRegexp);
28+
bool bResult = std::regex_match(sNow, rx);
29+
compare("now for log format: " + sNow + ", regexp: " + sRegexp, bResult, true);
30+
}
31+
32+
// like this '20200917_023347'
33+
{
34+
std::string sNow = WsjcppCore::getCurrentTimeForFilename();
35+
// std::cout << sNow << std::endl;
36+
std::string sRegexp = "^\\d{8}_\\d{6}$";
37+
std::regex rx(sRegexp);
38+
bool bResult = std::regex_match(sNow, rx);
39+
compare("now for filename: " + sNow + ", regexp: " + sRegexp, bResult, true);
40+
}
41+
42+
{
43+
long nMs = WsjcppCore::getCurrentTimeInMilliseconds();
44+
long nSec = WsjcppCore::getCurrentTimeInSeconds();
45+
// std::cout << nMs << std::endl;
46+
// std::cout << nSec << std::endl;
47+
long nMsToSec = nMs / 1000;
48+
compare("now sec and ms", nMsToSec, nSec);
49+
}
50+
51+
// like this '20200917_023347'
52+
{
53+
long nNow = WsjcppCore::getCurrentTimeInSeconds();
54+
std::string sNow = WsjcppCore::formatTimeForFilename(nNow);
55+
std::string sRegexp = "^\\d{8}_\\d{6}$";
56+
std::regex rx(sRegexp);
57+
bool bResult = std::regex_match(sNow, rx);
58+
compare("now for filename (2): " + sNow + ", regexp: " + sRegexp, bResult, true);
59+
}
60+
61+
// like 'Thu, 17 Sep 2020 02:39:52 GMT'
62+
{
63+
std::map<long, std::string> mapTimes;
64+
65+
// monthes
66+
// 2592000 - 30 days
67+
mapTimes[1578105758] = "Sat, 04 Jan 2020 02:42:38 GMT";
68+
mapTimes[1580697758] = "Mon, 03 Feb 2020 02:42:38 GMT";
69+
mapTimes[1583289758] = "Wed, 04 Mar 2020 02:42:38 GMT";
70+
mapTimes[1585881758] = "Fri, 03 Apr 2020 02:42:38 GMT";
71+
mapTimes[1588473758] = "Sun, 03 May 2020 02:42:38 GMT";
72+
mapTimes[1591065758] = "Tue, 02 Jun 2020 02:42:38 GMT";
73+
mapTimes[1593657758] = "Thu, 02 Jul 2020 02:42:38 GMT";
74+
mapTimes[1596249758] = "Sat, 01 Aug 2020 02:42:38 GMT";
75+
mapTimes[1598841758] = "Mon, 31 Aug 2020 02:42:38 GMT";
76+
mapTimes[1601433758] = "Wed, 30 Sep 2020 02:42:38 GMT";
77+
mapTimes[1604025758] = "Fri, 30 Oct 2020 02:42:38 GMT";
78+
mapTimes[1606617758] = "Sun, 29 Nov 2020 02:42:38 GMT";
79+
mapTimes[1609209758] = "Tue, 29 Dec 2020 02:42:38 GMT";
80+
81+
// week days
82+
mapTimes[1599964958] = "Sun, 13 Sep 2020 02:42:38 GMT";
83+
mapTimes[1600051358] = "Mon, 14 Sep 2020 02:42:38 GMT";
84+
mapTimes[1600137758] = "Tue, 15 Sep 2020 02:42:38 GMT";
85+
mapTimes[1600224158] = "Wed, 16 Sep 2020 02:42:38 GMT";
86+
mapTimes[1600310558] = "Thu, 17 Sep 2020 02:42:38 GMT";
87+
mapTimes[1600396958] = "Fri, 18 Sep 2020 02:42:38 GMT";
88+
mapTimes[1600483358] = "Sat, 19 Sep 2020 02:42:38 GMT";
89+
90+
std::map<long, std::string>::iterator it;
91+
for (it = mapTimes.begin(); it != mapTimes.end(); ++it) {
92+
long nTime = it->first;
93+
std::string sTime = WsjcppCore::formatTimeForWeb(nTime);
94+
// std::cout << sTime << std::endl;
95+
// std::string sRegexp = "^(Sun|Mon|Tue|Wed|Thu|Fri|Sat){1}, \\d{1,2} .* \\d{1,4} GMT$";
96+
std::string sRegexp = "^(Sun|Mon|Tue|Wed|Thu|Fri|Sat){1}, \\d{2} "
97+
"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) "
98+
"\\d{1,4} \\d{2}:\\d{2}:\\d{2} GMT$";
99+
std::regex rx(sRegexp);
100+
bool bResult = std::regex_match(sTime, rx);
101+
compare("now for web: " + sTime + ", regexp: " + sRegexp, bResult, true);
102+
compare("now for web str:", sTime, it->second);
103+
}
104+
}
105+
106+
// like "2020-09-17 02:39:52"
107+
{
108+
long nNow = WsjcppCore::getCurrentTimeInSeconds();
109+
std::string sNow = WsjcppCore::formatTimeUTC(nNow);
110+
std::string sRegexp = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$";
111+
std::regex rx(sRegexp);
112+
bool bResult = std::regex_match(sNow, rx);
113+
compare("now for time utc: " + sNow + ", regexp: " + sRegexp, bResult, true);
114+
}
115+
}
116+
117+
// ---------------------------------------------------------------------
118+
119+
bool UnitTestDateTimeFormat::doAfterTest() {
120+
// nothing
121+
return true;
122+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_DATE_TIME_FORMAT_H
2+
#define UNIT_TEST_DATE_TIME_FORMAT_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
class UnitTestDateTimeFormat : public WsjcppUnitTestBase {
7+
public:
8+
UnitTestDateTimeFormat();
9+
virtual bool doBeforeTest() override;
10+
virtual void executeTest() override;
11+
virtual bool doAfterTest() override;
12+
};
13+
14+
#endif // UNIT_TEST_DATE_TIME_FORMAT_H
15+

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ unit-tests:
8686
description: ""
8787
- name: "StringPadding"
8888
description: ""
89+
- name: "DateTimeFormat"
90+
description: ""

0 commit comments

Comments
 (0)