elanhickler

Untitled

Jan 11th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. TimeString timestr;
  2.  
  3. // trying to get one of these to work
  4. bool check = timestr("the_string").is_valid
  5. bool check = timestr("the_string")->is_valid
  6. bool check = timestr("the_string")
  7.  
  8. // here is the class
  9. class TimeString {
  10. private:
  11.     vector<string> str_vec;
  12.     int h, m;
  13.     double s, total_sec;
  14.     string timestr;
  15.  
  16.     void to_timestr(const string& str) {
  17.         if (timestr == str) return;
  18.         is_valid = true;
  19.         str_vec = split(str, ':', 3, "0");
  20.         if (is_number(str_vec[2])) {
  21.             h = ToI(str_vec[2]);
  22.             total_sec += h * 3600;
  23.         }
  24.         else is_valid = false; return;
  25.  
  26.         if (is_number(str_vec[1])) {
  27.             m = ToI(str_vec[1]);
  28.             total_sec += m * 60;
  29.         }
  30.         else is_valid = false; return;
  31.  
  32.         if (is_number(str_vec[2])) {
  33.             s = ToI(str_vec[2]);
  34.             total_sec += s;
  35.         }
  36.         else is_valid = false; return;
  37.         timestr.clear();
  38.  
  39.         return;
  40.     }
  41.  
  42.     void to_timestr(double sec) {
  43.         is_valid = true;
  44.         str_vec.resize(3, "0");
  45.         total_sec = sec;
  46.  
  47.         h = sec / 3600;
  48.         s = sec - h * 3600;
  49.         m = s / 60;
  50.         s = sec - m * 60;
  51.         timestr.pop_back();
  52.     }
  53.  
  54. public:
  55.     TimeString() {};
  56.  
  57.     bool is_valid = false;
  58.  
  59.     TimeString(const string& str) { to_timestr(str); }
  60.  
  61.     TimeString operator=(const char* str) {
  62.         to_timestr(str);
  63.         return *this;
  64.     }
  65.  
  66.     //TimeString operator=(double sec) {
  67.     //  to_timestr(sec);
  68.     //  return *this;
  69.     //}
  70.  
  71.     //operator const string() {
  72.     //  if (!is_valid) return "";
  73.     //  if (timestr.size()) return timestr;
  74.     //  if (h) timestr += ToS(h) + ":";
  75.     //  if (m) timestr += ToS(m) + ":";
  76.     //  if (s) timestr += ToS(s) + ":";
  77.     //  return *this;
  78.     //}
  79.  
  80.     //operator const double() { return total_sec; }
  81.  
  82.     //operator const bool() { return is_valid; }
  83. };
Advertisement
Add Comment
Please, Sign In to add comment