/
Time.cpp
76 lines (57 loc) · 1.8 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
#include "Time.h"
Time::Time(std::string time_string){
this->init_time = parseTime(time_string);
this->elapsed_time = 0;
}
void Time::elapse(unsigned int time){
this->elapsed_time += time;
}
void Time::elapse(std::string time_string){
this->elapsed_time += parseTime(time_string);
}
unsigned int Time::getElapsedTime(){
return this->elapsed_time;
}
std::string Time::prettyCurrentTime(){
int SECS_IN_DAY = 86400;
int SECS_IN_HOUR = 3600;
int SECS_IN_MIN = 60;
int MINS_IN_HOUR = 60;
unsigned int hours = 0;
unsigned int minutes = 0;
unsigned int seconds = 0;
unsigned int days = 1;
bool am = (this->init_time / SECS_IN_HOUR) < 12;
seconds = this->init_time % SECS_IN_MIN + this->elapsed_time % SECS_IN_MIN;
if (seconds >= SECS_IN_MIN){
seconds -= SECS_IN_MIN;
minutes++;
}
minutes += (this->init_time / MINS_IN_HOUR) % MINS_IN_HOUR +
(this->elapsed_time / MINS_IN_HOUR) % MINS_IN_HOUR;
if (minutes >= MINS_IN_HOUR){
minutes -= MINS_IN_HOUR;
hours++;
}
hours += (this->init_time / SECS_IN_HOUR) +
(this->elapsed_time / SECS_IN_HOUR) % 24;
am = (hours / 12) % 2 == 0;
hours %= 12;
if (hours == 0) hours = 12;
days += (this->init_time + this->elapsed_time) / SECS_IN_DAY;
std::string am_string;
std::string minute_string;
std::string second_string;
if (am) am_string = "am";
else am_string = "pm";
if (minutes < 10) minute_string = "0" + std::to_string(minutes);
else minute_string = std::to_string(minutes);
if (seconds < 10) second_string = "0" + std::to_string(seconds);
else second_string = std::to_string(seconds);
std::string pretty_current_time = "[Day " + std::to_string(days) + ": " +
std::to_string(hours) + ":" +
minute_string + ":" +
second_string +
am_string + "]";
return pretty_current_time;
}