Time.hh
00001 /*
00002  * Copyright 2011 Nate Koenig & Andrew Howard
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 /* Desc: External interfaces for Gazebo
00018  * Author: Nate Koenig
00019  * Date: 03 Apr 2007
00020  */
00021 
00022 #ifndef TIME_HH
00023 #define TIME_HH
00024 
00025 #include <stdlib.h>
00026 #include <time.h>
00027 #include <iostream>
00028 
00029 namespace gazebo
00030 {
00031     namespace common
00032   {
00035     
00037     class Time
00038     {
00040       public: Time();
00041     
00044       public: Time( const Time &time );
00045     
00048       public: Time( const struct timeval &tv );
00049     
00053       public: Time( int32_t sec,  int32_t nsec );
00054     
00057       public: Time( double time );
00058     
00060       public: virtual ~Time();
00061     
00063       public: static const Time &GetWallTime();
00064     
00066       public: void SetToWallTime();
00067     
00071       public: void Set( int32_t sec, int32_t nsec );
00072     
00075       public: void Set(double seconds);
00076     
00079       public: double Double() const;
00080 
00082       public: static Time MSleep(unsigned int _ms);
00083     
00085       public: const Time &operator=( const struct timeval &tv );
00086     
00088       public: const Time &operator=( const Time &time );
00089     
00091       public: Time operator +( const struct timeval &tv ) const;
00092     
00094       public: const Time &operator +=( const struct timeval &tv );
00095     
00097       public: Time operator +( const Time &time ) const;
00098     
00100       public: const Time &operator +=( const Time &time );
00101     
00103       public: Time operator -( const struct timeval &tv ) const;
00104     
00106       public: const Time &operator -=( const struct timeval &tv );
00107     
00109       public: Time operator -( const Time &time ) const;
00110     
00112       public: const Time &operator -=( const Time &time );
00113     
00115       public: Time operator *( const struct timeval &tv ) const;
00116     
00118       public: const Time &operator *=( const struct timeval &tv );
00119     
00121       public: Time operator *( const Time &time ) const;
00122     
00124       public: const Time &operator *=( const Time &time );
00125     
00127       public: Time operator /( const struct timeval &tv ) const;
00128     
00130       public: const Time &operator /=( const struct timeval &tv );
00131     
00133       public: Time operator /( const Time &time ) const;
00134     
00136       public: const Time &operator /=( const Time &time );
00137     
00139       public: bool operator==( const struct timeval &tv ) const;
00141       public: bool operator==( const Time &time ) const;
00143       public: bool operator==( double time ) const;
00145       public: bool operator!=( const struct timeval &tv ) const;
00147       public: bool operator!=( const Time &time ) const;
00149       public: bool operator!=( double time ) const;
00151       public: bool operator<( const struct timeval &tv ) const;
00153       public: bool operator<( const Time &time ) const;
00155       public: bool operator<( double time ) const;
00157       public: bool operator<=( const struct timeval &tv ) const;
00159       public: bool operator<=( const Time &time ) const;
00161       public: bool operator<=( double time ) const;
00163       public: bool operator>( const struct timeval &tv ) const;
00165       public: bool operator>( const Time &time ) const;
00167       public: bool operator>( double time ) const;
00169       public: bool operator>=( const struct timeval &tv ) const;
00171       public: bool operator>=( const Time &time ) const;
00173       public: bool operator>=( double time ) const;
00174     
00176       public: friend std::ostream &operator<<(std::ostream &out, const gazebo::common::Time &time)
00177               {
00178                 out << time.Double();
00179                 return out;
00180               }
00181     
00182       public: friend std::istream &operator>>(std::istream &in, gazebo::common::Time &time)
00183               {
00184                 double t;
00185                 in >> t;
00186                 time.Set(t);
00187                 return in;
00188               }
00189     
00191       public: int32_t sec;
00192     
00194       public: int32_t nsec;
00195 
00196       private: static Time wallTime;
00197 
00199       private: inline void Correct()
00200                {
00201                  // Make any corrections
00202                  if (this->nsec > 1e9)
00203                  {
00204                    this->sec++;
00205                    this->nsec = (int32_t)(this->nsec - 1e9);
00206                  }
00207                  else if (this->nsec < 0)
00208                  {
00209                    this->sec--;
00210                    this->nsec = (int32_t)(this->nsec + 1e9);
00211                  }
00212                }
00213     };
00215   
00216   }
00217 }
00218 #endif
00219