-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcTime_t.cpp
169 lines (117 loc) · 2.48 KB
/
cTime_t.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "cTime_t.h"
#include <time.h>
#include <iostream>
#include <iomanip>
using namespace std;
/******************
* Static Members
******************/
int cTime_t::format = 1; //Default format setting
/****************
* CTORs and DTOR
****************/
cTime_t::cTime_t() {
//Get current Time zone
time_t time_data;
struct tm* current_time;
time( &time_data);
current_time = localtime(&time_data);
setTime_p(current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
}
cTime_t::cTime_t(int h, int m, int s) {
setTime_p(h,m,s);
}
cTime_t::cTime_t(const cTime_t& t) {
setTime_p(t.getHours(), t.getMinutes(), t.getSeconds());
}
cTime_t::~cTime_t() {
//DTOR
}
/************
* Operators
***********/
const cTime_t& cTime_t::operator=(const cTime_t &t) {
if (this != &t){
setTime_p(t.getHours(), t.getMinutes(), t.getSeconds());
}
return *this;
}
const cTime_t& cTime_t::operator+(const cTime_t &t) {
addTime_p(t.getHours(), t.getMinutes(), t.getSeconds());
return *this;
}
/******************
* Public Methods
****************/
void cTime_t::setTime(int h, int m, int s) {
setTime_p(h,m,s);
}
void cTime_t::printTime() const{
printTime(this->format);
}
void cTime_t::printTime(int f) const{
switch (f){
case 1:
cout << setfill('0');
cout << setw(2) << getHours() << ':'
<< setw(2) << getMinutes() << ':'
<< setw(2) << getSeconds();
break;
case 2:
if (getHours() > 12)
cout << (getHours() - 12);
else
cout << getHours();
cout << ":";
cout << setfill('0');
cout << setw(2) << getMinutes() << ':'
<< setw(2) << getSeconds() << ' ';
if (getHours() > 12)
cout << "PM";
else
cout << "AM";
break;
default: //Unknown format...
cout << "Unknown format!";
break;
}
}
/******************
* Private Methods
******************/
/*
* Add h, m, s to current time
* Take care of overflows (s < 60, m < 60, h < 24)
*/
void cTime_t::addTime_p(int h, int m, int s) {
//Set seconds
s += seconds;
seconds = s % 60;
m += int(s/60);
//Set minutes
m += minutes;
minutes = m % 60;
h += int(m/60);
//Set hours
h += hours;
hours = h % 24;
}
/*
* Set time as 0:0:0
*/
void cTime_t::zeroTime(){
hours = 0;
minutes = 0;
seconds = 0;
}
/**
* Set current time as h:m:s . (Takes care of overflows)
*/
void cTime_t::setTime_p(int h, int m, int s){
zeroTime();
//case value invalid set to default value 00:00:00
if (s < 0 || m < 0 || h < 0){
return;
}
addTime_p(h,m,s);
}