timeutil.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "timeutil.h"
28 #include <iostream>
29 
30 #ifdef _WINDOWS
31 #include <time.h>
32 #include <windows.h>
33 #endif
34 
35 #ifdef UNIX
36 #include <unistd.h>
37 #endif
38 
39 namespace g2o {
40 
41 #ifdef _WINDOWS
42 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
43  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
44 #else
45  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
46 #endif
47 
48 struct timezone
49 {
50  int tz_minuteswest; /* minutes W of Greenwich */
51  int tz_dsttime; /* type of dst correction */
52 };
53 
54 int gettimeofday(struct timeval *tv, struct timezone *tz)
55 {
56 // Define a structure to receive the current Windows filetime
57  FILETIME ft;
58 
59 // Initialize the present time to 0 and the timezone to UTC
60  unsigned __int64 tmpres = 0;
61  static int tzflag = 0;
62 
63  if (NULL != tv)
64  {
65  GetSystemTimeAsFileTime(&ft);
66 
67 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
68 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
69 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
70  tmpres |= ft.dwHighDateTime;
71  tmpres <<= 32;
72  tmpres |= ft.dwLowDateTime;
73 
74 // Convert to microseconds by dividing by 10
75  tmpres /= 10;
76 
77 // The Unix epoch starts on Jan 1 1970. Need to subtract the difference
78 // in seconds from Jan 1 1601.
79  tmpres -= DELTA_EPOCH_IN_MICROSECS;
80 
81 // Finally change microseconds to seconds and place in the seconds value.
82 // The modulus picks up the microseconds.
83  tv->tv_sec = (long)(tmpres / 1000000UL);
84  tv->tv_usec = (long)(tmpres % 1000000UL);
85  }
86 
87  if (NULL != tz) {
88  if (!tzflag) {
89  _tzset();
90  tzflag++;
91  }
92 
93  long sec;
94  int hours;
95  _get_timezone(&sec);
96  _get_daylight(&hours);
97 
98 // Adjust for the timezone west of Greenwich
99  tz->tz_minuteswest = sec / 60;
100  tz->tz_dsttime = hours;
101  }
102 
103  return 0;
104 }
105 #endif
106 
107 ScopeTime::ScopeTime(const char* title) : _title(title), _startTime(get_monotonic_time()) {}
108 
110  std::cerr << _title<<" took "<<1000*(get_monotonic_time()-_startTime)<<"ms.\n";
111 }
112 
114 {
115 #if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS+0 >= 0) && defined(_POSIX_MONOTONIC_CLOCK))
116  struct timespec ts;
117  clock_gettime(CLOCK_MONOTONIC, &ts);
118  return ts.tv_sec + ts.tv_nsec*1e-9;
119 #else
120  return get_time();
121 #endif
122 }
123 
124 } // end namespace
ScopeTime(const char *title)
Definition: timeutil.cpp:107
utility functions for handling time related stuff
double get_monotonic_time()
Definition: timeutil.cpp:113
double _startTime
Definition: timeutil.h:120
std::string _title
Definition: timeutil.h:119
double get_time()
Definition: timeutil.h:91


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47