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 /****************************************************************************/
16 // Variables, methods, and tools for internal time representation
17 /****************************************************************************/
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
23 #include <sstream>
24 #include <iostream>
25 #include <iomanip>
26 #include "SUMOTime.h"
27 #include "StringTokenizer.h"
28 #include "StringUtils.h"
29 #include "StdDefs.h"
30 #include "MsgHandler.h"
31 
32 
33 // ===========================================================================
34 // type definitions
35 // ===========================================================================
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
42 
44 string2time(const std::string& r) {
45  if (r.find(":") == std::string::npos) {
46  const double time = StringUtils::toDouble(r);
47  if (time > STEPS2TIME(SUMOTime_MAX)) {
48  throw TimeFormatException("Input string '" + r + "' 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 TimeFormatException("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  if (t < 0) {
70  oss << "-";
71  }
72  // needed for signed zero errors, see #5926
73  t = abs(t);
74  const SUMOTime scale = (SUMOTime)pow(10, MAX2(0, 3 - gPrecision));
75  if (scale > 1) {
76  t = (t + scale / 2) / scale;
77  }
78  const SUMOTime second = TIME2STEPS(1) / scale;
79  if (gHumanReadableTime) {
80  const SUMOTime minute = 60 * second;
81  const SUMOTime hour = 60 * minute;
82  const SUMOTime day = 24 * hour;
83  // 123456 -> "00:00:12.34"
84  if (t > day) {
85  oss << t / day << ":";
86  t %= day;
87  }
88  oss << std::setfill('0') << std::setw(2);
89  oss << t / hour << ":";
90  t %= hour;
91  oss << std::setw(2) << t / minute << ":";
92  t %= minute;
93  oss << std::setw(2) << t / second;
94  t %= second;
95  if (t != 0 || TS != 1.) {
96  oss << std::setw(MIN2(3, gPrecision));
97  oss << "." << t;
98  }
99  } else {
100  oss << t / second << ".";
101  oss << std::setfill('0') << std::setw(MIN2(3, gPrecision));
102  oss << t % second;
103  }
104  return oss.str();
105 }
106 
107 
108 bool checkStepLengthMultiple(const SUMOTime t, const std::string& error) {
109  if (t % DELTA_T != 0) {
110  WRITE_WARNING("The given time value " + time2string(t) + " is not a multiple of the step length " + time2string(DELTA_T) + error + ".")
111  }
112  // next line used to fix build
113  return false;
114 }
115 
116 
117 /****************************************************************************/
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:73
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
SUMOTime.h
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:36
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:345
MsgHandler.h
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:79
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:58
TS
#define TS
Definition: SUMOTime.h:43
StringTokenizer
Definition: StringTokenizer.h:61
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:56
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:67
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
StringUtils.h
gHumanReadableTime
bool gHumanReadableTime
Definition: StdDefs.cpp:28
TimeFormatException
Definition: UtilExceptions.h:108
checkStepLengthMultiple
bool checkStepLengthMultiple(const SUMOTime t, const std::string &error)
Definition: SUMOTime.cpp:108
StringTokenizer::getVector
std::vector< std::string > getVector()
return vector of strings
Definition: StringTokenizer.cpp:191
config.h
StringTokenizer.h
gPrecision
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:26
StdDefs.h
SUMOTime_MAX
#define SUMOTime_MAX
Definition: SUMOTime.h:35