00001
00002 #ifndef DDF_CTIME_HPP
00003 #define DDF_CTIME_HPP
00004
00005 #include <boost/cstdint.hpp>
00006 #include <boost/thread/xtime.hpp>
00007 #include <boost/thread/thread.hpp>
00008 #include <iosfwd>
00009 #include <iostream>
00010
00011 namespace jafar
00012 {
00013 namespace ddf
00014 {
00015
00022 class time
00023 {
00024 public:
00025 typedef time_t sec_t;
00026 typedef suseconds_t usec_t;
00027 static const int UsecPerSec = 1000000;
00028
00029 private:
00030 enum Type
00031 {
00032 Normal,
00033 Infty
00034 };
00035
00036 Type m_type;
00037 sec_t m_sec;
00038 usec_t m_usec;
00039
00040 explicit time(Type mode)
00041 : m_type(mode), m_sec(0), m_usec(0) {}
00042
00043 void assign(sec_t sec, usec_t usec);
00044 int compare(time const& other) const;
00045
00046 public:
00047
00049 explicit time(sec_t sec = 0, usec_t usec = 0): m_type(Normal)
00050 {
00051
00052 assign(sec, usec);
00053 }
00054
00056 explicit time(double d): m_type(Normal)
00057 {
00058 sec_t sec = static_cast<sec_t> (d);
00059 usec_t usec = static_cast<usec_t> (UsecPerSec * (d- (double) sec));
00060 assign(sec, usec);
00061 }
00062
00066 explicit time(timeval const& t): m_type(Normal), m_sec(t.tv_sec), m_usec(t.tv_usec) {}
00067
00068 bool is_normal() const { return m_type == Normal; }
00069 bool is_infty() const { return m_type == Infty; }
00070 bool is_null() const { return m_type == Normal && !m_sec && !m_usec; }
00071
00081 static time infty() { return time(Infty); }
00082 static time null() { return time(); }
00083 static time current();
00084
00086 static time from_ms(int msecs) { return time(0, msecs * 1000); }
00087 static time from_us(int usecs) { return time(0, usecs); }
00088
00089 time_t s() const { return m_sec; }
00090 time_t ms() const { return m_usec / 1000; }
00091 time_t us() const { return m_usec; }
00092
00093 timeval to_timeval() const
00094 {
00095 timeval tv = { m_sec, m_usec };
00096 return tv;
00097 }
00098
00099 timespec to_timespec() const
00100 {
00101 timespec ts = { m_sec, m_usec * 1000 };
00102 return ts;
00103 }
00104
00105 double to_double() const
00106 {
00107 return (double) (m_sec + ((double) m_usec) / ((double) UsecPerSec));
00108 }
00109
00110 boost::xtime to_xtime() const;
00111
00112 void sleep_period() const
00113 {
00114 timeval tv = to_timeval();
00115 select(0,0,0,0,&tv);
00116 }
00117
00118 void sleep_until() const
00119 {
00120 boost::thread::sleep(to_xtime());
00121 }
00122
00123 bool check_close(time const& other, time const& epsilon) const;
00124
00125 bool operator > (const time& t) const;
00126 bool operator >= (const time& t) const;
00127 bool operator < (const time& t) const;
00128 bool operator <= (const time& t) const;
00129 bool operator == (const time& t) const;
00130 bool operator != (const time& t) const;
00131 time operator - (const time& other) const;
00132 time operator + (const time& other) const;
00133 time operator / (double divider) const;
00134 time operator * (double multipl) const;
00135 void operator = (const time& other);
00136
00137 friend std::istream& operator >> (std::istream& stream, time& t);
00138 friend std::ostream& operator << (std::ostream& stream, const time& t);
00139 };
00140
00141 std::ostream& operator << (std::ostream& stream, const time& t);
00142
00143 }
00144 }
00145 #endif