00001 /* 00002 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00003 May be copied only for purposes and under conditions described 00004 on the Web page www.basepath.com/aup/copyright.htm. 00005 00006 The Example Files are provided "as is," without any warranty; 00007 without even the implied warranty of merchantability or fitness 00008 for a particular purpose. The author and his publisher are not 00009 responsible for any damages, direct or incidental, resulting 00010 from the use or non-use of these Example Files. 00011 00012 The Example Files may contain defects, and some contain deliberate 00013 coding mistakes that were included for educational reasons. 00014 You are responsible for determining if and how the Example Files 00015 are to be used. 00016 00017 */ 00018 #ifndef _UXTIME_HPP_ 00019 #define _UXTIME_HPP_ 00020 00021 #include <wchar.h> 00022 #include <sys/time.h> 00023 00024 namespace Ux { 00025 00026 enum when {epoch, now}; 00027 00028 /** 00029 \ingroup Ux 00030 Clearer replacement for Timet (time_t). 00031 */ 00032 class TimeSec : public Base { 00033 protected: 00034 time_t tt; 00035 00036 public: 00037 TimeSec(time_t t) 00038 : tt(t) 00039 { } 00040 TimeSec(when w = epoch); 00041 operator time_t() const 00042 { return tt; } 00043 void set(time_t t) 00044 { tt = t; } 00045 double diff(time_t time0); 00046 }; 00047 00048 /** 00049 \ingroup Ux 00050 Wrapper class for struct timeval. 00051 */ 00052 class TimeMsec : public timeval, public Base { 00053 public: 00054 TimeMsec(TimeMsec& t) 00055 { 00056 tv_sec = t.tv_sec; 00057 tv_usec = t.tv_usec; 00058 } 00059 TimeMsec(when w = epoch); 00060 const char *get_string(char *buf, size_t bufsize); 00061 }; 00062 00063 /** 00064 \ingroup Ux 00065 Wrapper class for struct timespec. 00066 */ 00067 class TimeNsec : public timespec, public Base { 00068 public: 00069 TimeNsec(TimeNsec& t) 00070 { 00071 tv_sec = t.tv_sec; 00072 tv_nsec = t.tv_nsec; 00073 } 00074 TimeNsec(when w = epoch); 00075 const char *get_string(char *buf, size_t bufsize); 00076 }; 00077 00078 //@{ 00079 /** \name ostream Operators 00080 Operators to display UNIX times. 00081 */ 00082 std::ostream& operator<<(std::ostream& s, const timeval& t); 00083 std::ostream& operator<<(std::ostream& s, const timespec& t); 00084 //@} 00085 00086 class TimeParts; 00087 00088 /** 00089 \ingroup Ux 00090 Clearer replacement for Timestr (char *). 00091 */ 00092 class TimeString : public Base { 00093 friend TimeParts; 00094 00095 protected: 00096 char timestr[26]; 00097 00098 public: 00099 TimeString(void) 00100 { 00101 set(); 00102 } 00103 TimeString(time_t t, bool keep_nl = true) 00104 { 00105 set(t, keep_nl); 00106 } 00107 void set(const char *s = "") 00108 { 00109 strncpy(timestr, s, sizeof(timestr)); 00110 timestr[sizeof(timestr) - 1] = '\0'; 00111 } 00112 operator const char*() const 00113 { return timestr; } 00114 void set(time_t t, bool keep_nl = true); 00115 }; 00116 00117 /** 00118 \ingroup Ux 00119 Clearer replacement for Timetm (struct tm). 00120 */ 00121 class TimeParts : public tm, public Base { 00122 protected: 00123 TimeString str; 00124 00125 public: 00126 enum zone {zone_local, zone_utc} ; 00127 /** 00128 Initializes to local time (at epoch by default). 00129 */ 00130 TimeParts(time_t t = 0) 00131 { 00132 set(t); 00133 } 00134 operator const char*() 00135 { return get_string(false); } 00136 void set(const struct tm& t); 00137 TimeSec get_secs(void); 00138 const char *get_string(bool keep_nl = true); 00139 void set(const char *s); 00140 void set(time_t t, zone z = zone_local); 00141 void set(zone z = zone_local) 00142 { return set(TimeSec(now), z); } 00143 char *set_fmt(const char *s, const char *format); 00144 size_t get_fmt(char *buf, size_t bufsize, const char *format); 00145 size_t get_fmtw(wchar_t *buf, size_t bufsize, const wchar_t *format); 00146 }; 00147 00148 /** 00149 \ingroup Ux 00150 00151 */ 00152 class Clock : public Base { 00153 protected: 00154 clockid_t clock_id; 00155 00156 public: 00157 Clock(clockid_t cid = CLOCK_REALTIME) 00158 { clock_id = cid; } 00159 operator clock_t() const 00160 { return clock_id; } 00161 void nanosleep(int flags, const TimeNsec& nsecs); 00162 void nanosleep(int flags, const TimeNsec& nsecs, const TimeNsec& remain); 00163 static void nanosleep(const TimeNsec& nsecs, TimeNsec& remain); 00164 static void nanosleep(const TimeNsec& nsecs); 00165 static void getcpuclockid(pid_t pid, Clock& clock); 00166 void getres(TimeNsec& res); 00167 void gettime(TimeNsec& t); 00168 void settime(const TimeNsec& t); 00169 /** 00170 Calls ::sleep. 00171 */ 00172 static unsigned sleep(unsigned secs) 00173 { return ::sleep(secs); } 00174 static void usleep(useconds_t usecs); 00175 }; 00176 00177 /** 00178 \ingroup Ux 00179 00180 */ 00181 class Timer : public Base { 00182 public: 00183 /** 00184 Calls ::alarm. 00185 */ 00186 static unsigned alarm(unsigned secs) 00187 { return ::alarm(secs); } 00188 }; 00189 00190 /** 00191 \ingroup Ux 00192 00193 */ 00194 class RealtimeTimer : public Timer { 00195 protected: 00196 timer_t timer_id; 00197 00198 public: 00199 RealtimeTimer(timer_t t = -1) 00200 : timer_id(t) 00201 { } 00202 void create(const Clock& clock, struct sigevent *sig = NULL); 00203 void del(void); 00204 void get(struct itimerspec& val); 00205 void set(int flags, const struct itimerspec& val, struct itimerspec& oval); 00206 }; 00207 00208 /** 00209 \ingroup Ux 00210 00211 */ 00212 class IntervalTimer : public Timer { 00213 protected: 00214 int which_timer; 00215 00216 public: 00217 IntervalTimer(int which = ITIMER_REAL) 00218 : which_timer(which) 00219 { } 00220 void get(struct itimerval& val); 00221 void set(const struct itimerval& val, struct itimerval& oval); 00222 }; 00223 00224 } // namespace 00225 00226 #endif // _UXTIME_HPP_