time.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <utils/time/time.h>
00026 #include <utils/time/clock.h>
00027
00028 #include <core/exception.h>
00029
00030 #include <time.h>
00031 #include <cmath>
00032 #include <cstdio>
00033 #include <cstdlib>
00034 #include <cstring>
00035 #include <unistd.h>
00036
00037 namespace fawkes {
00038 #if 0
00039 }
00040 #endif
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 const unsigned int Time::TIMESTR_SIZE = 26;
00052
00053
00054
00055
00056
00057 Time::Time()
00058 {
00059 __clock = Clock::instance();
00060 __clock->get_time(&__time);
00061 __timestr = NULL;
00062 }
00063
00064
00065
00066
00067
00068
00069 Time::Time(const timeval* tv)
00070 {
00071 __time.tv_sec = tv->tv_sec;
00072 __time.tv_usec = tv->tv_usec;
00073 __clock = Clock::instance();
00074 __timestr = NULL;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 Time::Time(long sec, long usec, Clock *clock)
00086 {
00087 __time.tv_sec = sec;
00088 __time.tv_usec = usec;
00089 if (clock) {
00090 __clock = clock;
00091 } else {
00092 __clock = Clock::instance();
00093 }
00094 __timestr = NULL;
00095 }
00096
00097
00098
00099
00100
00101
00102 Time::Time(long ms)
00103 {
00104 time_t sec = (time_t) (ms / 1000.0);
00105 suseconds_t usec = (ms % 1000) * 1000;
00106
00107 __time.tv_sec = sec;
00108 __time.tv_usec = usec;
00109 __clock = Clock::instance();
00110 __timestr = NULL;
00111 }
00112
00113
00114
00115
00116
00117
00118 Time::Time(float s)
00119 {
00120 time_t sec = (time_t) s;
00121 suseconds_t usec = (suseconds_t)roundf((s - sec) * 1000000.f);
00122
00123 __time.tv_sec = sec;
00124 __time.tv_usec = usec;
00125 __clock = Clock::instance();
00126 __timestr = NULL;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135 Time::Time(Clock *clock)
00136 {
00137 this->__clock = clock;
00138 __clock->get_time(&__time);
00139 __timestr = NULL;
00140 }
00141
00142
00143
00144
00145
00146 Time::Time(const Time &t)
00147 {
00148 __time.tv_sec = t.__time.tv_sec;
00149 __time.tv_usec = t.__time.tv_usec;
00150 __clock = t.__clock;
00151 if (t.__timestr) {
00152 __timestr = (char *)malloc(TIMESTR_SIZE);
00153 strncpy(__timestr, t.__timestr, TIMESTR_SIZE);
00154 } else {
00155 __timestr = NULL;
00156 }
00157 }
00158
00159
00160
00161 Time::~Time()
00162 {
00163 if (__timestr) free(__timestr);
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173 float
00174 Time::in_sec() const
00175 {
00176 return (__time.tv_sec + __time.tv_usec / 1000000.f);
00177 }
00178
00179
00180
00181
00182
00183 long
00184 Time::in_msec() const
00185 {
00186 return (__time.tv_sec * 1000 + (long) (__time.tv_usec / 1000));
00187 }
00188
00189
00190
00191
00192
00193 long
00194 Time::in_usec() const
00195 {
00196 return (__time.tv_sec * 1000000 + __time.tv_usec);
00197 }
00198
00199
00200
00201
00202
00203 const timeval *
00204 Time::get_timeval() const
00205 {
00206 return &__time;
00207 }
00208
00209
00210
00211
00212
00213 void
00214 Time::set_time(const timeval* tv)
00215 {
00216 __time.tv_sec = tv->tv_sec;
00217 __time.tv_usec = tv->tv_usec;
00218 }
00219
00220
00221
00222
00223
00224
00225 void
00226 Time::set_time(long int sec, long int usec)
00227 {
00228 __time.tv_sec = sec;
00229 __time.tv_usec = usec;
00230 }
00231
00232
00233
00234
00235
00236 void
00237 Time::set_time(long ms)
00238 {
00239 __time.tv_sec = (time_t) (ms / 1000.0);
00240 __time.tv_usec = (ms % 1000) * 1000;
00241 }
00242
00243
00244
00245
00246
00247 void
00248 Time::set_time(float s)
00249 {
00250 __time.tv_sec = (time_t)floor(s);
00251 __time.tv_usec = (suseconds_t)(s - __time.tv_sec) * 1000000;
00252 }
00253
00254
00255
00256
00257
00258
00259 void
00260 Time::set_time(const Time &t)
00261 {
00262 *this = t;
00263 }
00264
00265
00266
00267
00268
00269 void
00270 Time::set_time(const Time *t)
00271 {
00272 __time.tv_sec = t->__time.tv_sec;
00273 __time.tv_usec = t->__time.tv_usec;
00274 }
00275
00276
00277
00278
00279
00280
00281
00282
00283 void
00284 Time::add(float seconds)
00285 {
00286 *this += seconds;
00287 }
00288
00289
00290
00291
00292
00293 Time
00294 Time::operator+(const Time& t) const
00295 {
00296 Time ret;
00297 if (__time.tv_usec + t.__time.tv_usec >= 1000000)
00298 {
00299 ret.__time.tv_usec = __time.tv_usec + t.__time.tv_usec - 1000000;
00300 ret.__time.tv_sec = __time.tv_sec + t.__time.tv_sec + 1;
00301 }
00302 else
00303 {
00304 ret.__time.tv_usec = __time.tv_usec + t.__time.tv_usec;
00305 ret.__time.tv_sec = __time.tv_sec + t.__time.tv_sec;
00306 }
00307
00308 return ret;
00309 }
00310
00311
00312
00313
00314
00315
00316 Time
00317 Time::operator+(const Time* t) const
00318 {
00319 return *this + *t;
00320 }
00321
00322
00323
00324
00325
00326
00327 Time
00328 Time::operator+(const float sec) const
00329 {
00330 Time ret;
00331 time_t sec_only = (time_t)floor(sec);
00332 suseconds_t usec_only = (suseconds_t)roundf((sec - sec_only) * 1000000);
00333 if ((__time.tv_usec + usec_only) >= 1000000)
00334 {
00335 ret.__time.tv_usec = __time.tv_usec + usec_only - 1000000;
00336 ret.__time.tv_sec = __time.tv_sec + sec_only + 1;
00337 }
00338 else
00339 {
00340 ret.__time.tv_usec = __time.tv_usec + usec_only;
00341 ret.__time.tv_sec = __time.tv_sec + sec_only;
00342 }
00343
00344 return ret;
00345 }
00346
00347
00348
00349
00350
00351
00352 Time
00353 Time::operator-(const Time& t) const
00354 {
00355 Time ret;
00356 if (__time.tv_usec < t.__time.tv_usec)
00357 {
00358 ret.__time.tv_usec = 1000000 + __time.tv_usec - t.__time.tv_usec;
00359 ret.__time.tv_sec = __time.tv_sec - t.__time.tv_sec - 1;
00360 }
00361 else
00362 {
00363 ret.__time.tv_usec = __time.tv_usec - t.__time.tv_usec;
00364 ret.__time.tv_sec = __time.tv_sec - t.__time.tv_sec;
00365 }
00366
00367 return ret;
00368 }
00369
00370
00371
00372
00373
00374
00375 float
00376 Time::operator-(const Time* t) const
00377 {
00378 return time_diff_sec(__time, t->__time);
00379 }
00380
00381
00382
00383
00384
00385
00386 Time &
00387 Time::operator+=(const Time& t)
00388 {
00389 if (__time.tv_usec + t.__time.tv_usec >= 1000000)
00390 {
00391 __time.tv_usec += t.__time.tv_usec - 1000000;
00392 __time.tv_sec += t.__time.tv_sec + 1;
00393 }
00394 else
00395 {
00396 __time.tv_usec += t.__time.tv_usec;
00397 __time.tv_sec += t.__time.tv_sec;
00398 }
00399
00400 return *this;
00401 }
00402
00403
00404
00405
00406
00407
00408 Time &
00409 Time::operator+=(const long int usec)
00410 {
00411 if ( __time.tv_usec + usec >= 1000000 )
00412 {
00413
00414 long int tmp_usec = __time.tv_usec + usec;
00415 __time.tv_usec = tmp_usec % 1000000;
00416 __time.tv_sec += tmp_usec / 1000000;
00417 }
00418 else
00419 {
00420 __time.tv_usec += usec;
00421 }
00422
00423 return *this;
00424 }
00425
00426
00427
00428
00429
00430
00431 Time &
00432 Time::operator+=(const float sec)
00433 {
00434 time_t sec_only = (time_t)floor(sec);
00435 suseconds_t usec_only = (suseconds_t)roundf((sec - sec_only) * 1000000);
00436 if ((__time.tv_usec + usec_only) >= 1000000)
00437 {
00438 __time.tv_usec += usec_only - 1000000;
00439 __time.tv_sec += sec_only + 1;
00440 }
00441 else
00442 {
00443 __time.tv_usec += usec_only;
00444 __time.tv_sec += sec_only;
00445 }
00446
00447 return *this;
00448 }
00449
00450
00451
00452
00453
00454
00455 Time &
00456 Time::operator-=(const Time& t)
00457 {
00458 *this = *this - t;
00459 return *this;
00460 }
00461
00462
00463
00464
00465
00466
00467 Time &
00468 Time::operator=(const Time &t)
00469 {
00470 __time.tv_sec = t.__time.tv_sec;
00471 __time.tv_usec = t.__time.tv_usec;
00472 __clock = t.__clock;
00473 return *this;
00474 }
00475
00476
00477
00478
00479
00480 Time &
00481 Time::stamp()
00482 {
00483 if ( NULL != __clock ) {
00484 __clock->get_time(&__time);
00485 } else {
00486 throw Exception("Clock not set, cannot stamp time");
00487 }
00488 return *this;
00489 }
00490
00491
00492
00493
00494
00495
00496
00497 Time &
00498 Time::stamp_systime()
00499 {
00500 if ( NULL != __clock ) {
00501 __clock->get_systime(&__time);
00502 } else {
00503 throw Exception("Clock not set, cannot stamp time (systime)");
00504 }
00505 return *this;
00506 }
00507
00508
00509
00510
00511
00512
00513
00514 void
00515 Time::wait()
00516 {
00517 Time until, now;
00518 until += *this;
00519
00520
00521 usleep(0);
00522
00523 long int remaining_usec = (until - now).in_usec();
00524 while ( remaining_usec > 0 ) {
00525 usleep(remaining_usec);
00526 now.stamp();
00527 remaining_usec = (until - now).in_usec();
00528 }
00529 }
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539 void
00540 Time::wait_systime()
00541 {
00542 Time until, now;
00543
00544 __clock->get_systime(until);
00545 until += *this;
00546
00547 __clock->get_systime(now);
00548
00549
00550 usleep(0);
00551
00552 long int remaining_usec = (until - now).in_usec();
00553 while ( remaining_usec > 0 ) {
00554 usleep(remaining_usec);
00555 __clock->get_systime(now);
00556 remaining_usec = (until - now).in_usec();
00557 }
00558 }
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569 const char *
00570 Time::str(bool utc)
00571 {
00572
00573 if ( ! __timestr ) __timestr = (char *)malloc(TIMESTR_SIZE);
00574
00575
00576 if (__time.tv_sec < 1000000000) {
00577 #ifdef __FreeBSD__
00578 snprintf(__timestr, TIMESTR_SIZE, "%i:%li", __time.tv_sec, __time.tv_usec);
00579 #else
00580 snprintf(__timestr, TIMESTR_SIZE, "%li:%li", __time.tv_sec, __time.tv_usec);
00581 #endif
00582 } else {
00583 tm time_tm;
00584 if ( utc ) {
00585 gmtime_r( &(__time.tv_sec), &time_tm );
00586 } else {
00587 localtime_r( &(__time.tv_sec), &time_tm );
00588 }
00589 asctime_r(&time_tm, __timestr);
00590 __timestr[strlen(__timestr) - 1] = 0;
00591 }
00592
00593 return __timestr;
00594 }
00595
00596
00597
00598
00599
00600
00601
00602 void
00603 Time::str_r(char *s, bool utc)
00604 {
00605
00606 if (__time.tv_sec < 1000000000) {
00607 #ifdef __FreeBSD__
00608 snprintf(s, TIMESTR_SIZE, "%i:%li", __time.tv_sec, __time.tv_usec);
00609 #else
00610 snprintf(s, TIMESTR_SIZE, "%li:%li", __time.tv_sec, __time.tv_usec);
00611 #endif
00612 } else {
00613 tm time_tm;
00614 if ( utc ) {
00615 gmtime_r( &(__time.tv_sec), &time_tm );
00616 } else {
00617 localtime_r( &(__time.tv_sec), &time_tm );
00618 }
00619 asctime_r(&time_tm, s);
00620 s[strlen(s) - 1] = 0;
00621 }
00622 }
00623
00624 }