00001 // 00002 // plumbing - C++ wrapper facades for Berkeley sockets 00003 // Copyright (C) 2009 Peter Miller 00004 // 00005 // This program is free software; you can redistribute it and/or modify it 00006 // under the terms of the GNU General Public License, version 3, as published 00007 // by the Free Software Foundation. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT 00010 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00012 // more details. 00013 // 00014 // You should have received a copy of the GNU General Public License along 00015 // with this program. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 00018 #ifndef LIBPLUMBING_TIME_VALUE_H 00019 #define LIBPLUMBING_TIME_VALUE_H 00020 00021 #include <sys/time.h> 00022 #include <string> 00023 00024 namespace plumbing { 00025 00030 class time_value: 00031 public timeval 00032 { 00033 public: 00039 ~time_value() { } 00040 00046 time_value() { tv_sec = 0; tv_usec = 0; } 00047 00058 time_value(long seconds, long microseconds = 0); 00059 00066 time_value(int seconds) { tv_sec = seconds; tv_usec = 0; } 00067 00075 time_value(double seconds); 00076 00084 time_value(const char *str); 00085 00089 time_value(const time_value &); 00090 00094 time_value & 00095 operator=(const time_value &rhs) 00096 { 00097 if (this != &rhs) 00098 { 00099 tv_sec = rhs.tv_sec; 00100 tv_usec = rhs.tv_usec; 00101 } 00102 return *this; 00103 } 00104 00108 time_value &operator=(double); 00109 00113 time_value & 00114 operator=(long sec) 00115 { 00116 tv_sec = sec; 00117 tv_usec = 0; 00118 return *this; 00119 } 00120 00125 long get_seconds() const { return tv_sec; } 00126 00131 operator double() const; 00132 00141 int get_time_of_day(); 00142 00150 static time_value now(); 00151 00156 std::string representation() const; 00157 00158 time_value &operator+=(const time_value &); 00159 time_value operator+(const time_value &) const; 00160 time_value &operator-=(const time_value &); 00161 time_value operator-(const time_value &) const; 00162 time_value operator-() const; 00163 time_value &operator*=(double); 00164 time_value operator*(double) const; 00165 time_value &operator*=(long); 00166 time_value operator*(long) const; 00167 00168 bool operator==(const time_value &) const; 00169 bool operator!=(const time_value &) const; 00170 bool operator<(const time_value &) const; 00171 bool operator<=(const time_value &) const; 00172 bool operator>(const time_value &) const; 00173 bool operator>=(const time_value &) const; 00174 00175 private: 00184 bool in_normal_form() const { return (tv_usec >= 0 && tv_usec < 1000000); } 00185 }; 00186 00187 } 00188 00189 #endif // LIBPLUMBING_TIME_VALUE_H 00190 // vim: set ts=4 et