forked from chunyi1994/cppevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.cpp
106 lines (86 loc) · 1.43 KB
/
timer.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
#include "timer.h"
#include <iostream>
namespace cppevent{
Time::Time(const struct timeval &tv) : tv_(tv)
{
}
Time::Time(time_t sec, time_t usec)
{
tv_.tv_sec = sec;
tv_.tv_usec = usec;
}
void Time::now()
{
gettimeofday(&tv_,&tz_);
}
bool Time::operator==(const Time& other) const
{
return usec() == other.usec() && sec() == other.sec();
}
bool Time::operator>(const Time& other) const
{
if(sec() > other.sec())
{
return true;
}
else if(sec() < other.sec())
{
return false;
}
if(usec() > other.usec()){
return true;
}
return false;
}
bool Time::operator<(const Time &other) const
{
if(operator==(other))
{
return false;
}
return !operator>(other);
}
Time &Time::operator+(const Time &other)
{
add(other);
return *this;
}
time_t Time::sec() const
{
return tv_.tv_sec;
}
time_t Time::usec() const
{
return tv_.tv_usec;
}
void Time::setSec(time_t sec)
{
tv_.tv_sec = sec;
}
void Time::setUSec(time_t usec)
{
tv_.tv_usec = usec;
}
void Time::addSec(time_t sec)
{
tv_.tv_sec += sec;
}
void Time::addUSec(time_t usec)
{
time_t sum = usec + tv_.tv_usec;
while(sum > 1000000){
tv_.tv_sec++;
sum = sum - 1000000;
}
tv_.tv_usec = sum;
}
void Time::add(const Time &other)
{
addUSec(other.tv_.tv_usec);
addSec(other.tv_.tv_sec);
}
struct timeval& Time::timeval()
{
return tv_;
}
}