00001 /* 00002 * File: Timestamp.h 00003 * Author: Dorian Galvez-Lopez 00004 * Date: March 2009 00005 * Description: timestamping functions 00006 * 00007 * 00008 * This program is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 * 00021 */ 00022 00023 #ifndef __D_TIMESTAMP__ 00024 #define __D_TIMESTAMP__ 00025 00026 #include <iostream> 00027 using namespace std; 00028 00029 namespace DUtils { 00030 00031 class Timestamp 00032 { 00033 public: 00034 enum tOptions 00035 { 00036 NONE = 0, 00037 CURRENT_TIME = 0x1 00038 }; 00039 00040 public: 00041 Timestamp(Timestamp::tOptions option = NONE); 00042 ~Timestamp(void); 00043 00044 /* Sets this instance to the current time 00045 */ 00046 void setToCurrentTime(); 00047 00048 /* Sets the timestamp from seconds and microseconds 00049 * @param secs: seconds 00050 * @param usecs: microseconds 00051 */ 00052 inline void setTime(unsigned long secs, unsigned long usecs){ 00053 m_secs = secs; 00054 m_usecs = usecs; 00055 } 00056 00057 /* Sets the timestamp from a string with the time in seconds 00058 * @param stime: string such as "1235603336.036609" 00059 */ 00060 void setTime(const string &stime); 00061 00062 /* Returns this timestamp as the number of seconds in (long) float format 00063 */ 00064 double getFloatTime() const; 00065 00066 /* Returns this timestamp as the number of seconds in fixed length string format 00067 */ 00068 string getStringTime() const; 00069 00070 /* Returns the difference in seconds between this timestamp (greater) and t (smaller) 00071 * If the order is swapped, a negative number is returned 00072 * @param t: timestamp to subtract from this timestamp 00073 * @return difference in seconds 00074 */ 00075 double operator- (const Timestamp &t) const; 00076 00077 /* Returns a copy of this timestamp + s seconds 00078 * @param s: seconds 00079 */ 00080 Timestamp operator+ (double s) const; 00081 00082 /* Returns a copy of this timestamp - s seconds 00083 * @param s: seconds 00084 */ 00085 Timestamp operator- (double s) const; 00086 00087 /* Returns whether this timestamp is at the future of t 00088 * @param t 00089 */ 00090 bool operator> (const Timestamp &t) const; 00091 00092 /* Returns whether this timestamp is at the future of (or is the same as) t 00093 * @param t 00094 */ 00095 bool operator>= (const Timestamp &t) const; 00096 00097 /* Returns whether this timestamp and t represent the same instant 00098 * @param t 00099 */ 00100 bool operator== (const Timestamp &t) const; 00101 00102 /* Returns whether this timestamp is at the past of t 00103 * @param t 00104 */ 00105 bool operator< (const Timestamp &t) const; 00106 00107 /* Returns whether this timestamp is at the past of (or is the same as) t 00108 * @param t 00109 */ 00110 bool operator<= (const Timestamp &t) const; 00111 00119 string Format(bool machine_friendly = false) const; 00120 00121 /* Returns a string version of the elapsed time in seconds, with the format 00122 * xd hh:mm:ss, hh:mm:ss, mm:ss or s.us 00123 * @param s: elapsed seconds (given by getFloatTime) to format 00124 */ 00125 static string Format(double s); 00126 00127 00128 protected: 00129 unsigned long m_secs; // seconds 00130 unsigned long m_usecs; // microseconds 00131 }; 00132 00133 } 00134 00135 #endif 00136