-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helper.h
85 lines (66 loc) · 2.25 KB
/
Helper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef HELPER_H
#define HELPER_H
#include <ctime>
#include <string>
#include <sstream>
#include <fstream>
namespace Helper // custom namespace to store our custom data types
{
template <class T>
std::string ToString(const T &);
struct DateTime
{
int D, m, y, M, H, S;
DateTime()
{
time_t ms;
time(&ms);
struct tm* info = localtime(&ms);
// format data from info
D = info->tm_mday;
m = info->tm_mon + 1; // need to add 1 since january is represented 0
y = 1900 + info->tm_year; // reference year since C came in the 70s and locatime returns time from that particular day until present day
M = info->tm_min;
H = info->tm_hour;
S = info->tm_sec;
}
DateTime(int D, int m, int y, int M, int H, int S) : D(D), m(m), y(y), M(M), H(H), S(S) {}
DateTime(int D, int m, int y) : D(D), m(m), y(y), M(0), H(0), S(0) {}
DateTime Now() const
{
return DateTime(); // return current date time
}
std::string GetDateString() const
{
// Generate the current date that is correctly formatted in string
return std::string(D < 10 ? "0" : "") + ToString(D) +
std::string(m < 10 ? ".0" : ".") + ToString(m) + "." + ToString(y);
}
std::string GetTimeString(const std::string &sep = ":") const// reference is to default separator which is set to a colon
{
// Generate the current time that is correctly formatted in string
return std::string(H < 10 ? "0" : "") + ToString(H) + sep +
std::string(M < 10 ? "0" : "") + ToString(M) + sep +
std::string(S < 10 ? "0" : "") + ToString(S);
}
std::string GetDateTimeString(const std::string &sep = ":") const
{
return GetDateString() + " " + GetTimeString(sep);
}
};
template <class T>
std::string ToString(const T &e) // only able types that supports the insertion operator
{
std::ostringstream s;
s << e;
return s.str();
}
// OPTIONAL FUNCTION FOR DEBUGGING PURPOSES
void WriteAppLog(const std::string &s) // reference to const string we wish to log
{
std::ofstream file("AppLog.txt", std::ios::app); // app stands for append file
file << "[" << Helper::DateTime().GetDateTimeString() << "]" << "\n" << s << std::endl << "\n";
file.close();
}
}
#endif // HELPER_H