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