Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TimeString timestr;
- // trying to get one of these to work
- bool check = timestr("the_string").is_valid
- bool check = timestr("the_string")->is_valid
- bool check = timestr("the_string")
- // here is the class
- class TimeString {
- private:
- vector<string> str_vec;
- int h, m;
- double s, total_sec;
- string timestr;
- void to_timestr(const string& str) {
- if (timestr == str) return;
- is_valid = true;
- str_vec = split(str, ':', 3, "0");
- if (is_number(str_vec[2])) {
- h = ToI(str_vec[2]);
- total_sec += h * 3600;
- }
- else is_valid = false; return;
- if (is_number(str_vec[1])) {
- m = ToI(str_vec[1]);
- total_sec += m * 60;
- }
- else is_valid = false; return;
- if (is_number(str_vec[2])) {
- s = ToI(str_vec[2]);
- total_sec += s;
- }
- else is_valid = false; return;
- timestr.clear();
- return;
- }
- void to_timestr(double sec) {
- is_valid = true;
- str_vec.resize(3, "0");
- total_sec = sec;
- h = sec / 3600;
- s = sec - h * 3600;
- m = s / 60;
- s = sec - m * 60;
- timestr.pop_back();
- }
- public:
- TimeString() {};
- bool is_valid = false;
- TimeString(const string& str) { to_timestr(str); }
- TimeString operator=(const char* str) {
- to_timestr(str);
- return *this;
- }
- //TimeString operator=(double sec) {
- // to_timestr(sec);
- // return *this;
- //}
- //operator const string() {
- // if (!is_valid) return "";
- // if (timestr.size()) return timestr;
- // if (h) timestr += ToS(h) + ":";
- // if (m) timestr += ToS(m) + ":";
- // if (s) timestr += ToS(s) + ":";
- // return *this;
- //}
- //operator const double() { return total_sec; }
- //operator const bool() { return is_valid; }
- };
Advertisement
Add Comment
Please, Sign In to add comment