]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timestamp.cxx
Merge branch 'maint' into next
[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() and the _POSIX_TIMERS define
40 #endif
41 #ifdef HAVE_SYS_TIME_H
42 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
43 #endif
44
45 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
46 #  include <time.h>
47 #  include <errno.h>
48 #endif
49
50 #ifdef WIN32
51 #  include <windows.h>
52 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
53 #    define NEAR /* */
54 #    define FAR  /* */
55 #  endif
56 #  include <mmsystem.h>
57 #endif
58
59 #include "timestamp.hxx"
60
61 void SGTimeStamp::stamp() {
62 #if defined( WIN32 ) && !defined(__CYGWIN__)
63     unsigned int t;
64     t = timeGetTime();
65     _sec = t / 1000;
66     _nsec = ( t - ( _sec * 1000 ) ) * 1000 * 1000;
67 #elif defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
68     struct timespec ts;
69 #if defined(_POSIX_MONOTONIC_CLOCK)
70     static clockid_t clockid = CLOCK_MONOTONIC;
71     static bool firstTime = true;
72     if (firstTime) {
73         firstTime = false;
74         // For the first time test if the monotonic clock is available.
75         // If so use this if not use the realtime clock.
76         if (-1 == clock_gettime(clockid, &ts) && errno == EINVAL)
77             clockid = CLOCK_REALTIME;
78     }
79     clock_gettime(clockid, &ts);
80 #else
81     clock_gettime(CLOCK_REALTIME, &ts);
82 #endif
83     _sec = ts.tv_sec;
84     _nsec = ts.tv_nsec;
85 #elif defined( HAVE_GETTIMEOFDAY )
86     struct timeval current;
87     struct timezone tz;
88     // sg_timestamp currtime;
89     gettimeofday(&current, &tz);
90     _sec = current.tv_sec;
91     _nsec = current.tv_usec * 1000;
92 #elif defined( HAVE_GETLOCALTIME )
93     SYSTEMTIME current;
94     GetLocalTime(&current);
95     _sec = current.wSecond;
96     _nsec = current.wMilliseconds * 1000 * 1000;
97 #elif defined( HAVE_FTIME )
98     struct timeb current;
99     ftime(&current);
100     _sec = current.time;
101     _nsec = current.millitm * 1000 * 1000;
102 #else
103 # error Port me
104 #endif
105 }
106