3 * Provides a class for managing a timestamp (seconds & milliseconds.)
6 // Written by Curtis Olson, started December 1998.
8 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 # include <simgear_config.h>
31 #include <simgear/compiler.h>
35 #ifdef HAVE_SYS_TIMEB_H
36 # include <sys/timeb.h> // for ftime() and struct timeb
39 # include <unistd.h> // for gettimeofday() and the _POSIX_TIMERS define
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
45 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
52 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
56 # include <mmsystem.h>
59 #include "timestamp.hxx"
61 void SGTimeStamp::stamp() {
66 _nsec = ( t - ( _sec * 1000 ) ) * 1000 * 1000;
67 #elif defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
69 #if defined(_POSIX_MONOTONIC_CLOCK)
70 static clockid_t clockid = CLOCK_MONOTONIC;
71 static bool firstTime = true;
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;
79 clock_gettime(clockid, &ts);
81 clock_gettime(CLOCK_REALTIME, &ts);
85 #elif defined( HAVE_GETTIMEOFDAY )
86 struct timeval current;
88 // sg_timestamp currtime;
89 gettimeofday(¤t, &tz);
90 _sec = current.tv_sec;
91 _nsec = current.tv_usec * 1000;
92 #elif defined( HAVE_GETLOCALTIME )
94 GetLocalTime(¤t);
95 _sec = current.wSecond;
96 _nsec = current.wMilliseconds * 1000 * 1000;
97 #elif defined( HAVE_FTIME )
101 _nsec = current.millitm * 1000 * 1000;