Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 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 : /****************************************************************************/
14 : /// @file SUMOTime.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @author Mirko Barthauer
19 : /// @date Fri, 29.04.2005
20 : ///
21 : // Variables, methods, and tools for internal time representation
22 : /****************************************************************************/
23 : #pragma once
24 : #include <config.h>
25 : #include <limits>
26 : #include <string>
27 : #include "UtilExceptions.h"
28 :
29 :
30 : // ===========================================================================
31 : // type definitions
32 : // ===========================================================================
33 : typedef long long int SUMOTime;
34 : #define SUMOTime_MAX (std::numeric_limits<SUMOTime>::max() - 1000)
35 : #define SUMOTime_MIN std::numeric_limits<SUMOTime>::min()
36 : #define SUMOTime_MAX_PERIOD (SUMOTime_MAX - SUMOTime_MAX % DELTA_T)
37 : #define SUMOTime_DAY 86400000
38 : #define SUMOTime_HOUR 3600000
39 : #define SUMOTime_MINUTE 60000
40 :
41 : // the step length in ms
42 : extern SUMOTime DELTA_T;
43 :
44 : // the step length in seconds as double
45 : #define TS (static_cast<double>(DELTA_T)/1000.)
46 :
47 : // x*deltaT
48 : #define SPEED2DIST(x) ((x)*TS)
49 : // x/deltaT
50 : #define DIST2SPEED(x) ((x)/TS)
51 : // x*deltaT*deltaT
52 : #define ACCEL2DIST(x) ((x)*TS*TS)
53 : // x*deltaT
54 : #define ACCEL2SPEED(x) ((x)*TS)
55 : // x*deltaT
56 : #define SPEED2ACCEL(x) ((x)/TS)
57 :
58 : #define STEPS2TIME(x) (static_cast<double>(x)/1000.)
59 :
60 : inline constexpr SUMOTime TIME2STEPS(double x) {
61 : // static cast to long long int truncates so we must pad away from 0 for correct rounding
62 3108535036 : return static_cast<SUMOTime>(x * 1000. + (x >= 0 ? 0.5 : -0.5));
63 : }
64 :
65 :
66 : #define STEPFLOOR(x) (static_cast<SUMOTime>((x)/DELTA_T)*DELTA_T)
67 : #define STEPS2MS(x) (x)
68 :
69 : #define SIMSTEP MSNet::getInstance()->getCurrentTimeStep()
70 : #define SIMTIME STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
71 :
72 : // ===========================================================================
73 : // method declarations
74 : // ===========================================================================
75 :
76 : /// @brief convert string to SUMOTime
77 : SUMOTime string2time(const std::string& r);
78 :
79 : /// @brief check if the given string is a valid time
80 : bool isTime(const std::string& r);
81 :
82 : /// @brief convert SUMOTime to string (independently of global format setting)
83 : std::string time2string(SUMOTime t, bool humanReadable);
84 :
85 : /// @brief convert SUMOTime to string (using the global format setting)
86 : std::string time2string(SUMOTime t);
87 :
88 : /// @brief convert ms to string for log output
89 : std::string elapsedMs2string(long long int t);
90 :
91 : /// @brief check if given SUMOTime is multiple of the step length
92 : bool checkStepLengthMultiple(const SUMOTime t, const std::string& error = "", SUMOTime deltaT = DELTA_T, SUMOTime begin = 0);
93 :
94 : /// @brief check the valid SUMOTime range of double input and throw an error if out of bounds
95 : void checkTimeBounds(const double time);
|