peaclock

A responsive and customizable clock, timer, and stopwatch for the terminal.


peaclock

/

src

/

ob

/

timer.hh

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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
#ifndef OB_TIMER_HH
#define OB_TIMER_HH

#include "ob/string.hh"

#include <ctime>

#include <tuple>
#include <chrono>
#include <string>
#include <sstream>
#include <iomanip>

namespace OB
{

class Timer
{
public:

  Timer() = default;

  operator bool()
  {
    return _is_running;
  }

  Timer& start()
  {
    _is_running = true;
    _start = std::chrono::high_resolution_clock::now();

    return *this;
  }

  Timer& stop()
  {
    update();
    _is_running = false;

    return *this;
  }

  Timer& reset()
  {
    _is_running = false;
    _total = {};

    return *this;
  }

  Timer& toggle()
  {
    if (_is_running)
    {
      update();
      _is_running = false;
    }
    else
    {
      _is_running = true;
      _start = std::chrono::high_resolution_clock::now();
    }

    return *this;
  }

  template<typename T>
  T time()
  {
    if (_is_running)
    {
      update();
    }

    return std::chrono::duration_cast<T>(_total);
  }

  std::tuple<int, int, int> diff(long int const sec)
  {
    if (_is_running)
    {
      update();
    }

    long int const now {static_cast<long int>(std::chrono::time_point_cast<std::chrono::seconds>(_total).time_since_epoch().count())};

    return seconds_to_hms(sec - now);
  }

  long int seconds()
  {
    if (_is_running)
    {
      update();
    }

    return static_cast<long int>(std::chrono::time_point_cast<std::chrono::seconds>(_total).time_since_epoch().count());
  }

  std::tuple<int, int, int> hms()
  {
    if (_is_running)
    {
      update();
    }

    long int const diff {static_cast<long int>(std::chrono::time_point_cast<std::chrono::seconds>(_total).time_since_epoch().count())};

    return seconds_to_hms(diff);
  }

  std::string str()
  {
    if (_is_running)
    {
      update();
    }

    long int const diff {static_cast<long int>(std::chrono::time_point_cast<std::chrono::seconds>(_total).time_since_epoch().count())};

    return seconds_to_string(diff);
  }

  void str(std::string const& str)
  {
    reset();
    _total = std::chrono::time_point<std::chrono::high_resolution_clock>(string_to_seconds(str));
  }

  static long int str_to_sec(std::string const& str)
  {
    return std::chrono::time_point<std::chrono::seconds>(string_to_seconds(str)).time_since_epoch().count();
  }

  static std::string sec_to_str(long int const sec)
  {
    return seconds_to_string(sec);
  }

private:

  inline static long int constexpr t_second {1};
  inline static long int constexpr t_minute {t_second * 60};
  inline static long int constexpr t_hour   {t_minute * 60};
  inline static long int constexpr t_day    {t_hour * 24};
  inline static long int constexpr t_week   {t_day * 7};
  inline static long int constexpr t_month  {static_cast<long int>(t_day * 30.4)};
  inline static long int constexpr t_year   {t_month * 12};

  static std::chrono::seconds string_to_seconds(std::string const& str)
  {
    auto const pstr = OB::String::match(str,
      std::regex("^(?:(\\d+)Y)?:?(?:(\\d+)M)?:?(?:(\\d+)W)?:?(?:(\\d+)D)?:?(?:(\\d+)h)?:?(?:(\\d+)m)?:?(?:(\\d+)s)?$"));

    if (! pstr)
    {
      return static_cast<std::chrono::seconds>(0);
    }

    auto const t = pstr.value();
    long int sec {0};

    if (! t.at(1).empty())
    {
      sec += std::stol(t.at(1)) * t_year;
    }

    if (! t.at(2).empty())
    {
      sec += std::stol(t.at(2)) * t_month;
    }

    if (! t.at(3).empty())
    {
      sec += std::stol(t.at(3)) * t_week;
    }

    if (! t.at(4).empty())
    {
      sec += std::stol(t.at(4)) * t_day;
    }

    if (! t.at(5).empty())
    {
      sec += std::stol(t.at(5)) * t_hour;
    }

    if (! t.at(6).empty())
    {
      sec += std::stol(t.at(6)) * t_minute;
    }

    if (! t.at(7).empty())
    {
      sec += std::stol(t.at(7)) * t_second;
    }

    return static_cast<std::chrono::seconds>(sec);
  }

  static std::string seconds_to_string(long int sec)
  {
    std::string res;

    auto const fuzzy_string = [&](long int const time_ref, std::string const time_str)
    {
      auto const t = (sec / time_ref);
      sec -= (t * time_ref);
      res += std::to_string(t) + time_str + ":";
    };

    if (sec >= t_year)
    {
      fuzzy_string(t_year, "Y");
    }
    if (sec >= t_month)
    {
      fuzzy_string(t_month, "M");
    }
    if (sec >= t_week)
    {
      fuzzy_string(t_week, "W");
    }
    if (sec >= t_day)
    {
      fuzzy_string(t_day, "D");
    }
    if (sec >= t_hour)
    {
      fuzzy_string(t_hour, "h");
    }
    if (sec >= t_minute)
    {
      fuzzy_string(t_minute, "m");
    }
    if (sec >= t_second)
    {
      fuzzy_string(t_second, "s");
    }
    else
    {
      res += "0s:";
    }

    res.pop_back();

    return res;
  }

  static std::tuple<int, int, int> seconds_to_hms(long int sec)
  {
    int hour {0};
    int minute {0};
    int second {0};

    auto const calc = [&](long int const time_ref)
    {
      auto const t = (sec / time_ref);
      sec -= (t * time_ref);
      return t;
    };

    if (sec > 0)
    {
      if (sec >= t_year)
      {
        calc(t_year);
      }
      if (sec >= t_month)
      {
        calc(t_month);
      }
      if (sec >= t_week)
      {
        calc(t_week);
      }
      if (sec >= t_day)
      {
        calc(t_day);
      }
      if (sec >= t_hour)
      {
        hour = calc(t_hour);
      }
      if (sec >= t_minute)
      {
        minute = calc(t_minute);
      }
      if (sec >= t_second)
      {
        second = calc(t_second);
      }
    }

    return {hour, minute, second};
  }

  void update()
  {
    auto const stop = std::chrono::high_resolution_clock::now();

    if (stop > _start)
    {
      _total += (stop - _start);
    }

    _start = stop;
  }

  bool _is_running {false};
  std::chrono::time_point<std::chrono::high_resolution_clock> _start;
  std::chrono::time_point<std::chrono::high_resolution_clock> _total;
}; // class Timer

} // namespace OB

#endif // OB_TIMER_HH
Back to Top