-
Notifications
You must be signed in to change notification settings - Fork 0
/
human_readable_duration_format.cpp
95 lines (74 loc) · 2.43 KB
/
human_readable_duration_format.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
/*
* Problem Link https://www.codewars.com/kata/52742f58faf5485cae000b9a/train/cpp
*/
#define YEAR_SECONDS ((365*24*60*60))
#define MONTH_SECONDS ((30*24*60*60))
#define DAY_SECONDS ((24*60*60))
#define HOUR_SECONDS ((60*60))
#define MINUTE_SECONDS ((60))
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string add_s(int num) {
if (num > 1)
return "s";
return "";
}
string date_to_string(int n, string name) {
if (!n)
return "none";
return to_string(n)+" "+name+add_s(n);
}
std::string format_duration(int seconds) {
if (seconds == 0)
return "now";
int years = seconds / YEAR_SECONDS;
seconds %= YEAR_SECONDS;
/*
int months = seconds / MONTH_SECONDS;
seconds %= MONTH_SECONDS;
*/
int months = 0;
int days = seconds / DAY_SECONDS;
seconds %= DAY_SECONDS;
int hours = seconds / HOUR_SECONDS;
seconds %= HOUR_SECONDS;
int minutes = seconds / MINUTE_SECONDS;
seconds %= MINUTE_SECONDS;
int num_of_date_types = (bool)years+(bool)months+(bool)days+(bool)hours+(bool)minutes+(bool)seconds;
vector<string> result;
result.push_back(date_to_string(years, "year"));
result.push_back(date_to_string(months, "month"));
result.push_back(date_to_string(days, "day"));
result.push_back(date_to_string(hours, "hour"));
result.push_back(date_to_string(minutes, "minute"));
result.push_back(date_to_string(seconds, "second"));
vector<string> final_result;
copy_if(result.begin(), result.end(), back_inserter(final_result), [](string tmp){return tmp!="none";});
if (num_of_date_types>1) {
final_result.insert(final_result.end()-1, "and");
if (num_of_date_types > 2) {
for (int i = 0; i < final_result.size() - 3; ++i) {
//if (final_result[i] != "")
final_result[i] += ",";
}
}
}
string final_string;
for (int i=0; i < final_result.size()-1; ++i) {
//if (final_result[i] != "")
final_string += final_result[i]+" ";
}
//if (final_result[final_result.size()-1] != "")
final_string += final_result[final_result.size()-1];
return final_string;
}
int main() {
cout << format_duration(0) << endl;
cout << format_duration(1) << endl;
cout << format_duration(62) << endl;
cout << format_duration(120) << endl;
cout << format_duration(3662) << endl;
}