-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeStamp.cpp
More file actions
63 lines (51 loc) · 2 KB
/
TimeStamp.cpp
File metadata and controls
63 lines (51 loc) · 2 KB
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
//
// Created by Max on 2021-05-09.
//
#include "TimeStamp.h"
void TimeStamp::init(std::string timestamp) {
timestamp = trim(timestamp);
hours = splitTime(×tamp);
minutes = splitTime(×tamp);
seconds = splitTime(×tamp);
milliseconds = splitTime(×tamp);
}
int TimeStamp::splitTime(std::string *timestamp) {
int endPos = (timestamp->find(':') == std::string::npos) ? timestamp->find(',') : timestamp->find(':');
std::string temp = timestamp->substr(0, endPos);
timestamp->erase(0, endPos+1);
return std::stoi(temp);
}
TimeStamp &TimeStamp::operator=(const TimeStamp &other) {
if (this == &other)
return *this;
this->hours = other.hours;
this->minutes = other.minutes;
this->seconds = other.seconds;
this->milliseconds = other.milliseconds;
return *this;
}
bool TimeStamp::operator<(const TimeStamp &other) {
if (this->hours != other.hours) return this->hours < other.hours;
if (this->minutes != other.minutes) return this->minutes < other.minutes;
if (this->seconds != other.seconds) return this->seconds < other.seconds;
return this->milliseconds < other.milliseconds;
}
bool TimeStamp::operator>(const TimeStamp &other) {
if (this->hours != other.hours) return this->hours > other.hours;
if (this->minutes != other.minutes) return this->minutes > other.minutes;
if (this->seconds != other.seconds) return this->seconds > other.seconds;
return this->milliseconds > other.milliseconds;
}
std::string TimeStamp::toString() {
std::string time;
time.append(format(hours, 2)).append(":").append(format(minutes, 2))
.append(":").append(format(seconds, 2)).append(",").append(format(milliseconds, 3));
return time;
}
std::string TimeStamp::format(int n, int len) {
std::string result(len--, '0');
for (int val=(n<0)?-n:n; len>=0&&val!=0; --len,val/=10)
result[len]='0'+val%10;
if (len>=0&&n<0) result[0]='-';
return result;
}