-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeObject.cpp
135 lines (119 loc) · 2.29 KB
/
TimeObject.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "stdafx.h"
#include "TimeObject.h"
#include <iostream>
#include <iomanip>
using namespace std;
unsigned int CTimeObject::GetMillisecond()
{
#ifdef _WIN32
return m_st.wMilliseconds;
#else
return m_tb.millitm;
#endif // _WIN32
}
unsigned int CTimeObject::GetSecond()
{
#ifdef _WIN32
return m_st.wSecond;
#else
return m_tm.tm_sec;
#endif // _WIN32
}
unsigned int CTimeObject::GetMinute()
{
#ifdef _WIN32
return m_st.wMinute;
#else
return m_tm.tm_min;
#endif // _WIN32
}
unsigned int CTimeObject::GetHour()
{
#ifdef _WIN32
return m_st.wHour;
#else
return m_tm.tm_hour;
#endif // _WIN32
}
unsigned int CTimeObject::GetDay()
{
#ifdef _WIN32
return m_st.wDay;
#else
return m_tm.tm_mday;
#endif // _WIN32
}
unsigned int CTimeObject::GetMonth()
{
#ifdef _WIN32
return m_st.wMonth;
#else
return m_tm.tm_mon+1;
#endif // _WIN32
}
unsigned int CTimeObject::GetYear()
{
#ifdef _WIN32
return m_st.wYear;
#else
return m_tm.tm_year+1900;
#endif // _WIN32
}
void CTimeObject::SetCurrentTime()
{
#ifdef _WIN32
GetSystemTime(&m_st);
#else
// Get the full time info
ftime(&m_tb);
// Split the time integer up into parts
localtime_r(&(m_tb.time), &m_tm);
#endif // _WIN32
}
CTimeObject CTimeObject::Now()
{
// Create a new object
CTimeObject obj;
// Set it to the current time
obj.SetCurrentTime();
// Return the new object
return obj;
}
/*
* Print the date and time information in the following format
* YYYY-MM-DD:HH:MM:SS.MMM
*/
void CTimeObject::Print()
{
cout << setfill('0');
cout << setw(4) << GetYear() << "-";
cout << setw(2) << GetMonth() << "-";
cout << setw(2) << GetDay() << ":";
cout << setw(2) << GetHour() << ":";
cout << setw(2) << GetMinute() << ":";
cout << setw(2) << GetSecond() << ".";
cout << setw(3) << GetMillisecond() << endl;
}
/*
* Print the time information in the following format
* YYYY-MM-DD
*/
void CTimeObject::PrintTime()
{
cout << setfill('0');
cout << setw(4) << GetYear() << "-";
cout << setw(2) << GetMonth() << "-";
cout << setw(2) << GetDay() << endl;
}
/*
* Print the date information in the following format
* HH:MM:SS.MMM
*/
void CTimeObject::PrintDate()
{
cout << setfill('0');
cout << setw(2) << GetHour() << ":";
cout << setw(2) << GetMinute() << ":";
cout << setw(2) << GetSecond() << ".";
cout << setw(3) << GetMillisecond() << endl;
}