-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc++_time.cpp
72 lines (60 loc) · 1.45 KB
/
c++_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
#include <ctime>
#include <iostream>
#include <map>
#include <string>
// #pragma warning(disable : 4996) // To prevent conflicts
const std::map<std::string, int> arguments_table = {
{"now", 1}, {"zero", 2}, {"from_now", 3}};
void PrintTime(const std::time_t &time) {
std::cout << "time_t: " << time << "\n";
std::cout << "ctime: " << ctime(&time) << std::endl;
}
void UsageMessage() {
std::cout << "Usage: pass as argument one of (now, zero, from_now)"
<< std::endl;
}
void TimeNow_modif(const bool mod) {
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
if (mod) {
int plus_hour = 0;
int plus_minute = 0;
int plus_days = 0;
std::cout << "plus days: ";
std::cin >> plus_days;
std::cout << "plus hours: ";
std::cin >> plus_hour;
std::cout << "plus minutes: ";
std::cin >> plus_minute;
std::cout << "\n";
tm.tm_mday += plus_days;
tm.tm_hour += plus_hour;
tm.tm_min += plus_minute;
}
std::time_t time = mktime(&tm);
PrintTime(time);
}
void ZeroTime() {
std::time_t time = 0;
PrintTime(time);
}
int main(int argc, char *argv[]) {
if (argc != 2 || !arguments_table.count(std::string(argv[1]))) {
UsageMessage();
return 1;
}
switch (arguments_table.at(std::string(argv[1]))) {
case 1:
TimeNow_modif(false);
break;
case 2:
ZeroTime();
break;
case 3:
TimeNow_modif(true);
break;
default:
return 1;
}
return 0;
}