timeutil.cpp
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 // 
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 // 
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "timeutil.h"
00018 #include <iostream>
00019 
00020 #ifdef _WINDOWS
00021 #include <time.h>
00022 #include <windows.h>
00023 #endif
00024 
00025 namespace g2o {
00026 
00027 #ifdef _WINDOWS
00028 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
00029   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
00030 #else
00031   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
00032 #endif
00033 
00034 struct timezone
00035 {
00036   int  tz_minuteswest; /* minutes W of Greenwich */
00037   int  tz_dsttime;     /* type of dst correction */
00038 };
00039 
00040 int gettimeofday(struct timeval *tv, struct timezone *tz)
00041 {
00042 // Define a structure to receive the current Windows filetime
00043   FILETIME ft;
00044  
00045 // Initialize the present time to 0 and the timezone to UTC
00046   unsigned __int64 tmpres = 0;
00047   static int tzflag = 0;
00048  
00049   if (NULL != tv)
00050   {
00051     GetSystemTimeAsFileTime(&ft);
00052  
00053 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond 
00054 // intervals since Jan 1, 1601 in a structure. Copy the high bits to 
00055 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
00056     tmpres |= ft.dwHighDateTime;
00057     tmpres <<= 32;
00058     tmpres |= ft.dwLowDateTime;
00059  
00060 // Convert to microseconds by dividing by 10
00061     tmpres /= 10;
00062  
00063 // The Unix epoch starts on Jan 1 1970.  Need to subtract the difference 
00064 // in seconds from Jan 1 1601.
00065     tmpres -= DELTA_EPOCH_IN_MICROSECS;
00066  
00067 // Finally change microseconds to seconds and place in the seconds value. 
00068 // The modulus picks up the microseconds.
00069     tv->tv_sec = (long)(tmpres / 1000000UL);
00070     tv->tv_usec = (long)(tmpres % 1000000UL);
00071   }
00072  
00073   if (NULL != tz) {
00074     if (!tzflag) {
00075       _tzset();
00076       tzflag++;
00077     }
00078 
00079     long sec;
00080     int hours;
00081     _get_timezone(&sec);
00082     _get_daylight(&hours);
00083   
00084 // Adjust for the timezone west of Greenwich
00085     tz->tz_minuteswest = sec / 60;
00086     tz->tz_dsttime = hours;
00087   }
00088  
00089   return 0;
00090 }
00091 #endif
00092 
00093 ScopeTime::ScopeTime(const char* title) : _title(title), _startTime(get_time()) {}
00094 
00095 ScopeTime::~ScopeTime() {
00096   std::cerr << _title<<" took "<<1000*(get_time()-_startTime)<<"ms.\n";
00097 }
00098 
00099 } // end namespace


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:08