]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timestamp.cxx
Reduce compiler.h to almost nothing (but it's worth keeping around I think, for
[simgear.git] / simgear / timing / timestamp.cxx
1 /**
2  * \file timestamp.cxx
3  * Provides a class for managing a timestamp (seconds & milliseconds.)
4  */
5
6 // Written by Curtis Olson, started December 1998.
7 //
8 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <simgear_config.h>
29 #endif
30
31 #ifdef HAVE_WINDOWS_H
32 #  include <windows.h>
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include <ctime>
38
39 #ifdef HAVE_SYS_TIMEB_H
40 #  include <sys/timeb.h> // for ftime() and struct timeb
41 #endif
42 #ifdef HAVE_UNISTD_H
43 #  include <unistd.h>    // for gettimeofday()
44 #endif
45 #ifdef HAVE_SYS_TIME_H
46 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
47 #endif
48
49 #ifdef WIN32
50 #  include <windows.h>
51 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
52 #    define NEAR /* */
53 #    define FAR  /* */
54 #  endif
55 #  include <mmsystem.h>
56 #endif
57
58 #include "timestamp.hxx"
59
60
61 void SGTimeStamp::stamp() {
62 #if defined( WIN32 ) && !defined(__CYGWIN__)
63     unsigned int t;
64     t = timeGetTime();
65     seconds = t / 1000;
66     usec = ( t - ( seconds * 1000 ) ) * 1000;
67 #elif defined( HAVE_GETTIMEOFDAY )
68     struct timeval current;
69     struct timezone tz;
70     // sg_timestamp currtime;
71     gettimeofday(&current, &tz);
72     seconds = current.tv_sec;
73     usec = current.tv_usec;
74 #elif defined( HAVE_GETLOCALTIME )
75     SYSTEMTIME current;
76     GetLocalTime(&current);
77     seconds = current.wSecond;
78     usec = current.wMilliseconds * 1000;
79 #elif defined( HAVE_FTIME )
80     struct timeb current;
81     ftime(&current);
82     seconds = current.time;
83     usec = current.millitm * 1000;
84 #else
85 # error Port me
86 #endif
87 }
88
89 // increment the time stamp by the number of microseconds (usec)
90 SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
91     return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
92                         ( t.usec + m ) % 1000000 );
93 }
94
95 // difference between time stamps in microseconds (usec)
96 long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
97 {
98     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
99 }