SUMOTime.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Variables, methods, and tools for internal time representation
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 // ===========================================================================
00020 // included modules
00021 // ===========================================================================
00022 #ifdef _MSC_VER
00023 #include <windows_config.h>
00024 #else
00025 #include <config.h>
00026 #endif
00027 
00028 #include <sstream>
00029 #include "SUMOTime.h"
00030 #include "TplConvert.h"
00031 
00032 
00033 // ===========================================================================
00034 // type definitions
00035 // ===========================================================================
00036 #ifdef HAVE_SUBSECOND_TIMESTEPS
00037 SUMOTime DELTA_T = 1000;
00038 #endif
00039 
00040 
00041 // ===========================================================================
00042 // method definitions
00043 // ===========================================================================
00044 SUMOTime
00045 string2time(const std::string &r) throw(EmptyData, NumberFormatException) {
00046     size_t idx = r.find('.');
00047     if (idx==std::string::npos) {
00048         // no sub-seconds
00049         return TplConvert<char>::_2int(r.c_str())*1000;
00050     }
00051     int secs = idx>0 ? TplConvert<char>::_2int(r.substr(0, idx).c_str()) : 0;
00052     int subsecs = TplConvert<char>::_2int((r.substr(idx+1)+"0000").substr(0,3).c_str()) * 10;
00053     do {
00054         subsecs = subsecs / 10;
00055     } while (idx<r.length()&&r[idx]=='0');
00056     return secs*1000 + subsecs;
00057 }
00058 
00059 
00060 std::string
00061 time2string(SUMOTime t) throw() {
00062     std::ostringstream oss;
00063     oss << ((SUMOReal) t / 1000.);
00064     std::string ret = oss.str();
00065     size_t idx = ret.find('.');
00066     if (idx==std::string::npos) {
00067         return ret + ".00";
00068     }
00069     return (ret+"00").substr(0, idx+3);
00070 }
00071 
00072 
00073 /****************************************************************************/
00074 

Generated on Wed May 5 00:06:36 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6