00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include <cstdio>
00021 #include <ctime>
00022 #include <boost/date_time/posix_time/posix_time.hpp>
00023 #include <boost/thread/xtime.hpp>
00024 #undef assert
00025 #define assert MONGO_assert
00026
00027 namespace mongo {
00028
00029 inline void time_t_to_Struct(time_t t, struct tm * buf , bool local = false ) {
00030 #if defined(_WIN32)
00031 if ( local )
00032 localtime_s( buf , &t );
00033 else
00034 gmtime_s(buf, &t);
00035 #else
00036 if ( local )
00037 localtime_r(&t, buf);
00038 else
00039 gmtime_r(&t, buf);
00040 #endif
00041 }
00042
00043
00044
00045 inline string terseCurrentTime(bool colonsOk=true) {
00046 struct tm t;
00047 time_t_to_Struct( time(0) , &t );
00048
00049 const char* fmt = (colonsOk ? "%Y-%m-%dT%H:%M:%S" : "%Y-%m-%dT%H-%M-%S");
00050 char buf[32];
00051 assert(strftime(buf, sizeof(buf), fmt, &t) == 19);
00052 return buf;
00053 }
00054
00055 inline boost::gregorian::date currentDate() {
00056 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
00057 return now.date();
00058 }
00059
00060
00061 inline bool toPointInTime( const string& str , boost::posix_time::ptime* timeOfDay ) {
00062 int hh = 0;
00063 int mm = 0;
00064 if ( 2 != sscanf( str.c_str() , "%d:%d" , &hh , &mm ) ) {
00065 return false;
00066 }
00067
00068
00069 if ( ( hh / 24 ) || ( mm / 60 ) ) {
00070 return false;
00071 }
00072
00073 boost::posix_time::ptime res( currentDate() , boost::posix_time::hours( hh ) + boost::posix_time::minutes( mm ) );
00074 *timeOfDay = res;
00075 return true;
00076 }
00077
00078 #define MONGO_asctime _asctime_not_threadsafe_
00079 #define asctime MONGO_asctime
00080 #define MONGO_gmtime _gmtime_not_threadsafe_
00081 #define gmtime MONGO_gmtime
00082 #define MONGO_localtime _localtime_not_threadsafe_
00083 #define localtime MONGO_localtime
00084 #define MONGO_ctime _ctime_is_not_threadsafe_
00085 #define ctime MONGO_ctime
00086
00087 #if defined(_WIN32)
00088 inline void sleepsecs(int s) {
00089 Sleep(s*1000);
00090 }
00091 inline void sleepmillis(long long s) {
00092 assert( s <= 0xffffffff );
00093 Sleep((DWORD) s);
00094 }
00095 inline void sleepmicros(long long s) {
00096 if ( s <= 0 )
00097 return;
00098 boost::xtime xt;
00099 boost::xtime_get(&xt, boost::TIME_UTC);
00100 xt.sec += (int)( s / 1000000 );
00101 xt.nsec += (int)(( s % 1000000 ) * 1000);
00102 if ( xt.nsec >= 1000000000 ) {
00103 xt.nsec -= 1000000000;
00104 xt.sec++;
00105 }
00106 boost::thread::sleep(xt);
00107 }
00108 #elif defined(__sunos__)
00109 inline void sleepsecs(int s) {
00110 boost::xtime xt;
00111 boost::xtime_get(&xt, boost::TIME_UTC);
00112 xt.sec += s;
00113 boost::thread::sleep(xt);
00114 }
00115 inline void sleepmillis(long long s) {
00116 boost::xtime xt;
00117 boost::xtime_get(&xt, boost::TIME_UTC);
00118 xt.sec += (int)( s / 1000 );
00119 xt.nsec += (int)(( s % 1000 ) * 1000000);
00120 if ( xt.nsec >= 1000000000 ) {
00121 xt.nsec -= 1000000000;
00122 xt.sec++;
00123 }
00124 boost::thread::sleep(xt);
00125 }
00126 inline void sleepmicros(long long s) {
00127 if ( s <= 0 )
00128 return;
00129 boost::xtime xt;
00130 boost::xtime_get(&xt, boost::TIME_UTC);
00131 xt.sec += (int)( s / 1000000 );
00132 xt.nsec += (int)(( s % 1000000 ) * 1000);
00133 if ( xt.nsec >= 1000000000 ) {
00134 xt.nsec -= 1000000000;
00135 xt.sec++;
00136 }
00137 boost::thread::sleep(xt);
00138 }
00139 #else
00140 inline void sleepsecs(int s) {
00141 struct timespec t;
00142 t.tv_sec = s;
00143 t.tv_nsec = 0;
00144 if ( nanosleep( &t , 0 ) ) {
00145 cout << "nanosleep failed" << endl;
00146 }
00147 }
00148 inline void sleepmicros(long long s) {
00149 if ( s <= 0 )
00150 return;
00151 struct timespec t;
00152 t.tv_sec = (int)(s / 1000000);
00153 t.tv_nsec = 1000 * ( s % 1000000 );
00154 struct timespec out;
00155 if ( nanosleep( &t , &out ) ) {
00156 cout << "nanosleep failed" << endl;
00157 }
00158 }
00159 inline void sleepmillis(long long s) {
00160 sleepmicros( s * 1000 );
00161 }
00162 #endif
00163
00164
00165 inline int tdiff(unsigned told, unsigned tnew) {
00166 return WrappingInt::diff(tnew, told);
00167 }
00168
00170 inline unsigned curTimeMillis() {
00171 boost::xtime xt;
00172 boost::xtime_get(&xt, boost::TIME_UTC);
00173 unsigned t = xt.nsec / 1000000;
00174 return (xt.sec & 0xfffff) * 1000 + t;
00175 }
00176
00178 inline Date_t jsTime() {
00179 boost::xtime xt;
00180 boost::xtime_get(&xt, boost::TIME_UTC);
00181 unsigned long long t = xt.nsec / 1000000;
00182 return ((unsigned long long) xt.sec * 1000) + t;
00183 }
00184
00185 inline unsigned long long curTimeMicros64() {
00186 boost::xtime xt;
00187 boost::xtime_get(&xt, boost::TIME_UTC);
00188 unsigned long long t = xt.nsec / 1000;
00189 return (((unsigned long long) xt.sec) * 1000000) + t;
00190 }
00191
00192
00193 inline unsigned curTimeMicros() {
00194 boost::xtime xt;
00195 boost::xtime_get(&xt, boost::TIME_UTC);
00196 unsigned t = xt.nsec / 1000;
00197 unsigned secs = xt.sec % 1024;
00198 return secs*1000000 + t;
00199 }
00200
00201 }