00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "ux.hpp"
00019 #include <iomanip>
00020
00021 using namespace Ux;
00022
00023
00024
00025
00026 TimeSec TimeParts::get_secs(void)
00027 {
00028 time_t t;
00029
00030 errno = 0;
00031 if ((t = ::mktime(this)) == -1) {
00032 if (errno == 0)
00033 errno = EINVAL;
00034 throw Error(errno);
00035 }
00036 return TimeSec(t);
00037 }
00038
00039
00040
00041
00042 const char *TimeParts::get_string(bool keep_nl)
00043 {
00044 errno = 0;
00045 if (::asctime_r(this, str.timestr) == NULL) {
00046 if (errno == 0)
00047 throw Error(EINVAL);
00048 throw Error(errno);
00049 }
00050 if (!keep_nl)
00051 str.timestr[strlen(str.timestr) - 1] = '\0';
00052 return str;
00053 }
00054
00055
00056
00057
00058 void TimeParts::set(const char *s)
00059 {
00060 struct tm *t;
00061
00062 if ((t = ::getdate(s)) == NULL)
00063 throw Error(getdate_err, EC_GETDATE);
00064 set(*t);
00065 }
00066 #if 0
00067
00068 void TimeParts::gmtime(const time_t *t)
00069 {
00070 time_t tx;
00071
00072 if (t == NULL) {
00073 tx = TimeSec::time();
00074 t = &tx;
00075 }
00076 if (::gmtime_r(t, this) == NULL)
00077 throw Error(errno);
00078 }
00079
00080 void TimeParts::localtime(const time_t *t)
00081 {
00082 time_t tx;
00083
00084 if (t == NULL) {
00085 tx = TimeSec::time();
00086 t = &tx;
00087 }
00088 if (::localtime_r(t, this) == NULL)
00089 throw Error(errno);
00090 }
00091 #endif
00092
00093
00094
00095
00096 void TimeParts::set(time_t t, zone z)
00097 {
00098 if (z == zone_local) {
00099 if (::localtime_r(&t, this) == NULL)
00100 throw Error(errno);
00101 }
00102 else {
00103 if (::gmtime_r(&t, this) == NULL)
00104 throw Error(errno);
00105 }
00106
00107 }
00108
00109
00110
00111
00112 void TimeParts::set(const struct tm& t)
00113 {
00114 tm_sec = t.tm_sec;
00115 tm_min = t.tm_min;
00116 tm_hour = t.tm_hour;
00117 tm_mday = t.tm_mday;
00118 tm_mon = t.tm_mon;
00119 tm_year = t.tm_year;
00120 tm_wday = t.tm_wday;
00121 tm_yday = t.tm_yday;
00122 tm_isdst = t.tm_isdst;
00123 str.set();
00124 }
00125
00126
00127
00128
00129 char *TimeParts::set_fmt(const char *s, const char *format)
00130 {
00131 char *p;
00132
00133 errno = 0;
00134 if ((p = ::strptime(s, format, this)) == NULL) {
00135 if (errno == 0)
00136 errno = EINVAL;
00137 throw Error(errno);
00138 }
00139 return p;
00140 }
00141
00142
00143
00144
00145 size_t TimeParts::get_fmt(char *buf, size_t bufsize, const char *format)
00146 {
00147 size_t n;
00148
00149 errno = 0;
00150 if ((n = ::strftime(buf, bufsize, format, this)) == 0) {
00151 if (errno == 0)
00152 errno = EINVAL;
00153 throw Error(errno);
00154 }
00155 return n;
00156 }
00157
00158
00159
00160
00161 size_t TimeParts::get_fmtw(wchar_t *buf, size_t bufsize, const wchar_t *format)
00162 {
00163 size_t n;
00164
00165 errno = 0;
00166 if ((n = ::wcsftime(buf, bufsize, format, this)) == 0) {
00167 if (errno == 0)
00168 errno = EINVAL;
00169 throw Error(errno);
00170 }
00171 return n;
00172 }
00173
00174
00175
00176
00177 void TimeString::set(time_t t, bool keep_nl)
00178 {
00179 if (::ctime_r(&t, timestr) == NULL)
00180 throw Error(errno);
00181 if (!keep_nl)
00182 timestr[strlen(timestr) - 1] = '\0';
00183 }
00184
00185
00186
00187
00188 TimeSec::TimeSec(when w)
00189 {
00190 if (w == epoch)
00191 tt = 0;
00192 else
00193 tt = ::time(NULL);
00194 }
00195
00196
00197
00198
00199 double TimeSec::diff(time_t time0)
00200 {
00201 return ::difftime(tt, time0);
00202 }
00203
00204
00205
00206
00207 TimeMsec::TimeMsec(when w)
00208 {
00209 if (w == epoch) {
00210 tv_sec = 0;
00211 tv_usec = 0;
00212 }
00213 else {
00214 errno = 0;
00215 if (::gettimeofday(this, NULL) != 0) {
00216 if (errno == 0)
00217 errno = EINVAL;
00218 throw Error(errno);
00219 }
00220 }
00221 }
00222
00223
00224
00225
00226 const char *TimeMsec::get_string(char *buf, size_t bufsize)
00227 {
00228 snprintf(buf, bufsize, "%lld.%.6ld", (long long)tv_sec, (long)tv_usec);
00229 return buf;
00230 }
00231
00232
00233
00234
00235 TimeNsec::TimeNsec(when w)
00236 {
00237 if (w == epoch) {
00238 tv_sec = 0;
00239 tv_nsec = 0;
00240 }
00241 else
00242 Clock().gettime(*this);
00243 }
00244
00245
00246
00247
00248 const char *TimeNsec::get_string(char *buf, size_t bufsize)
00249 {
00250 snprintf(buf, bufsize, "%lld.%.9ld", (long long)tv_sec, (long)tv_nsec);
00251 return buf;
00252 }
00253
00254
00255
00256
00257 std::ostream& Ux::operator<<(std::ostream& s, const timeval& t)
00258 {
00259 s << t.tv_sec << "." << setw(6) << setfill('0') << t.tv_usec;
00260 return s;
00261 }
00262
00263
00264
00265
00266 std::ostream& Ux::operator<<(std::ostream& s, const timespec& t)
00267 {
00268 s << t.tv_sec << "." << setw(9) << setfill('0') << t.tv_nsec;
00269 return s;
00270 }
00271
00272
00273
00274
00275 void Clock::gettime(TimeNsec& t)
00276 {
00277 if (::clock_gettime(clock_id, &t) == -1)
00278 throw Error(errno);
00279 }
00280
00281
00282
00283
00284 void Clock::getres(TimeNsec& res)
00285 {
00286 if (::clock_getres(clock_id, &res) == -1)
00287 throw Error(errno);
00288 }
00289
00290
00291
00292
00293 void Clock::settime(const TimeNsec& t)
00294 {
00295 if (::clock_settime(clock_id, &t) == -1)
00296 throw Error(errno);
00297 }
00298
00299
00300
00301
00302 void Clock::nanosleep(int flags, const TimeNsec& nsecs, const TimeNsec& remain)
00303 {
00304 #if _POSIX_CLOCK_SELECTION > 0
00305 int r;
00306
00307 if ((r = ::clock_nanosleep(clock_id, flags, &nsecs, &remain)) > 0)
00308 throw Error(r);
00309 #else
00310 throw Error(ENOSYS);
00311 #endif
00312 }
00313
00314
00315
00316
00317 void Clock::nanosleep(int flags, const TimeNsec& nsecs)
00318 {
00319 #if _POSIX_CLOCK_SELECTION > 0
00320 int r;
00321
00322 if ((r = ::clock_nanosleep(clock_id, flags, &nsecs, NULL)) > 0)
00323 throw Error(r);
00324 #else
00325 throw Error(ENOSYS);
00326 #endif
00327 }
00328
00329
00330
00331
00332 void Clock::nanosleep(const TimeNsec& nsecs, TimeNsec& remain)
00333 {
00334 int r;
00335
00336 if ((r = ::nanosleep(&nsecs, &remain)) > 0)
00337 throw Error(r);
00338 }
00339
00340
00341
00342
00343 void Clock::nanosleep(const TimeNsec& nsecs)
00344 {
00345 int r;
00346
00347 if ((r = ::nanosleep(&nsecs, NULL)) > 0)
00348 throw Error(r);
00349 }
00350
00351
00352
00353
00354 void Clock::getcpuclockid(pid_t pid, Clock& clock)
00355 {
00356 #if _POSIX_CPUTIME > 0
00357 int r;
00358
00359 if ((r = ::clock_getcpuclockid(pid, &clock.clock_id)) > 0)
00360 throw Error(r);
00361 #else
00362 throw Error(ENOSYS);
00363 #endif
00364 }
00365
00366
00367
00368
00369 void Clock::usleep(useconds_t usecs)
00370 {
00371 if (::usleep(usecs) == -1)
00372 throw Error(errno);
00373 }
00374
00375
00376
00377
00378 void RealtimeTimer::create(const Clock& clock, struct sigevent *sig)
00379 {
00380 if (::timer_create(clock, sig, &timer_id) == -1)
00381 throw Error(errno);
00382 }
00383
00384
00385
00386
00387 void RealtimeTimer::del(void)
00388 {
00389 if (::timer_delete(timer_id) == -1)
00390 throw Error(errno);
00391 }
00392
00393
00394
00395
00396 void RealtimeTimer::get(struct itimerspec& val)
00397 {
00398 if (::timer_gettime(timer_id, &val) == -1)
00399 throw Error(errno);
00400 }
00401
00402
00403
00404
00405 void RealtimeTimer::set(int flags, const struct itimerspec& val, struct itimerspec& oval)
00406 {
00407 if (::timer_settime(timer_id, flags, &val, &oval) == -1)
00408 throw Error(errno);
00409 }
00410
00411
00412
00413
00414 void IntervalTimer::get(struct itimerval& val)
00415 {
00416 if (::getitimer(which_timer, &val) == -1)
00417 throw Error(errno);
00418 }
00419
00420
00421
00422
00423 void IntervalTimer::set(const struct itimerval& val, struct itimerval& oval)
00424 {
00425 if (::setitimer(which_timer, &val, &oval) == -1)
00426 throw Error(errno);
00427 }
00428
00429 #if 0
00430
00431 void TimeMsec::now(void)
00432 {
00433 errno = 0;
00434 if (::gettimeofday(this, NULL) != 0) {
00435 if (errno == 0)
00436 errno = EINVAL;
00437 throw Error(errno);
00438 }
00439 }
00440
00441 #endif