Skip to content

Commit 4ee30be

Browse files
committed
Add datetime class
1 parent aa18d4a commit 4ee30be

File tree

6 files changed

+109
-16
lines changed

6 files changed

+109
-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: 22 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,11 @@ Logger *Logger::create() {
8889
return sLogger;
8990
}
9091

92+
void Logger::set(Logger *logger) {
93+
kill();
94+
sLogger = logger;
95+
}
96+
9197
Logger *Logger::getInstance() {
9298
if (nullptr == sLogger) {
9399
static_cast<void>(create());
@@ -97,7 +103,7 @@ Logger *Logger::getInstance() {
97103
}
98104

99105
void Logger::kill() {
100-
if (sLogger) {
106+
if (sLogger != nullptr) {
101107
delete sLogger;
102108
sLogger = nullptr;
103109
}
@@ -134,7 +140,10 @@ void Logger::debug(const String &domain, const String &msg) {
134140
}
135141

136142
void Logger::info(const String &domain, const String &msg) {
137-
if (getVerboseMode() == VerboseMode::Normal || getVerboseMode() == VerboseMode::Verbose || getVerboseMode() == VerboseMode::Debug || getVerboseMode() == VerboseMode::Trace) {
143+
if (getVerboseMode() == VerboseMode::Normal ||
144+
getVerboseMode() == VerboseMode::Verbose ||
145+
getVerboseMode() == VerboseMode::Debug ||
146+
getVerboseMode() == VerboseMode::Trace) {
138147
String logMsg;
139148

140149
logMsg += String("Info: ", 6);
@@ -243,19 +252,19 @@ Logger::~Logger() {
243252
}
244253

245254
String Logger::getDateTime() {
246-
//static const uint32_t Space = 2;
247-
/* DateTime currentDateTime = DateTime::getCurrentUTCTime();
255+
static const uint32_t Space = 2;
256+
DateTime currentDateTime;
248257
std::stringstream stream;
249258
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;
259+
stream << std::setw(Space) << currentDateTime.year << "."
260+
<< std::setw(Space) << currentDateTime.month << "."
261+
<< std::setw(Space * 2) << currentDateTime.day << " "
262+
<< std::setw(Space) << currentDateTime.hour << ":"
263+
<< std::setw(Space) << currentDateTime.min << ":"
264+
<< std::setw(Space) << currentDateTime.sec;
265+
const std::string tmp(stream.str());
266+
String dateTime(tmp.c_str(), tmp.size());
267+
return dateTime;
259268
}
260269

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

include/cppcore/Common/DateTime.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 THash
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 min{}; ///< The current minute
42+
uint32_t sec{}; ///< The current second
43+
44+
/// @brief The class constructor.
45+
DateTime() {
46+
time_t timestamp = ::time(nullptr);
47+
tm dt{};
48+
dt = *::localtime(&timestamp);
49+
year = dt.tm_year + 1900;
50+
month = dt.tm_mon;
51+
day = dt.tm_mday;
52+
hour = dt.tm_hour;
53+
min = dt.tm_min;
54+
sec = dt.tm_sec;
55+
}
56+
};
57+
58+
} // namespace cppcore

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <cppcore/Common/DateTime.h>
2+
#include "gtest/gtest.h"
3+
4+
using namespace cppcore;
5+
6+
class DateTimeTest : public testing::Test {};
7+
8+
TEST_F(DateTimeTest, CreateTest) {
9+
DateTime dt;
10+
EXPECT_EQ(dt.year, 2025);
11+
}

test/common/LoggerTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <cppcore/Common/Logger.h>
2+
#include "gtest/gtest.h"
3+
4+
using namespace cppcore;
5+
6+
class LoggerTest : public testing::Test {};
7+
8+
TEST_F(LoggerTest, CreateTest) {
9+
10+
}

0 commit comments

Comments
 (0)