]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timestamp.cxx
Don't waste space with too huge stl containers.
[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 #include <simgear/compiler.h>
32
33 #include <ctime>
34
35 #ifdef HAVE_SYS_TIMEB_H
36 #  include <sys/timeb.h> // for ftime() and struct timeb
37 #endif
38 #ifdef HAVE_UNISTD_H
39 #  include <unistd.h>    // for gettimeofday()
40 #endif
41 #ifdef HAVE_SYS_TIME_H
42 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
43 #endif
44
45 #ifdef WIN32
46 #  include <windows.h>
47 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
48 #    define NEAR /* */
49 #    define FAR  /* */
50 #  endif
51 #  include <mmsystem.h>
52 #endif
53
54 #include "timestamp.hxx"
55
56
57 void SGTimeStamp::stamp() {
58 #if defined( WIN32 ) && !defined(__CYGWIN__)
59     unsigned int t;
60     t = timeGetTime();
61     seconds = t / 1000;
62     usec = ( t - ( seconds * 1000 ) ) * 1000;
63 #elif defined( HAVE_GETTIMEOFDAY )
64     struct timeval current;
65     struct timezone tz;
66     // sg_timestamp currtime;
67     gettimeofday(&current, &tz);
68     seconds = current.tv_sec;
69     usec = current.tv_usec;
70 #elif defined( HAVE_GETLOCALTIME )
71     SYSTEMTIME current;
72     GetLocalTime(&current);
73     seconds = current.wSecond;
74     usec = current.wMilliseconds * 1000;
75 #elif defined( HAVE_FTIME )
76     struct timeb current;
77     ftime(&current);
78     seconds = current.time;
79     usec = current.millitm * 1000;
80 #else
81 # error Port me
82 #endif
83 }
84
85 // increment the time stamp by the number of microseconds (usec)
86 SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
87     return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
88                         ( t.usec + m ) % 1000000 );
89 }
90
91 // difference between time stamps in microseconds (usec)
92 long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
93 {
94     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
95 }