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!

Recommended Answers

All 2 Replies

#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();
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.