Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
SUMOTime.cpp
Go to the documentation of this file.
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/****************************************************************************/
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// ===========================================================================
39
40
41// ===========================================================================
42// method definitions
43// ===========================================================================
44
46string2time(const std::string& r) {
47 if (r.find(":") == std::string::npos) {
48 const double time = StringUtils::toDouble(r);
49 if (time > STEPS2TIME(SUMOTime_MAX)) {
50 throw TimeFormatException("Input string '" + r + "' exceeds the time value range.");
51 }
52 return TIME2STEPS(time);
53 } else {
54 // try to parse jj:hh:mm:ss.s
55 std::vector<std::string> hrt = StringTokenizer(r, ":").getVector();
56 if (hrt.size() == 3) {
57 //std::cout << "parsed '" << r << "' as " << (3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2])) << "\n";
58 return 3600 * string2time(hrt[0]) + 60 * string2time(hrt[1]) + string2time(hrt[2]);
59 } 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 return 24 * 3600 * string2time(hrt[0]) + 3600 * string2time(hrt[1]) + 60 * string2time(hrt[2]) + string2time(hrt[3]);
62 }
63 throw TimeFormatException("Input string '" + r + "' is not a valid time format (jj:HH:MM:SS.S).");
64 }
65}
66
67
68std::string
69time2string(SUMOTime t, bool humanReadable) {
70 std::ostringstream oss;
71 if (t < 0) {
72 oss << "-";
73 }
74 // needed for signed zero errors, see #5926
75 t = llabs(t);
76 SUMOTime scale = (SUMOTime)pow(10, MAX2(0, 3 - gPrecision));
77 if (scale > 1) {
78 if (t != SUMOTime_MAX) {
79 t = (t + scale / 2) / scale;
80 } else {
81 scale = 1;
82 }
83 }
84 const SUMOTime second = TIME2STEPS(1) / scale;
85 if (humanReadable) {
86 const SUMOTime minute = 60 * second;
87 const SUMOTime hour = 60 * minute;
88 const SUMOTime day = 24 * hour;
89 // 123456 -> "00:00:12.34"
90 if (t > day) {
91 oss << t / day << ":";
92 t %= day;
93 }
94 oss << std::setfill('0') << std::setw(2);
95 oss << t / hour << ":";
96 t %= hour;
97 oss << std::setw(2) << t / minute << ":";
98 t %= minute;
99 oss << std::setw(2) << t / second;
100 t %= second;
101 if (t != 0 || TS < 1.) {
102 oss << ".";
103 oss << std::setw(MIN2(3, gPrecision));
104 oss << t;
105 }
106 } else {
107 oss << t / second << ".";
108 oss << std::setfill('0') << std::setw(MIN2(3, gPrecision));
109 oss << t % second;
110 }
111 return oss.str();
112}
113
114
115std::string
119
120
121std::string
122elapsedMs2string(long long int t) {
123 if (gHumanReadableTime) {
124 if (STEPS2TIME(t) > 60) {
125 // round to seconds
126 return time2string((t / 1000) * 1000);
127 } else {
128 return toString((double)t / 1000.0) + "s";
129 }
130 } else {
131 return time2string(t) + "s";
132 }
133}
134
135bool checkStepLengthMultiple(const SUMOTime t, const std::string& error, SUMOTime deltaT, SUMOTime begin) {
136 if (begin % deltaT == 0) {
137 if (t % deltaT != 0) {
138 WRITE_WARNING("The given time value " + time2string(t) + " is not a multiple of the step length " + time2string(deltaT) + error + ".")
139 }
140 } else {
141 if ((t - begin) % deltaT != 0) {
142 WRITE_WARNING("The given time value " + time2string(t) + " is not reached with step length " + time2string(deltaT)
143 + " and begin time " + time2string(begin) + error + ".")
144 }
145 }
146 // next line used to fix build
147 return false;
148}
149
150void checkTimeBounds(const double time) {
151 if (time > STEPS2TIME(SUMOTime_MAX)) {
152 throw TimeFormatException("Input time " + toString(time) + "s exceeds the time value range.");
153 }
154}
155
156
157/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNING(msg)
Definition MsgHandler.h:295
void checkTimeBounds(const double time)
check the valid SUMOTime range of double input and throw an error if out of bounds
Definition SUMOTime.cpp:150
std::string elapsedMs2string(long long int t)
convert ms to string for log output
Definition SUMOTime.cpp:122
SUMOTime DELTA_T
Definition SUMOTime.cpp:38
bool checkStepLengthMultiple(const SUMOTime t, const std::string &error, SUMOTime deltaT, SUMOTime begin)
check if given SUMOTime is multiple of the step length
Definition SUMOTime.cpp:135
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:46
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
#define STEPS2TIME(x)
Definition SUMOTime.h:55
#define SUMOTime_MAX
Definition SUMOTime.h:34
#define TS
Definition SUMOTime.h:42
#define TIME2STEPS(x)
Definition SUMOTime.h:57
int gPrecision
the precision for floating point outputs
Definition StdDefs.cpp:26
bool gHumanReadableTime
Definition StdDefs.cpp:29
T MIN2(T a, T b)
Definition StdDefs.h:76
T MAX2(T a, T b)
Definition StdDefs.h:82
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
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