00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 # include <simgear_config.h>
00029 #endif
00030
00031 #include <simgear/compiler.h>
00032
00033 #include <ctime>
00034
00035 #ifdef HAVE_SYS_TIMEB_H
00036 # include <sys/timeb.h>
00037 #endif
00038 #ifdef HAVE_UNISTD_H
00039 # include <unistd.h>
00040 #endif
00041 #ifdef HAVE_SYS_TIME_H
00042 # include <sys/time.h>
00043 #endif
00044
00045 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
00046 # include <time.h>
00047 # include <errno.h>
00048 #endif
00049
00050 #ifdef WIN32
00051 # include <windows.h>
00052 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
00053 # define NEAR
00054 # define FAR
00055 # endif
00056 # include <mmsystem.h>
00057 #endif
00058
00059 #include "timestamp.hxx"
00060
00061 void SGTimeStamp::stamp() {
00062 #ifdef _WIN32
00063 unsigned int t;
00064 t = timeGetTime();
00065 _sec = t / 1000;
00066 _nsec = ( t - ( _sec * 1000 ) ) * 1000 * 1000;
00067 #elif defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
00068 struct timespec ts;
00069 #if defined(_POSIX_MONOTONIC_CLOCK)
00070 static clockid_t clockid = CLOCK_MONOTONIC;
00071 static bool firstTime = true;
00072 if (firstTime) {
00073 firstTime = false;
00074
00075
00076 if (-1 == clock_gettime(clockid, &ts) && errno == EINVAL)
00077 clockid = CLOCK_REALTIME;
00078 }
00079 clock_gettime(clockid, &ts);
00080 #else
00081 clock_gettime(CLOCK_REALTIME, &ts);
00082 #endif
00083 _sec = ts.tv_sec;
00084 _nsec = ts.tv_nsec;
00085 #elif defined( HAVE_GETTIMEOFDAY )
00086 struct timeval current;
00087 struct timezone tz;
00088
00089 gettimeofday(¤t, &tz);
00090 _sec = current.tv_sec;
00091 _nsec = current.tv_usec * 1000;
00092 #elif defined( HAVE_GETLOCALTIME )
00093 SYSTEMTIME current;
00094 GetLocalTime(¤t);
00095 _sec = current.wSecond;
00096 _nsec = current.wMilliseconds * 1000 * 1000;
00097 #elif defined( HAVE_FTIME )
00098 struct timeb current;
00099 ftime(¤t);
00100 _sec = current.time;
00101 _nsec = current.millitm * 1000 * 1000;
00102 #else
00103 # error Port me
00104 #endif
00105 }
00106