LCOV - code coverage report
Current view: top level - src/utils/common - SUMOTime.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 95.2 % 63 60
Test Date: 2024-11-22 15:46:21 Functions: 100.0 % 6 6

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    SUMOTime.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @author  Mirko Barthauer
      19              : /// @date    Fri, 29.04.2005
      20              : ///
      21              : // Variables, methods, and tools for internal time representation
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #include <sstream>
      26              : #include <iostream>
      27              : #include <iomanip>
      28              : #include "SUMOTime.h"
      29              : #include "StringTokenizer.h"
      30              : #include "StringUtils.h"
      31              : #include "StdDefs.h"
      32              : #include "MsgHandler.h"
      33              : 
      34              : 
      35              : // ===========================================================================
      36              : // type definitions
      37              : // ===========================================================================
      38              : SUMOTime DELTA_T = 1000;
      39              : 
      40              : 
      41              : // ===========================================================================
      42              : // method definitions
      43              : // ===========================================================================
      44              : 
      45              : SUMOTime
      46     10477002 : string2time(const std::string& r) {
      47     10477002 :     if (r.find(":") == std::string::npos) {
      48     10476377 :         const double time = StringUtils::toDouble(r);
      49     10476175 :         if (time > STEPS2TIME(SUMOTime_MAX)) {
      50           18 :             throw TimeFormatException("Input string '" + r + "' exceeds the time value range.");
      51              :         }
      52     11716888 :         return TIME2STEPS(time);
      53              :     } else {
      54              :         // try to parse jj:hh:mm:ss.s
      55         1875 :         std::vector<std::string> hrt = StringTokenizer(r, ":").getVector();
      56          625 :         if (hrt.size() == 3) {
      57              :             //std::cout << "parsed '" << r << "' as " << (3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2])) << "\n";
      58          246 :             return 3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2]);
      59          379 :         } else if (hrt.size() == 4) {
      60              :             //std::cout << "parsed '" << r << "' as " << (24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3])) << "\n";
      61          379 :             return 24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3]);
      62              :         }
      63            0 :         throw TimeFormatException("Input string '" + r + "' is not a valid time format (jj:HH:MM:SS.S).");
      64          625 :     }
      65              : }
      66              : 
      67              : 
      68              : std::string
      69     10774521 : time2string(SUMOTime t, bool humanReadable) {
      70     10774521 :     std::ostringstream oss;
      71     10774521 :     if (t < 0) {
      72      3252671 :         oss << "-";
      73              :     }
      74              :     // needed for signed zero errors, see #5926
      75              :     // llabs(SUMOTime_MIN) would create overflow and must be handleed separately
      76     10774521 :     t = t == SUMOTime_MIN ? SUMOTime_MAX : (SUMOTime)llabs(t);
      77     10774521 :     SUMOTime scale = (SUMOTime)pow(10, MAX2(0, 3 - gPrecision));
      78     10774521 :     if (scale > 1) {
      79     10763514 :         if (t != SUMOTime_MAX) {
      80      7512960 :             t = (t + scale / 2) / scale;
      81              :         } else {
      82              :             scale = 1;
      83              :         }
      84              :     }
      85     10774521 :     const SUMOTime second = TIME2STEPS(1) / scale;
      86     10774521 :     if (humanReadable) {
      87          875 :         const SUMOTime minute = 60 * second;
      88          875 :         const SUMOTime hour = 60 * minute;
      89          875 :         const SUMOTime day = 24 * hour;
      90              :         // 123456 -> "00:00:12.34"
      91          875 :         if (t > day) {
      92          284 :             oss << t / day << ":";
      93          284 :             t %= day;
      94              :         }
      95              :         oss << std::setfill('0') << std::setw(2);
      96          875 :         oss << t / hour << ":";
      97          875 :         t %= hour;
      98          875 :         oss << std::setw(2) << t / minute << ":";
      99          875 :         t %= minute;
     100          875 :         oss << std::setw(2) << t / second;
     101          875 :         t %= second;
     102          875 :         if (t != 0 || TS < 1.) {
     103          260 :             oss << ".";
     104          260 :             oss << std::setw(MIN2(3, gPrecision));
     105              :             oss << t;
     106              :         }
     107              :     } else {
     108     10773646 :         oss << t / second << ".";
     109     10773646 :         oss << std::setfill('0') << std::setw(MIN2(3, gPrecision));
     110     10773646 :         oss << t % second;
     111              :     }
     112     10774521 :     return oss.str();
     113     10774521 : }
     114              : 
     115              : 
     116              : std::string
     117     10774521 : time2string(SUMOTime t) {
     118     10774521 :     return time2string(t, gHumanReadableTime);
     119              : }
     120              : 
     121              : 
     122              : std::string
     123        34676 : elapsedMs2string(long long int t) {
     124        34676 :     if (gHumanReadableTime) {
     125           56 :         if (STEPS2TIME(t) > 60) {
     126              :             // round to seconds
     127            0 :             return time2string((t / 1000) * 1000);
     128              :         } else {
     129          112 :             return toString((double)t / 1000.0) + "s";
     130              :         }
     131              :     } else {
     132        69240 :         return time2string(t) + "s";
     133              :     }
     134              : }
     135              : 
     136        76343 : bool checkStepLengthMultiple(const SUMOTime t, const std::string& error, SUMOTime deltaT, SUMOTime begin) {
     137        76343 :     if (begin % deltaT == 0) {
     138        76331 :         if (t % deltaT != 0) {
     139          644 :             WRITE_WARNING("The given time value " + time2string(t) + " is not a multiple of the step length " + time2string(deltaT) + error + ".")
     140              :         }
     141              :     } else {
     142           12 :         if ((t - begin) % deltaT != 0) {
     143           30 :             WRITE_WARNING("The given time value " + time2string(t) + " is not reached with step length " + time2string(deltaT)
     144              :                           + " and begin time " + time2string(begin) + error + ".")
     145              :         }
     146              :     }
     147              :     // next line used to fix build
     148        76343 :     return false;
     149              : }
     150              : 
     151        99878 : void checkTimeBounds(const double time) {
     152        99878 :     if (time > STEPS2TIME(SUMOTime_MAX)) {
     153            0 :         throw TimeFormatException("Input time " + toString(time) + "s exceeds the time value range.");
     154              :     }
     155        99878 : }
     156              : 
     157              : 
     158              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1