LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - time_util.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 31 32 96.9 %
Date: 2026-09-28 02:13:17 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #ifndef BASE_TIME_UTIL_H__
       6             : #define BASE_TIME_UTIL_H__
       7             : 
       8             : #include <sstream>
       9             : 
      10             : #include <boost/date_time/posix_time/posix_time.hpp>
      11             : #include <boost/date_time/posix_time/posix_time_types.hpp>
      12             : /* timestamp - returns usec since epoch */
      13    19363910 : static inline uint64_t UTCTimestampUsec() {
      14             :     struct timespec ts;
      15    19363910 :     if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
      16         188 :         assert(0);
      17             :     }
      18             : 
      19    19379675 :     return ts.tv_sec * 1000000 + ts.tv_nsec/1000;
      20             : }
      21             : 
      22             : /* timestamp - returns seconds since epoch */
      23        1697 : static inline time_t UTCTimestamp() {
      24        1697 :     return time(NULL);
      25             : }
      26             : 
      27             : // Monotonically increasing timer starting from an arbitrary value
      28             : // 10x more efficient than UTCTimestampUsec
      29         962 : static inline uint64_t ClockMonotonicUsec() {
      30             :     struct timespec ts;
      31         962 :     if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
      32           0 :         assert(0);
      33             :     }
      34             : 
      35         962 :     return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
      36             : }
      37             : 
      38        3552 : static inline boost::posix_time::ptime UTCUsecToPTime(uint64_t tusec) {
      39             :     typedef boost::posix_time::time_duration::sec_type sec_type;
      40             :     typedef boost::posix_time::time_duration::fractional_seconds_type frac_type;
      41             : 
      42        3552 :     int64_t tps = boost::posix_time::time_duration::ticks_per_second();
      43        3552 :     sec_type seconds = static_cast<sec_type>(tusec / 1000000);
      44        3552 :     frac_type frac_seconds =
      45        3552 :         static_cast<frac_type>(tps / 1000000 * (tusec % 1000000));
      46             : 
      47             :     boost::posix_time::ptime pt(
      48        3552 :         boost::gregorian::date(1970, 1, 1),
      49        7104 :         boost::posix_time::time_duration(0, 0, seconds, frac_seconds));
      50             : 
      51        3552 :     return pt;
      52             : }
      53             : 
      54        3674 : static inline std::string UTCUsecToString(uint64_t tstamp) {
      55        3674 :     if (tstamp == 0)
      56         122 :         return "";
      57        3552 :     std::stringstream ss;
      58        3552 :     ss << UTCUsecToPTime(tstamp);
      59        3552 :     return ss.str();
      60        3552 : }
      61             : 
      62         815 : static inline const std::string duration_usecs_to_string(const uint64_t usecs) {
      63         815 :     std::ostringstream os;
      64         815 :     boost::posix_time::time_duration duration;
      65             : 
      66         815 :     duration = boost::posix_time::microseconds(usecs);
      67         815 :     os << duration;
      68        1630 :     return os.str();
      69         815 : }
      70             : 
      71             : #endif  // BASE_TIME_UTIL_H__

Generated by: LCOV version 1.14