]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timestamp.cxx
dcda28ec9e7265cb8e577f6e26cc273f94ccd2af
[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 #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 #ifdef WIN32
54 #  include <windows.h>
55 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
56 #    define NEAR /* */
57 #    define FAR  /* */
58 #  endif
59 #  include <mmsystem.h>
60 #endif
61
62 #include "timestamp.hxx"
63
64
65 void SGTimeStamp::stamp() {
66 #if defined( WIN32 ) && !defined(__CYGWIN__)
67     unsigned int t;
68     t = timeGetTime();
69     seconds = t / 1000;
70     usec = ( t - ( seconds * 1000 ) ) * 1000;
71 #elif defined( HAVE_GETTIMEOFDAY )
72     struct timeval current;
73     struct timezone tz;
74     // sg_timestamp currtime;
75     gettimeofday(&current, &tz);
76     seconds = current.tv_sec;
77     usec = current.tv_usec;
78 #elif defined( HAVE_GETLOCALTIME )
79     SYSTEMTIME current;
80     GetLocalTime(&current);
81     seconds = current.wSecond;
82     usec = current.wMilliseconds * 1000;
83 #elif defined( HAVE_FTIME )
84     struct timeb current;
85     ftime(&current);
86     seconds = current.time;
87     usec = current.millitm * 1000;
88 #else
89 # error Port me
90 #endif
91 }
92
93 // increment the time stamp by the number of microseconds (usec)
94 SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
95     return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
96                         ( t.usec + m ) % 1000000 );
97 }
98
99 // difference between time stamps in microseconds (usec)
100 long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
101 {
102     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
103 }