TCTime.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _TC_TIME_H_
00037 #define _TC_TIME_H_
00038
00039 #include "TCTypes.h"
00040
00041 namespace TC
00042 {
00058 class TCBASE_API Time
00059 {
00060 public:
00062 static Time FromSeconds(uint64 seconds);
00064 static Time FromMilliSeconds(uint64 milli_seconds);
00066 static Time FromMicroSeconds(uint64 micro_seconds);
00068 static Time FromNanoSeconds(uint64 nanos_seconds);
00070 static Time Now();
00072 static Time Since(const Time& start_time)
00073 {
00074 return Now() -= start_time;
00075 }
00076
00078 uint64 Seconds() const {return m_secs;}
00080 uint64 NanoSeconds() const {return m_nsecs;}
00081
00083 uint64 ToMilliSeconds() const
00084 {
00085 return m_secs * 1000 + m_nsecs / 1000000;
00086 }
00087
00089 uint64 ToMicroSeconds() const
00090 {
00091 return m_secs * 1000000 + m_nsecs / 1000;
00092 }
00093
00095 uint64 ToNanoSeconds() const
00096 {
00097 return m_secs * 1000000000 + m_nsecs;
00098 }
00099
00100 Time& operator+=(const Time& time_to_add)
00101 {
00102 m_secs += time_to_add.m_secs;
00103 m_nsecs += time_to_add.m_nsecs;
00104 if (m_nsecs >= 1000000000)
00105 {
00106 m_secs++;
00107 m_nsecs -= 1000000000;
00108 }
00109 return *this;
00110 }
00111
00112 Time& operator-=(const Time& time_to_add)
00113 {
00114 m_secs -= time_to_add.m_secs;
00115 if (m_nsecs < time_to_add.m_nsecs)
00116 {
00117 m_secs--;
00118 m_nsecs += 1000000000;
00119 }
00120 m_nsecs -= time_to_add.m_nsecs;
00121 return *this;
00122 }
00123
00124 bool operator==(const Time& a)
00125 {
00126 return (m_secs == a.m_secs) && (m_nsecs == a.m_nsecs);
00127 }
00128
00129 bool operator!=(const Time& a)
00130 {
00131 return !this->operator==(a);
00132 }
00133
00134 private:
00135 uint64 m_secs;
00136 uint64 m_nsecs;
00137 };
00138
00139 inline const Time operator+(const Time& a, const Time& b)
00140 {
00141 return Time(a) += b;
00142 }
00143
00144 inline const Time operator-(const Time& a, const Time& b)
00145 {
00146 return Time(a) -= b;
00147 }
00148 }
00149
00150 #endif // _TC_TIME_H_
00151