0

I have been frustrated by the lack of a time unit in all the C/C++ std librarys (hope I've got the right wording!). What is a function that I could use. I don't care if I write it or if it is in a library.

Thanx in advance!

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by caut_baia
0
#include <ctime>
#include <iostream>
#include <string>
#include <sstream>

class timestruct   {  
    protected:
        int time_int;
        std::string time_str;
    public:
        timestruct()   {
           std::time_t rawtime;
           std::time(&rawtime);
           struct std::tm* ptm=std::localtime(&rawtime);
           time_int=ptm->tm_hour*100+ptm->tm_min;
                                
           std::ostringstream buff;
           buff << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec;      
           time_str=buff.str();
         }
        timestruct& operator= (const timestruct& t);
        friend std::ostream& operator << (std::ostream& out,const timestruct& tm)  {           
           return out << tm;
         }

        operator int() const  {
            return time_int;
         }

        operator std::string()  {
            return time_str;
         }
   };

int main ()  {
        int n=timestruct();
        std::string s=timestruct();
      }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.