Eclipse SUMO - Simulation of Urban MObility
SUMOTime.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 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 /****************************************************************************/
20 // Variables, methods, and tools for internal time representation
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <sstream>
25 #include <iostream>
26 #include <iomanip>
27 #include "SUMOTime.h"
28 #include "StringTokenizer.h"
29 #include "StringUtils.h"
30 #include "StdDefs.h"
31 #include "MsgHandler.h"
32 
33 
34 // ===========================================================================
35 // type definitions
36 // ===========================================================================
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
43 
45 string2time(const std::string& r) {
46  if (r.find(":") == std::string::npos) {
47  const double time = StringUtils::toDouble(r);
48  if (time > STEPS2TIME(SUMOTime_MAX)) {
49  throw TimeFormatException("Input string '" + r + "' exceeds the time value range.");
50  }
51  return TIME2STEPS(time);
52  } else {
53  // try to parse jj:hh:mm:ss.s
54  std::vector<std::string> hrt = StringTokenizer(r, ":").getVector();
55  if (hrt.size() == 3) {
56  //std::cout << "parsed '" << r << "' as " << (3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2])) << "\n";
57  return 3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2]);
58  } else if (hrt.size() == 4) {
59  //std::cout << "parsed '" << r << "' as " << (24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3])) << "\n";
60  return 24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3]);
61  }
62  throw TimeFormatException("Input string '" + r + "' is not a valid time format (jj:HH:MM:SS.S).");
63  }
64 }
65 
66 
67 std::string
69  std::ostringstream oss;
70  if (t < 0) {
71  oss << "-";
72  }
73  // needed for signed zero errors, see #5926
74  t = abs(t);
75  const SUMOTime scale = (SUMOTime)pow(10, MAX2(0, 3 - gPrecision));
76  if (scale > 1 && t != SUMOTime_MAX) {
77  t = (t + scale / 2) / scale;
78  }
79  const SUMOTime second = TIME2STEPS(1) / scale;
80  if (gHumanReadableTime) {
81  const SUMOTime minute = 60 * second;
82  const SUMOTime hour = 60 * minute;
83  const SUMOTime day = 24 * hour;
84  // 123456 -> "00:00:12.34"
85  if (t > day) {
86  oss << t / day << ":";
87  t %= day;
88  }
89  oss << std::setfill('0') << std::setw(2);
90  oss << t / hour << ":";
91  t %= hour;
92  oss << std::setw(2) << t / minute << ":";
93  t %= minute;
94  oss << std::setw(2) << t / second;
95  t %= second;
96  if (t != 0 || TS < 1.) {
97  oss << std::setw(MIN2(3, gPrecision));
98  oss << "." << t;
99  }
100  } else {
101  oss << t / second << ".";
102  oss << std::setfill('0') << std::setw(MIN2(3, gPrecision));
103  oss << t % second;
104  }
105  return oss.str();
106 }
107 
108 std::string
109 elapsedMs2string(long long int t) {
110  if (gHumanReadableTime) {
111  if (STEPS2TIME(t) > 60) {
112  // round to seconds
113  return time2string((t / 1000) * 1000);
114  } else {
115  return toString(t / 1000.0) + "s";
116  }
117  } else {
118  return time2string(t) + "s";
119  }
120 }
121 
122 bool checkStepLengthMultiple(const SUMOTime t, const std::string& error, SUMOTime deltaT) {
123  if (t % deltaT != 0) {
124  WRITE_WARNING("The given time value " + time2string(t) + " is not a multiple of the step length " + time2string(deltaT) + error + ".")
125  }
126  // next line used to fix build
127  return false;
128 }
129 
130 
131 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
bool checkStepLengthMultiple(const SUMOTime t, const std::string &error, SUMOTime deltaT)
check if given SUMOTime is multiple of the step length
Definition: SUMOTime.cpp:122
std::string elapsedMs2string(long long int t)
convert ms to string for log output
Definition: SUMOTime.cpp:109
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
#define STEPS2TIME(x)
Definition: SUMOTime.h:53
#define SUMOTime_MAX
Definition: SUMOTime.h:32
#define TS
Definition: SUMOTime.h:40
#define TIME2STEPS(x)
Definition: SUMOTime.h:55
long long int SUMOTime
Definition: SUMOTime.h:31
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:25
bool gHumanReadableTime
Definition: StdDefs.cpp:27
T MIN2(T a, T b)
Definition: StdDefs.h:73
T MAX2(T a, T b)
Definition: StdDefs.h:79
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
std::vector< std::string > getVector()
return vector of strings
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter