00001 //---------------------------------------------------------------------------- 00002 /** @file Time.cpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include <sys/time.h> 00007 #include <cstdio> 00008 #include <iomanip> 00009 #include <sstream> 00010 #include "Time.hpp" 00011 00012 using namespace benzene; 00013 00014 //---------------------------------------------------------------------------- 00015 00016 double Time::Get() 00017 { 00018 struct timeval tt; 00019 gettimeofday(&tt, NULL); 00020 return tt.tv_sec + 1e-6*tt.tv_usec; 00021 } 00022 00023 std::string Time::Formatted(double elapsed) 00024 { 00025 std::ostringstream os; 00026 00027 int days = static_cast<int>(elapsed / ONE_DAY); 00028 elapsed -= days*ONE_DAY; 00029 int hours = static_cast<int>(elapsed / ONE_HOUR); 00030 elapsed -= hours*ONE_HOUR; 00031 int mins = static_cast<int>(elapsed / ONE_MINUTE); 00032 elapsed -= mins*ONE_MINUTE; 00033 00034 if (days) os << days << "d"; 00035 if (hours) os << hours << "h"; 00036 if (mins) os << mins << "m"; 00037 os << std::setprecision(4) << elapsed << "s"; 00038 00039 return os.str(); 00040 } 00041 00042 //----------------------------------------------------------------------------