TCTime.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCTime.h 874 2008-07-24 08:14:42Z the_____tiger $
00034 //*******************************************************************************
00035 
00036 #ifndef _TC_TIME_H_
00037 #define _TC_TIME_H_
00038 
00039 #include "TCTypes.h"
00040 
00041 namespace TC
00042 {
00058    class TCBASE_API Time
00059    {
00060    public:
00062       static Time FromSeconds(uint64 seconds);
00064       static Time FromMilliSeconds(uint64 milli_seconds);
00066       static Time FromMicroSeconds(uint64 micro_seconds);
00068       static Time FromNanoSeconds(uint64 nanos_seconds);
00070       static Time Now();
00072       static Time Since(const Time& start_time)
00073       {
00074          return Now() -= start_time;
00075       }
00076 
00078       uint64 Seconds() const {return m_secs;}
00080       uint64 NanoSeconds() const {return m_nsecs;}
00081 
00083       uint64 ToMilliSeconds() const
00084       {
00085          return m_secs * 1000 + m_nsecs / 1000000;
00086       }
00087 
00089       uint64 ToMicroSeconds() const
00090       {
00091          return m_secs * 1000000 + m_nsecs / 1000;
00092       }
00093 
00095       uint64 ToNanoSeconds() const
00096       {
00097          return m_secs * 1000000000 + m_nsecs;
00098       }
00099 
00100       Time& operator+=(const Time& time_to_add)
00101       {
00102          m_secs  += time_to_add.m_secs;
00103          m_nsecs += time_to_add.m_nsecs;
00104          if (m_nsecs >= 1000000000)
00105          {
00106             m_secs++;
00107             m_nsecs -= 1000000000;
00108          }
00109          return *this;
00110       }
00111 
00112       Time& operator-=(const Time& time_to_add)
00113       {
00114          m_secs -= time_to_add.m_secs;
00115          if (m_nsecs < time_to_add.m_nsecs)
00116          {
00117             m_secs--;
00118             m_nsecs += 1000000000;
00119          }
00120          m_nsecs -= time_to_add.m_nsecs;
00121          return *this;
00122       }
00123 
00124       bool operator==(const Time& a)
00125       {
00126          return (m_secs == a.m_secs) && (m_nsecs == a.m_nsecs);
00127       }
00128 
00129       bool operator!=(const Time& a)
00130       {
00131          return !this->operator==(a);
00132       }
00133 
00134    private:
00135       uint64 m_secs;
00136       uint64 m_nsecs;
00137    };
00138 
00139    inline const Time operator+(const Time& a, const Time& b) 
00140    {
00141       return Time(a) += b;
00142    }
00143 
00144    inline const Time operator-(const Time& a, const Time& b) 
00145    {
00146       return Time(a) -= b;
00147    }
00148 }
00149 
00150 #endif // _TC_TIME_H_
00151 

Copyright (c) Thomas Goessler 2003 - 2008