]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timestamp.cxx
Tidy up the autoconf/automake configuration a bit.
[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  - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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 #ifdef SG_HAVE_STD_INCLUDES
38 #  include <ctime>
39 #else
40 #  include <time.h>
41 #endif
42
43 #ifdef HAVE_SYS_TIMEB_H
44 #  include <sys/timeb.h> // for ftime() and struct timeb
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #  include <unistd.h>    // for gettimeofday()
48 #endif
49 #ifdef HAVE_SYS_TIME_H
50 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
51 #endif
52
53 // -dw- want to use metrowerks time.h
54 #ifdef macintosh
55 #  include <time.h>
56 #  include <timer.h>
57 #endif
58
59 #ifdef WIN32
60 #  include <windows.h>
61 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
62 #    define NEAR /* */
63 #    define FAR  /* */
64 #  endif
65 #  include <mmsystem.h>
66 #endif
67
68 #include "timestamp.hxx"
69
70
71 void SGTimeStamp::stamp() {
72 #if defined( WIN32 )
73     unsigned int t;
74     t = timeGetTime();
75     seconds = 0;
76     usec =  t * 1000;
77 #elif defined( HAVE_GETTIMEOFDAY )
78     struct timeval current;
79     struct timezone tz;
80     // sg_timestamp currtime;
81     gettimeofday(&current, &tz);
82     seconds = current.tv_sec;
83     usec = current.tv_usec;
84 #elif defined( HAVE_GETLOCALTIME )
85     SYSTEMTIME current;
86     GetLocalTime(&current);
87     seconds = current.wSecond;
88     usec = current.wMilliseconds * 1000;
89 #elif defined( HAVE_FTIME )
90     struct timeb current;
91     ftime(&current);
92     seconds = current.time;
93     usec = current.millitm * 1000;
94 // -dw- uses time manager
95 #elif defined( macintosh )
96     UnsignedWide ms;
97     Microseconds(&ms);
98         
99     seconds = ms.lo / 1000000;
100     usec = ms.lo - ( seconds * 1000000 );
101 #else
102 # error Port me
103 #endif
104 }
105
106 // increment the time stamp by the number of microseconds (usec)
107 SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
108 #if defined( WIN32 )
109     return SGTimeStamp( 0, t.usec + m );
110 #else
111     return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
112                         ( t.usec + m ) % 1000000 );
113 #endif
114 }
115
116 // difference between time stamps in microseconds (usec)
117 long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
118 {
119 #if defined( WIN32 )
120     return a.usec - b.usec;
121 #else
122     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
123 #endif
124 }