-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimeKeeper.cpp
213 lines (191 loc) · 7.2 KB
/
TimeKeeper.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// TimeKeeper v0.02 Wayne H Nixalo 2017 - for UNIX systems
// 2017-Jun-29 17:18
#include <iostream> // Console I/O
#include <chrono> // Time functions
// #include <unistd.h> // UNIX pause execution (debugging)
#include <ctime> // Date-Time
#include <fstream> // File I/O
#include <string> // File search
void checkTime(std::string dateString);
int main() {
// open/create file; std::ios::app <-- start at end of file
std::fstream timelog;
timelog.open("timelog.txt", std::ios::app);
if (!timelog.is_open()) {
std::cout << "Error opening `timelog.txt`" << std::endl;
return 1;
}
// timer variables
auto start = std::chrono::steady_clock::now();
auto end = std::chrono::steady_clock::now();
double duration = 0;
std::chrono::duration<double> difference;
std::time_t time_today = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now());
std::string dateString = std::string(std::ctime(&time_today), 0, 10);
// program state
int MODE = 0;
std::cout << "TimeKeeper v0.01; Wayne Nixalo 2017" << std::endl;
// prompt for input
std::cout << "Enter `s` to start counter, `e` to pause, `q` to quit, or ";
std::cout << " `t`/`d` to display elapsed time" << std::endl;
char input = ' ';
while (std::tolower(input) != 'q') {
std::cout << "start:s | stop:e | quit:q >> ";
std::cin >> input;
// Start timer
if (std::tolower(input) == 's') {
if (!MODE) {
start = std::chrono::steady_clock::now();
MODE = 1;
std::cout << "Timer: ON" << std::endl;
} else {
std::cout << " < Timer already running > " << std::endl;
}
}
// Stop timer
if (std::tolower(input) == 'e') {
if (MODE) {
end = std::chrono::steady_clock::now();
difference = end - start;
duration += difference.count();
MODE = 0;
std::cout << "Timer: OFF" << std::endl;
} else {
std::cout << " < Timer already stopped > " << std::endl;
}
}
// Quit
if (std::tolower(input) == 'q') {
// record final time if running
if (MODE) {
end = std::chrono::steady_clock::now();
difference = end - start;
duration += difference.count();
}
// save time to file
// use system_clock to get ctime date converstion
std::chrono::time_point<std::chrono::system_clock> end;
end = std::chrono::system_clock::now();
int H = (int)(duration / 3600), M = (int)((duration - H*3600) / 60);
double S = (duration - H*3600 - M*60);
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
timelog << "Time Log: " << std::ctime(&end_time) << " - ";
timelog << H << "h" << M << "m " << S << "s";
timelog << " | " << duration << "\n";
timelog.close();
std::cout << "Time Logged: " << duration << " | ";
std::cout << H << "h" << M << "m " << S << "s" <<std::endl;
std::cout << "Written to file: timelog.txt" << std::endl;
checkTime(dateString);
}
// Display time
if (std::tolower(input) == 't' || std::tolower(input) == 'd') {
end = std::chrono::steady_clock::now();
difference = end - start;
std::cout << "Time Elapsed: " << duration + difference.count();
std::cout << " seconds" << std::endl;
}
}
return 0;
}
/* tallies up time logged for a particular day */
void checkTime(std::string dateString = "") {
// if no date string input default to today
if (dateString == "") {
std::time_t time_today = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now());
std::string dateString = std::string(std::ctime(&time_today), 0, 10);
}
dateString = "Time Log: " + dateString;
// open file
std::ifstream timelog;
timelog.open("timelog.txt");
if (!timelog.is_open()) {
std::cout << "Error opening `timelog.txt`" << std::endl;
return;
}
double total = -1;
std::string buffer;
// check if date logged
while (!timelog.eof()) { // NOTE: could this give an off-by-1 error?
std::getline(timelog, buffer);
buffer = buffer.substr(0, 20);
if (dateString.compare(buffer) == 0) {
total = 0;
break;
}
}
if (total < 0) {
std::cout << dateString << " not found in log." << std::endl;
timelog.close();
return;
}
while(!timelog.eof() || dateString.compare(buffer) == 0) {
// find duratin(seconds) & add to total
std::getline(timelog, buffer);
int idx = buffer.find_first_of("|");
idx += 2; // duration starts a space over from `|`
buffer = buffer.substr(idx, 12); // will use up to 12 digits+point
total += std::stod(buffer); // string to double
// go to next dated line
std::getline(timelog, buffer);
buffer = buffer.substr(0, 20);
}
timelog.close();
int H = (int)(total/3600); int M = (int)((total - H*3600)/60);
dateString.erase(0,10);
std::cout << "Total time logged for " << dateString << ": " << total;
std::cout << " seconds" << std::endl;
std::cout << " ";
std::cout << H << ":" << M << ":" << total - H*3600 - M*60 << std::endl;
}
/* Change Log:
v0.02: 2017-Jun-29 17:18 - displays total logged time for given day
v0.01: 2017-Jun-28 14:37-51 - basic functionality
*/
// NOTE: apparently this doesn't work:
// if (!timelog.open("timelog.txt", std::ios::app)) {
// printf("Error openning file\n");
// return 1;
// }
// 2017-Jun-28 14:37-51 figuring out how to use time functions:
// #include <iostream>
// #include <chrono>
// #include <unistd.h>
//
// int main() {
// auto start = std::chrono::steady_clock::now();
//
// usleep(1e6);
//
// auto end = std::chrono::steady_clock::now();
//
// // std::cout << end - start << std::endl;
//
// std::chrono::duration<double> diff = end - start;
//
// std::cout << diff.count() << std::endl;
// }
// 2017-Jun-29 15:20 figuring out how to find things
// #include <chrono>
// #include <ctime>
// #include <iostream>
// #include <typeinfo>
// #include <string>
//
// int main() {
// auto now = std::chrono::system_clock::now();
// // std::cout << typeid(now).name() << std::endl;
// std::time_t time_now = std::chrono::system_clock::to_time_t(now);
// std::cout << std::ctime(&time_now);
// std::string datetime = std::ctime(&time_now);
// std::cout << datetime << std::endl;
// }
// more time function stuff
// string (const string& str, size_t pos, size_t len = npos);
// std::time_t time_today = std::chrono::system_clock::to_time_t(
// std::chrono::system_clock::now());
// // std::string datetime = std::ctime(&time_today);
//
// std::string datetime = std::string(std::ctime(&time_today), 0, 10);