-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.cpp
135 lines (121 loc) · 2.81 KB
/
time.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 "darmstadt.h"
// extensions to timespec
struct timespec mkts(time_t sec, long nsec) {
struct timespec ret;
ret.tv_sec=sec;
ret.tv_nsec=nsec;
while (ret.tv_nsec>1000000000) {
ret.tv_sec++;
ret.tv_nsec-=1000000000;
}
return ret;
}
const int pow10[10]={
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000
};
struct timespec stots(string s) {
struct timespec ret;
long int upper, lower;
bool curStat;
string upel, lowel;
curStat=false;
for (size_t i=0; i<s.length(); i++) {
if (s[i]=='.' || s[i]==',') {
if (curStat) {
throw std::invalid_argument("stots");
}
curStat=true;
continue;
}
if (s[i]<'0' || s[i]>'9') {
throw std::invalid_argument("stots");
}
if (curStat) {
if (lowel.length()<10) {
lowel+=s[i];
}
} else {
upel+=s[i];
}
}
if (lowel.empty()) lowel="0";
try {
upper=stoi(upel);
} catch (std::exception& err) {
throw err;
}
try {
lower=stoi(lowel)*pow10[9-lowel.length()];
} catch (std::exception& err) {
throw err;
}
ret.tv_sec=upper;
ret.tv_nsec=lower;
return ret;
}
string tstos(struct timespec ts) {
string ret,sdeci;
int deci;
deci=ts.tv_nsec;
ret+=std::to_string(ts.tv_sec);
ret+=".";
for (int i=0; i<9; i++) {
sdeci.insert(0,1,'0'+(deci%10));
deci/=10;
}
while (sdeci.size()>1 && *(sdeci.end()-1)=='0') {
sdeci.erase(sdeci.size()-1);
}
ret+=sdeci;
return ret;
}
struct timespec curTime(clockid_t clockSource) {
struct timespec ret;
clock_gettime(clockSource,&ret);
return ret;
}
bool operator >(const struct timespec& l, const struct timespec& r) {
if (l.tv_sec==r.tv_sec) {
return l.tv_nsec>r.tv_nsec;
}
return l.tv_sec>r.tv_sec;
}
bool operator <(const struct timespec& l, const struct timespec& r) {
if (l.tv_sec==r.tv_sec) {
return l.tv_nsec<r.tv_nsec;
}
return l.tv_sec<r.tv_sec;
}
bool operator ==(const struct timespec& l, const struct timespec& r) {
return (l.tv_sec==r.tv_sec && l.tv_nsec==r.tv_nsec);
}
struct timespec operator -(const struct timespec& l, const long& r) {
struct timespec ret;
ret=l;
if ((ret.tv_nsec-=r)<0) {
ret.tv_sec-=1-ret.tv_nsec/1000000000;
ret.tv_nsec+=(1+(ret.tv_nsec/1000000000))*1000000000;
}
return ret;
}
struct timespec operator -(const struct timespec& l, const struct timespec& r) {
struct timespec ret;
ret=l;
ret.tv_sec-=r.tv_sec;
ret.tv_nsec-=r.tv_nsec;
while (ret.tv_nsec<0) {
ret.tv_nsec+=1000000000;
ret.tv_sec--;
}
return ret;
}
struct timespec operator +(const struct timespec& l, const struct timespec& r) {
struct timespec ret;
ret.tv_sec=l.tv_sec+r.tv_sec;
ret.tv_nsec=l.tv_nsec+r.tv_nsec;
while (ret.tv_nsec>=1000000000) {
ret.tv_nsec-=1000000000;
ret.tv_sec++;
}
return ret;
}