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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Variables, methods, and tools for internal time representation
18 /****************************************************************************/
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <sstream>
25 #include <iostream>
26 #include "SUMOTime.h"
27 #include "StringTokenizer.h"
28 #include "StringUtils.h"
29 #include "StdDefs.h"
30 
31 
32 // ===========================================================================
33 // type definitions
34 // ===========================================================================
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
42 string2time(const std::string& r) {
43  if (r.find(":") == std::string::npos) {
44  const double time = StringUtils::toDouble(r);
45  if (time > STEPS2TIME(SUMOTime_MAX)) {
46  throw ProcessError("Input string '" + r + "' exceeds the time value range.");
47  }
48  return TIME2STEPS(time);
49  } else {
50  // try to parse jj:hh:mm:ss.s
51  std::vector<std::string> hrt = StringTokenizer(r, ":").getVector();
52  if (hrt.size() == 3) {
53  //std::cout << "parsed '" << r << "' as " << (3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2])) << "\n";
54  return 3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2]);
55  } else if (hrt.size() == 4) {
56  //std::cout << "parsed '" << r << "' as " << (24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3])) << "\n";
57  return 24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3]);
58  }
59  throw ProcessError("Input string '" + r + "' is not a valid time format (jj:HH:MM:SS.S).");
60  }
61 }
62 
63 
64 std::string
66  std::ostringstream oss;
67  oss.setf(oss.fixed);
68  oss.precision(gPrecision);
69  if (gHumanReadableTime) {
70  // 123456 -> "00:00:12.34"
71  double s = STEPS2TIME(t);
72  if (s > 3600 * 24) {
73  // days
74  oss << (long long)(s / (3600 * 24)) << ":";
75  s = fmod(s, 3600 * 24);
76  }
77  // hours, pad with zero
78  if (s / 3600 < 10 && s >= 0) {
79  oss << "0";
80  }
81  oss << (int)(s / 3600) << ":";
82  // minutes, pad with zero
83  s = fmod(s, 3600);
84  if (s / 60 < 10 && s >= 0) {
85  oss << "0";
86  }
87  oss << (int)(s / 60) << ":";
88  // seconds, pad with zero
89  s = fmod(s, 60);
90  if (s < 10 && s >= 0) {
91  oss << "0";
92  }
93  if (fmod(s, 1) == 0 && TS == 1) {
94  oss << (int)s;
95  } else {
96  oss << s;
97  }
98  } else if (t == 0) {
99  // needed due #5926
100  oss << "0.00";
101  } else {
102  // 123456 -> "12.34"
103  oss << STEPS2TIME(t);
104  }
105  return oss.str();
106 }
107 
108 
109 /****************************************************************************/
110 
SUMOTime.h
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:313
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:59
TS
#define TS
Definition: SUMOTime.h:44
StringTokenizer
Definition: StringTokenizer.h:62
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:57
ProcessError
Definition: UtilExceptions.h:40
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
StringUtils.h
gHumanReadableTime
bool gHumanReadableTime
Definition: StdDefs.cpp:29
StringTokenizer::getVector
std::vector< std::string > getVector()
return vector of strings
Definition: StringTokenizer.cpp:192
config.h
StringTokenizer.h
gPrecision
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:27
StdDefs.h
SUMOTime_MAX
#define SUMOTime_MAX
Definition: SUMOTime.h:36