LCOV - code coverage report
Current view: top level - DCPS - TimeDuration.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 47 97.9 %
Date: 2023-04-30 01:32:43 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include <DCPS/DdsDcps_pch.h> // Only the _pch include should start with DCPS/
       2             : 
       3             : #include "TimeDuration.h"
       4             : 
       5             : #include "SafetyProfileStreams.h"
       6             : 
       7             : #include <cmath>
       8             : #include <cstdio>
       9             : #ifdef ACE_HAS_CPP11
      10             : #  include <limits>
      11             : #else
      12             : #  include <ace/Numeric_Limits.h>
      13             : #endif /* ACE_HAS_CPP11*/
      14             : 
      15             : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
      16             : 
      17             : namespace OpenDDS {
      18             : namespace DCPS {
      19             : 
      20             : const TimeDuration TimeDuration::zero_value(0, 0);
      21             : const TimeDuration TimeDuration::max_value(
      22             : #ifdef ACE_HAS_CPP11
      23             :   std::numeric_limits<time_t>::max(),
      24             : #else
      25             :   ACE_Numeric_Limits<time_t>::max(),
      26             : #endif
      27             :   ACE_ONE_SECOND_IN_USECS - 1);
      28             : 
      29             : namespace {
      30          31 :   time_t usec_to_rounded_frac(
      31             :     suseconds_t value, unsigned decimal_places, time_t& carry)
      32             :   {
      33          31 :     const double frac = static_cast<double>(value) / ACE_ONE_SECOND_IN_USECS;
      34          31 :     const double denominator = std::pow(10.0, static_cast<double>(decimal_places));
      35          31 :     const double numerator = std::floor(frac * denominator + 0.5);
      36          31 :     if (numerator == denominator) {
      37           4 :       carry = 1;
      38           4 :       return 0;
      39             :     }
      40          27 :     carry = 0;
      41          27 :     return static_cast<time_t>(numerator);
      42             :   }
      43             : 
      44          33 :   String to_zero_pad_str(time_t value, unsigned len = 2)
      45             :   {
      46          33 :     const String nopad = to_dds_string(value);
      47          33 :     if (len > nopad.size()) {
      48          36 :       return String(len - nopad.size(), '0') + nopad;
      49             :     }
      50          15 :     return nopad;
      51          33 :   }
      52             : }
      53             : 
      54          31 : String TimeDuration::str(unsigned decimal_places, bool just_sec) const
      55             : {
      56          31 :   String rv;
      57          31 :   time_t sec = value().sec();
      58          31 :   suseconds_t usec = value().usec();
      59          31 :   bool negative = false;
      60          31 :   if (sec < 0) {
      61           2 :     negative = true;
      62           2 :     sec = -sec;
      63             :   }
      64          31 :   if (usec < 0) {
      65           2 :     negative = true;
      66           2 :     usec = -usec;
      67             :   }
      68          31 :   if (negative) {
      69           3 :     rv += "-";
      70             :   }
      71             :   time_t carry;
      72          31 :   const time_t numerator = usec_to_rounded_frac(usec, decimal_places, carry);
      73          31 :   const time_t seconds_total = sec + carry;
      74          31 :   const time_t minutes_total = seconds_total / 60;
      75          31 :   just_sec = just_sec || minutes_total == 0;
      76          31 :   if (just_sec) {
      77          21 :     rv += to_dds_string(seconds_total);
      78             :   } else {
      79          10 :     const time_t seconds = seconds_total % 60;
      80          10 :     const time_t minutes = minutes_total % 60;
      81          10 :     const time_t hours = minutes_total / 60;
      82          10 :     if (hours > 0) {
      83           6 :       rv += to_dds_string(hours) + ":" + to_zero_pad_str(minutes);
      84             :     } else {
      85           4 :       rv += to_dds_string(minutes);
      86             :     }
      87          10 :     rv += ":" + to_zero_pad_str(seconds);
      88             :   }
      89          31 :   if (decimal_places > 0) {
      90          17 :     rv += "." + to_zero_pad_str(numerator, decimal_places);
      91             :   }
      92          31 :   if (just_sec) {
      93          21 :     rv += " s";
      94             :   }
      95          62 :   return rv;
      96           0 : }
      97             : 
      98             : }
      99             : }
     100             : 
     101             : OPENDDS_END_VERSIONED_NAMESPACE_DECL
     102             : 
     103             : #if !defined (__ACE_INLINE__)
     104             : #  include "TimeDuration.inl"
     105             : #endif /* __ACE_INLINE__ */

Generated by: LCOV version 1.16