00001
00006 #include "ctl/ctldef.h"
00007 #include "ctl/mstime.h"
00008 #ifdef WIN32
00009 #include <windows.h>
00010 #endif
00011 #include <time.h>
00012 #include <string.h>
00013 #include <stdio.h>
00014 #include <errno.h>
00015
00024 ustime ctl_ustime(void)
00025 {
00026 #if defined(WIN32)
00027 static long long usresolution = 0;
00028 long long ustime;
00029
00030 if( 0 == usresolution )
00031 {
00032 QueryPerformanceFrequency((LARGE_INTEGER*)&usresolution);
00033 usresolution /= 1000000;
00034 }
00035 QueryPerformanceCounter((LARGE_INTEGER*)&ustime);
00036 return ustime / usresolution;
00037 #elif defined(__GNUC__)
00038 #ifdef __CYGWIN__
00039 #if CLOCKS_PER_SEC == 1000
00040 #define USCLOCK() ((clock()%1000)*1000)
00041 #else
00042 #define USCLOCK() ((ustime)(clock()%CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC)
00043 #endif
00044 time_t s = time(NULL);
00045 ustime us = USCLOCK();
00046 time_t s2 = time(NULL);
00047 if( s2 == s )
00048 return ((ustime)s * 1000000) + us;
00049 us = USCLOCK();
00050 return ((ustime)s2 * 1000000) + us;
00051 #else
00052 struct timespec ts;
00053 clock_gettime( CLOCK_REALTIME, &ts );
00054 return ((ustime)ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
00055 #endif
00056 #endif
00057 }
00058
00065 int ctl_ustime_print_seconds( char* buff, size_t bufflen, ustime us )
00066 {
00067 return snprintf( buff, bufflen, "%lld.%06lld", us/1000000, us%1000000 );
00068 }
00069
00070
00079 mstime ctl_mstime(void)
00080 {
00081 #ifdef WIN32
00082 SYSTEMTIME systime;
00083 FILETIME ftime;
00084 mstime now;
00085 GetSystemTime( &systime );
00086 SystemTimeToFileTime( &systime, &ftime );
00087 now = ((mstime)ftime.dwHighDateTime << 32) | ftime.dwLowDateTime;
00088 now -= int64const(116444736000000000);
00089 now /= 10000;
00090 return now;
00091 #else
00092 #ifdef __GNUC__
00093 #ifdef __CYGWIN__
00094 #if CLOCKS_PER_SEC == 1000
00095 #define MSCLOCK() (clock()%1000)
00096 #else
00097 #define MSCLOCK() ((mstime)(clock()%CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC)
00098 #endif
00099 time_t s = time(NULL);
00100 mstime ms = MSCLOCK();
00101 time_t s2 = time(NULL);
00102 if( s2 == s )
00103 return ((mstime)s * 1000) + ms;
00104 ms = MSCLOCK();
00105 return ((mstime)s2 * 1000) + ms;
00106 #else
00107 struct timespec ts;
00108 clock_gettime( CLOCK_REALTIME, &ts );
00109 return ((mstime)ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
00110 #endif
00111 #endif
00112 #endif
00113 }
00114
00121 int ctl_mstime_print_seconds( char* buff, size_t bufflen, mstime ms )
00122 {
00123 assertptr(buff,bufflen);
00124 return snprintf( buff, bufflen, "%lld.%03lld", ms/1000, ms%1000 );
00125 }
00126
00135 bool ctl_mstime_local( struct tm* ptm, mstime ms )
00136 {
00137 time_t time = ms/1000;
00138 struct tm* ret = localtime(&time);
00139 assertobjptr(ptm);
00140 if( NULL == ret )
00141 {
00142 memset(ptm,0,sizeof(*ptm));
00143 return false;
00144 }
00145 *ptm = *ret;
00146 return true;
00147 }
00148
00157 bool ctl_mstime_gm( struct tm* ptm, mstime ms )
00158 {
00159 time_t time = ms/1000;
00160 struct tm* ret = gmtime(&time);
00161 assertobjptr(ptm);
00162 if( NULL == ret )
00163 {
00164 memset(ptm,0,sizeof(*ptm));
00165 return false;
00166 }
00167 *ptm = *ret;
00168 return true;
00169 }
00170
00179 int ctl_mstime_print_time_local( char* buff, size_t bufflen, mstime ms )
00180 {
00181 time_t time = ms/1000;
00182 struct tm* tmtime = localtime(&time);
00183 assertptr(buff,bufflen);
00184 if( NULL == tmtime )
00185 return snprintf( buff, bufflen, "%lld: %s", (long long)ms, strerror(errno) );
00186 return snprintf( buff, bufflen, "%04d/%02d/%02d %2.2d:%2.2d:%2.2d.%03d", 1900+tmtime->tm_year, 1+tmtime->tm_mon, tmtime->tm_mday, tmtime->tm_hour, tmtime->tm_min, tmtime->tm_sec, (int)(ms%1000) );
00187 }
00188
00197 int ctl_mstime_print_time_gm( char* buff, size_t bufflen, mstime ms )
00198 {
00199 time_t time = ms/1000;
00200 struct tm* tmtime = gmtime(&time);
00201 assertptr(buff,bufflen);
00202 if( NULL == tmtime )
00203 return snprintf( buff, bufflen, "%lld: %s", (long long)ms, strerror(errno) );
00204 return snprintf( buff, bufflen, "%04d/%02d/%02d %2.2d:%2.2d:%2.2d.%03d GMT", 1900+tmtime->tm_year, 1+tmtime->tm_mon, tmtime->tm_mday, tmtime->tm_hour, tmtime->tm_min, tmtime->tm_sec, (int)(ms%1000) );
00205 }