summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-05-01 00:14:07 +0500
committerilotterytea <iltsu@alright.party>2024-05-01 00:14:07 +0500
commitf9859164676f8fc3fa174d6c351308e6acc2b369 (patch)
treec1337a85ac452b85d6c10e918ddc5b6dce8bba11
parentd45c3e8b9ba05f8df527a5d43a50a84fcbb73600 (diff)
feat: method for humanizing timestamps
-rw-r--r--src/utils/chrono.cpp30
-rw-r--r--src/utils/chrono.hpp7
2 files changed, 37 insertions, 0 deletions
diff --git a/src/utils/chrono.cpp b/src/utils/chrono.cpp
new file mode 100644
index 0000000..64ad32a
--- /dev/null
+++ b/src/utils/chrono.cpp
@@ -0,0 +1,30 @@
+#include "chrono.hpp"
+
+#include <cmath>
+#include <string>
+
+namespace bot::utils::chrono {
+ std::string format_timestamp(int seconds) {
+ int d = round(seconds / (60 * 60 * 24));
+ int h = round(seconds / (60 * 60) % 24);
+ int m = round(seconds % (60 * 60) / 60);
+ int s = round(seconds % 60);
+
+ // Only seconds:
+ if (d == 0 && h == 0 && m == 0) {
+ return std::to_string(s) + "s";
+ }
+ // Minutes and seconds:
+ else if (d == 0 && h == 0) {
+ return std::to_string(m) + "m" + std::to_string(s) + "s";
+ }
+ // Hours and minutes:
+ else if (d == 0) {
+ return std::to_string(h) + "h" + std::to_string(m) + "m";
+ }
+ // Days and hours:
+ else {
+ return std::to_string(d) + "d" + std::to_string(h) + "h";
+ }
+ }
+}
diff --git a/src/utils/chrono.hpp b/src/utils/chrono.hpp
new file mode 100644
index 0000000..5d0b3ed
--- /dev/null
+++ b/src/utils/chrono.hpp
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <string>
+
+namespace bot::utils::chrono {
+ std::string format_timestamp(int seconds);
+}