inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/sys/atime
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_sys_atime
00005 #define inlib_sys_atime
00006 
00007 #ifdef WIN32
00008 #include <sys/timeb.h>
00009 #else
00010 #include <sys/time.h>
00011 #endif
00012 
00013 #include <sstream>
00014 #include "../typedefs"
00015 
00016 namespace inlib {
00017 
00018 class atime {
00019 public:
00020   typedef uint64 num_t;
00021 public:
00022   static atime now() {
00023 #ifdef WIN32
00024     struct timeb tb;
00025     ::ftime(&tb);
00026     return atime((num_t)tb.time,(num_t)(tb.millitm * 1000));
00027 #else
00028     struct timeval tval;
00029     ::gettimeofday(&tval,0);
00030     return atime((num_t)tval.tv_sec,(num_t)tval.tv_usec);
00031 #endif
00032   }
00033   static atime elapsed(const atime& a_from){
00034     atime n = now();
00035 
00036     num_t f_all_mcs = a_from.m_secs * 1000000 + a_from.m_micro_secs;
00037     num_t n_all_mcs = n.m_secs * 1000000 + n.m_micro_secs;
00038 
00039     num_t d_all_mcs = n_all_mcs-f_all_mcs;
00040 
00041     num_t d_s = d_all_mcs/1000000;
00042     num_t d_mcs = d_all_mcs-d_s*1000000;
00043 
00044     return atime(d_s,d_mcs);
00045   }
00046 public:
00047   atime(num_t a_secs,num_t a_micro_secs)
00048   :m_secs(a_secs),m_micro_secs(a_micro_secs){}
00049   virtual ~atime(){}
00050 public:
00051   atime(const atime& a_from)
00052   :m_secs(a_from.m_secs),m_micro_secs(a_from.m_micro_secs){}
00053   atime& operator=(const atime& a_from){
00054     m_secs = a_from.m_secs;
00055     m_micro_secs = a_from.m_micro_secs;
00056     return *this;
00057   }
00058 public:
00059   num_t seconds() const {return m_secs;}
00060   num_t micro_seconds() const {return m_micro_secs;}
00061   double value() const {
00062     return ((double)m_secs) + ((double)m_micro_secs)/1000000.0; 
00063   }
00064   std::string time_string() const { 
00065     std::ostringstream strm;
00066     strm << m_secs;
00067     strm << " secs ";
00068     strm << m_micro_secs;
00069     strm << " micro_secs";
00070     return strm.str();
00071   }
00072 protected:
00073   num_t m_secs;
00074   num_t m_micro_secs;
00075 };
00076 
00077 }
00078 
00079 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines