summaryrefslogtreecommitdiff
path: root/src/utils/chrono.cpp
blob: 64ad32aad42845df3d4f2fc894c906deec909b68 (plain)
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
#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";
    }
  }
}