/
DateTimeInfo.cpp
executable file
·119 lines (98 loc) · 2.06 KB
/
DateTimeInfo.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "DateTimeInfo.h"
DateTimeInfo::DateTimeInfo()
{
cDate = currentDate();
cTime = currentTime();
cTimezone = currentTimezone();
}
//returns current date
std::string DateTimeInfo::currentDate()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
//weekday month day year
strftime(buf, sizeof(buf), "%A %B %d %Y", &tstruct);
return buf;
}
//returns current UTC date
std::string DateTimeInfo::currentUTCDate()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *gmtime(&now);
//weekday month day year
strftime(buf, sizeof(buf), "%A %B %d %Y", &tstruct);
return buf;
}
//returns current date
std::string DateTimeInfo::currentYYYY()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
//weekday month day year
strftime(buf, sizeof(buf), "%Y", &tstruct);
return buf;
}
//returns current date
std::string DateTimeInfo::currentYY()
{
return currentYYYY().substr(2, 2);
}
//returns current time
std::string DateTimeInfo::currentTime()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%X", &tstruct);
return buf;
}
//returns current UTC time
std::string DateTimeInfo::currentUTCTime()
{
time_t now = time(0);
struct tm tstruct;
tstruct = *gmtime(&now);
std::stringstream ss;
if(tstruct.tm_hour < 10)
ss << "0";
ss << tstruct.tm_hour << ":";
if(tstruct.tm_min < 10)
ss << "0";
ss << tstruct.tm_min << ":";
if(tstruct.tm_sec < 10)
ss << "0";
ss << tstruct.tm_sec;
return ss.str();
}
//returns current time zone
std::string DateTimeInfo::currentTimezone()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Z", &tstruct);
return buf;
}
//returns current operating system
std::string DateTimeInfo::currentOS()
{
#if defined _WIN32 || defined _WIN64
return "Windows";
#elif __APPLE__ //osx
return "Macintosh";
#elif __FreeBSD__
return "FreeBSD";
#elif __linux__
return "Linux";
#else //unix
return "Unix";
#endif
}