timestamp.cxx

Go to the documentation of this file.
00001 
00006 // Written by Curtis Olson, started December 1998.
00007 //
00008 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
00009 //
00010 // This program is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License as
00012 // published by the Free Software Foundation; either version 2 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This program is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the Free Software
00022 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 //
00024 // $Id: timestamp_8cxx-source.html,v 1.15 2007-12-17 15:37:16 curt Exp $
00025 
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <simgear_config.h>
00029 #endif
00030 
00031 #ifdef HAVE_WINDOWS_H
00032 #  include <windows.h>
00033 #endif
00034 
00035 #include <simgear/compiler.h>
00036 
00037 #ifdef SG_HAVE_STD_INCLUDES
00038 #  include <ctime>
00039 #else
00040 #  include <time.h>
00041 #endif
00042 
00043 #ifdef HAVE_SYS_TIMEB_H
00044 #  include <sys/timeb.h> // for ftime() and struct timeb
00045 #endif
00046 #ifdef HAVE_UNISTD_H
00047 #  include <unistd.h>    // for gettimeofday()
00048 #endif
00049 #ifdef HAVE_SYS_TIME_H
00050 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
00051 #endif
00052 
00053 // -dw- want to use metrowerks time.h
00054 #ifdef macintosh
00055 #  include <time.h>
00056 #  include <timer.h>
00057 #endif
00058 
00059 #ifdef WIN32
00060 #  include <windows.h>
00061 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
00062 #    define NEAR /* */
00063 #    define FAR  /* */
00064 #  endif
00065 #  include <mmsystem.h>
00066 #endif
00067 
00068 #include "timestamp.hxx"
00069 
00070 
00071 void SGTimeStamp::stamp() {
00072 #if defined( WIN32 ) && !defined(__CYGWIN__)
00073     unsigned int t;
00074     t = timeGetTime();
00075     seconds = t / 1000;
00076     usec = ( t - ( seconds * 1000 ) ) * 1000;
00077 #elif defined( HAVE_GETTIMEOFDAY )
00078     struct timeval current;
00079     struct timezone tz;
00080     // sg_timestamp currtime;
00081     gettimeofday(&current, &tz);
00082     seconds = current.tv_sec;
00083     usec = current.tv_usec;
00084 #elif defined( HAVE_GETLOCALTIME )
00085     SYSTEMTIME current;
00086     GetLocalTime(&current);
00087     seconds = current.wSecond;
00088     usec = current.wMilliseconds * 1000;
00089 #elif defined( HAVE_FTIME )
00090     struct timeb current;
00091     ftime(&current);
00092     seconds = current.time;
00093     usec = current.millitm * 1000;
00094 // -dw- uses time manager
00095 #elif defined( macintosh )
00096     UnsignedWide ms;
00097     Microseconds(&ms);
00098         
00099     seconds = ms.lo / 1000000;
00100     usec = ms.lo - ( seconds * 1000000 );
00101 #else
00102 # error Port me
00103 #endif
00104 }
00105 
00106 // increment the time stamp by the number of microseconds (usec)
00107 SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
00108     return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
00109                         ( t.usec + m ) % 1000000 );
00110 }
00111 
00112 // difference between time stamps in microseconds (usec)
00113 long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
00114 {
00115     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
00116 }

Generated on Mon Dec 17 09:30:56 2007 for SimGear by  doxygen 1.5.1