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-2018 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 "TplConvert.h"
28 #include "StringTokenizer.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  double time;
45  std::istringstream buf(r);
46  buf >> time;
47  if (buf.fail() || time > STEPS2TIME(SUMOTime_MAX)) {
48  throw ProcessError("Input string '" + r + "' is not a valid number or exceeds the time value range.");
49  }
50  return TIME2STEPS(time);
51  } else {
52  // try to parse jj:hh:mm:ss.s
53  std::vector<std::string> hrt = StringTokenizer(r, ":").getVector();
54  if (hrt.size() == 3) {
55  //std::cout << "parsed '" << r << "' as " << (3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2])) << "\n";
56  return 3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2]);
57  } else if (hrt.size() == 4) {
58  //std::cout << "parsed '" << r << "' as " << (24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3])) << "\n";
59  return 24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3]);
60  }
61  throw ProcessError("Input string '" + r + "' is not a valid time format (jj:HH:MM:SS.S).");
62  }
63 }
64 
65 
66 std::string
68  std::ostringstream oss;
69  oss.setf(oss.fixed);
70  oss.precision(gPrecision);
71  if (gHumanReadableTime) {
72  // 123456 -> "00:00:12.34"
73  double s = STEPS2TIME(t);
74  if (s > 3600 * 24) {
75  // days
76  oss << (int)(s / (3600 * 24)) << ":";
77  s = fmod(s, 3600 * 24);
78  }
79  // hours, pad with zero
80  if (s / 3600 < 10) {
81  oss << "0";
82  }
83  oss << (int)(s / 3600) << ":";
84  // minutes, pad with zero
85  s = fmod(s, 3600);
86  if (s / 60 < 10) {
87  oss << "0";
88  }
89  oss << (int)(s / 60) << ":";
90  s = fmod(s, 60);
91  if (s < 10) {
92  oss << "0";
93  }
94  if (fmod(s, 1) == 0) {
95  oss << (int)s;
96  } else {
97  oss << s;
98  }
99 
100  } else {
101  // 123456 -> "12.34"
102  oss << STEPS2TIME(t);
103  }
104  return oss.str();
105 }
106 
107 
108 /****************************************************************************/
109 
long long int SUMOTime
Definition: SUMOTime.h:36
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:27
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:67
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
#define STEPS2TIME(x)
Definition: SUMOTime.h:58
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
std::vector< std::string > getVector()
#define SUMOTime_MAX
Definition: SUMOTime.h:37
bool gHumanReadableTime
Definition: StdDefs.cpp:29