Skip to content

Commit 7c85479

Browse files
authored
Merge pull request #42 from kimkulling/feature/add_datetime
Add datetime class
2 parents aa18d4a + 7c6fe18 commit 7c85479

File tree

6 files changed

+171
-16
lines changed

6 files changed

+171
-16
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ SET(cppcore_src
7474
)
7575

7676
SET(cppcore_common_src
77+
code/Common/Logger.cpp
78+
include/cppcore/Common/DateTime.h
7779
include/cppcore/Common/Hash.h
7880
include/cppcore/Common/Logger.h
79-
code/Common/Logger.cpp
8081
include/cppcore/Common/TStringBase.h
8182
include/cppcore/Common/TStringView.h
8283
include/cppcore/Common/Variant.h
@@ -135,7 +136,9 @@ IF( CPPCORE_BUILD_UNITTESTS )
135136
)
136137

137138
SET( cppcore_common_test_src
139+
test/common/DateTimeTest.cpp
138140
test/common/HashTest.cpp
141+
test/common/LoggerTest.cpp
139142
test/common/VariantTest.cpp
140143
test/common/SortTest.cpp
141144
test/common/TBitFieldTest.cpp

code/Common/Logger.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2121
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
-----------------------------------------------------------------------------------------------*/
2323
#include <cppcore/Common/Logger.h>
24+
#include <cppcore/Common/DateTime.h>
2425

2526
#include <cassert>
2627
#include <iomanip>
@@ -88,6 +89,14 @@ Logger *Logger::create() {
8889
return sLogger;
8990
}
9091

92+
void Logger::set(Logger *logger) {
93+
if (logger == sLogger) {
94+
return;
95+
}
96+
kill();
97+
sLogger = logger;
98+
}
99+
91100
Logger *Logger::getInstance() {
92101
if (nullptr == sLogger) {
93102
static_cast<void>(create());
@@ -97,7 +106,7 @@ Logger *Logger::getInstance() {
97106
}
98107

99108
void Logger::kill() {
100-
if (sLogger) {
109+
if (sLogger != nullptr) {
101110
delete sLogger;
102111
sLogger = nullptr;
103112
}
@@ -134,7 +143,10 @@ void Logger::debug(const String &domain, const String &msg) {
134143
}
135144

136145
void Logger::info(const String &domain, const String &msg) {
137-
if (getVerboseMode() == VerboseMode::Normal || getVerboseMode() == VerboseMode::Verbose || getVerboseMode() == VerboseMode::Debug || getVerboseMode() == VerboseMode::Trace) {
146+
if (getVerboseMode() == VerboseMode::Normal ||
147+
getVerboseMode() == VerboseMode::Verbose ||
148+
getVerboseMode() == VerboseMode::Debug ||
149+
getVerboseMode() == VerboseMode::Trace) {
138150
String logMsg;
139151

140152
logMsg += String("Info: ", 6);
@@ -243,19 +255,19 @@ Logger::~Logger() {
243255
}
244256

245257
String Logger::getDateTime() {
246-
//static const uint32_t Space = 2;
247-
/* DateTime currentDateTime = DateTime::getCurrentUTCTime();
258+
static const uint32_t Space = 2;
259+
DateTime currentDateTime;
248260
std::stringstream stream;
249261
stream.fill('0');
250-
stream << std::setw(Space) << currentDateTime.getCurrentDay() << "."
251-
<< std::setw(Space) << currentDateTime.getCurrentMonth() << "."
252-
<< std::setw(Space * 2) << currentDateTime.getCurrentYear() << " "
253-
<< std::setw(Space) << currentDateTime.getCurrentHour() << ":"
254-
<< std::setw(Space) << currentDateTime.getCurrentMinute() << ":"
255-
<< std::setw(Space) << currentDateTime.getCurrentSeconds();
256-
*/
257-
String todo("none", 4);
258-
return todo;
262+
stream << std::setw(Space) << currentDateTime.year << "."
263+
<< std::setw(Space) << currentDateTime.month << "."
264+
<< std::setw(Space * 2) << currentDateTime.day << " "
265+
<< std::setw(Space) << currentDateTime.hour << ":"
266+
<< std::setw(Space) << currentDateTime.minute << ":"
267+
<< std::setw(Space) << currentDateTime.second;
268+
const std::string tmp(stream.str());
269+
String dateTime(tmp.c_str(), tmp.size());
270+
return dateTime;
259271
}
260272

261273
void Logger::StdLogStream::write(const String &msg) {

include/cppcore/Common/DateTime.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014-2025 Kim Kulling
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
-----------------------------------------------------------------------------------------------*/
23+
#pragma once
24+
25+
#include <cppcore/CPPCoreCommon.h>
26+
#include <ctime>
27+
28+
namespace cppcore {
29+
30+
//-------------------------------------------------------------------------------------------------
31+
/// @class DateTime
32+
/// @ingroup CPPCore
33+
///
34+
/// @brief This class is used to get the current date and time.
35+
//-------------------------------------------------------------------------------------------------
36+
struct DateTime {
37+
uint32_t year{}; ///< The current year
38+
uint32_t month{}; ///< The current month
39+
uint32_t day{}; ///< The current day
40+
uint32_t hour{}; ///< The current hour
41+
uint32_t minute{}; ///< The current minute
42+
uint32_t second{}; ///< The current second
43+
44+
/// @brief The class constructor.
45+
DateTime() {
46+
time_t timestamp = ::time(nullptr);
47+
tm dt{};
48+
#if defined(_WIN32)
49+
::localtime_s(&dt, &timestamp);
50+
+#else
51+
::localtime_r(&timestamp, &dt);
52+
+#endif year = dt.tm_year + 1900;
53+
month = dt.tm_mon;
54+
day = dt.tm_mday;
55+
hour = dt.tm_hour;
56+
minute = dt.tm_min;
57+
second = dt.tm_sec;
58+
}
59+
};
60+
61+
} // namespace cppcore
62+

include/cppcore/Common/Logger.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,14 @@ class DLL_CPPCORE_EXPORT Logger final {
101101
Count ///< Number of enums
102102
};
103103

104-
public:
105104
/// @brief Creates the unique logger instance and returns a pointer showing to it.
106105
/// @return The singleton pointer of the logger.
107106
static Logger *create();
108107

108+
/// @brief Will set a user-defined logger instance.
109+
/// @param[in] logger The new logger instance.
110+
static void set(Logger *logger);
111+
109112
/// @brief returns the singleton instance pointer of the logger.
110113
/// @return The singleton pointer of the logger.
111114
static Logger *getInstance();
@@ -250,4 +253,3 @@ void fatalPrint( const String &domain, const String &file, int line, const Strin
250253
/// @param message The warning to writhe into the log.
251254
//-------------------------------------------------------------------------------------------------
252255
#define log_fatal(domain, message) ::cppcore::fatalPrint(domain, __FILE__, __LINE__, message)
253-

test/common/DateTimeTest.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014-2025 Kim Kulling
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
-----------------------------------------------------------------------------------------------*/
23+
#include <cppcore/Common/DateTime.h>
24+
#include "gtest/gtest.h"
25+
26+
using namespace cppcore;
27+
28+
class DateTimeTest : public testing::Test {};
29+
30+
TEST_F(DateTimeTest, CreateTest) {
31+
DateTime dt;
32+
const time_t ts = ::time(nullptr);
33+
tm now{};
34+
#if defined(_WIN32)
35+
::localtime_s(&now, &ts);
36+
+#else
37+
::localtime_r(&ts, &now);
38+
+#endif
39+
EXPECT_EQ(dt.year, static_cast<uint32_t>(now.tm_year + 1900));
40+
EXPECT_GE(dt.month, 1u);
41+
EXPECT_LE(dt.month, 12u);
42+
EXPECT_GE(dt.day, 1u);
43+
EXPECT_LE(dt.day, 31u);
44+
}

test/common/LoggerTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014-2025 Kim Kulling
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
-----------------------------------------------------------------------------------------------*/
23+
#include <cppcore/Common/Logger.h>
24+
#include "gtest/gtest.h"
25+
26+
using namespace cppcore;
27+
28+
class LoggerTest : public testing::Test {};
29+
30+
TEST_F(LoggerTest, CreateTest) {
31+
// will come
32+
}

0 commit comments

Comments
 (0)