elanhickler

timestring class

Jan 13th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. /*** HPP ***/
  2. class TimeString {
  3. private:
  4.     vector<string> str_vec;
  5.     int _h = 0;
  6.     int _m = 0;
  7.     double _s = 0;
  8.     double total_sec = 0;
  9.     bool is_valid = false;
  10.     string timestr;
  11.  
  12.     void TimeString::to_timestr(const string& str);
  13.     void TimeString::to_timestr(double sec);
  14.     void TimeString::build_string();
  15.  
  16. public:
  17.     // constructor
  18.     TimeString();
  19.     TimeString(const string& str);
  20.     TimeString(double sec);
  21.  
  22.     // assignment
  23.     TimeString operator=(const char* str);
  24.     bool operator()(string str);
  25.  
  26.     // conversion
  27.     operator const string();
  28.     operator const double();
  29.     operator const bool();
  30.  
  31.     // comparison
  32.     bool operator<(double b);
  33.     bool operator>(double b);
  34.  
  35.     // getters
  36.     double h();
  37.     double m();
  38.     double s();
  39. };
  40.  
  41. /*** CPP ***/
  42. void TimeString::to_timestr(const string& str) {
  43.     if (str.empty()) return;
  44.     if (timestr == str) return;
  45.     timestr = str;
  46.     is_valid = true;
  47.     str_vec = split(str, ':');
  48.     auto i = str_vec.end();
  49.  
  50.     // get seconds
  51.     if (is_number(*--i)) {
  52.         _s = ToI(*i);
  53.         total_sec += _s;
  54.     }
  55.     else is_valid = false;
  56.  
  57.     if (is_valid == false || i == str_vec.begin()) return;
  58.  
  59.     // get minutes
  60.     if (is_number(*--i)) {
  61.         _m = ToI(*i);
  62.         total_sec += _m * 60;
  63.     }
  64.     else is_valid = false;
  65.  
  66.     if (is_valid == false || i == str_vec.begin()) return;
  67.  
  68.     // get hours
  69.     if (is_number(*--i)) {
  70.         _h = ToI(*i);
  71.         total_sec += _h * 3600;
  72.     }
  73.     else is_valid = false;
  74.  
  75.     return;
  76. }
  77.  
  78. void TimeString::to_timestr(double sec) {
  79.     is_valid = true;
  80.     str_vec.resize(3, "0");
  81.     total_sec = sec;
  82.  
  83.     _h = sec / 3600;
  84.     _s = sec - _h * 3600;
  85.     _m = _s / 60;
  86.     _s = sec - _m * 60;
  87. }
  88.  
  89. void TimeString::build_string() {
  90.     if (!is_valid) return;
  91.     if (timestr.size()) return;
  92.     if (_h) timestr += ToS(_h) + ":";
  93.     if (_m) timestr += ToS(_m) + ":";
  94.     if (_s) timestr += ToS(_s) + ":";
  95.     timestr.pop_back();
  96. }
  97.  
  98. TimeString TimeString::operator=(const char * str) {
  99.     to_timestr(str);
  100.     return *this;
  101. }
  102.  
  103. bool TimeString::operator()(string str) {
  104.     to_timestr(str);
  105.     return is_valid;
  106. }
  107.  
  108. TimeString::operator const string() { build_string(); return timestr; }
  109. TimeString::operator const double() { return total_sec; }
  110. TimeString::operator const bool() { return is_valid; }
  111.  
  112. bool TimeString::operator<(double b) { return total_sec < b; }
  113. bool TimeString::operator>(double b) { return total_sec > b; }
  114.  
  115. TimeString::TimeString() {};
  116. TimeString::TimeString(const string& str) { to_timestr(str); }
  117. TimeString::TimeString(double sec) { to_timestr(sec); }
  118.  
  119. double TimeString::h() { return _h; }
  120. double TimeString::m() { return _m; }
  121. double TimeString::s() { return _s; }
Advertisement
Add Comment
Please, Sign In to add comment