Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "Clock.h"
00015 #include <sstream>
00016
00017 using namespace std;
00018
00019 Clock* Clock::instance = 0;
00020
00021
00022 Clock* Clock::getInstance()
00023 {
00024 if ( instance == 0 )
00025 {
00026 instance = new Clock();
00027 }
00028 return instance;
00029 }
00030
00031
00032 Clock::Clock()
00033 {
00034 gettimeofday ( &m_StartTime, NULL );
00035 }
00036
00037
00038 unsigned int Clock::getTimestamp()
00039 {
00040 struct timeval now;
00041 gettimeofday ( &now, NULL );
00042 unsigned int delta_s = now.tv_sec - m_StartTime.tv_sec;
00043 int delta_us = now.tv_usec - m_StartTime.tv_usec;
00044 unsigned int delta_t = ( delta_s * 1000 ) + ( delta_us / 1000 );
00045 return delta_t;
00046 }
00047
00048 std::string Clock::getTimeString( unsigned timestamp )
00049 {
00050 ostringstream outStream;
00051 outStream.setf ( ios::right, ios::adjustfield );
00052 outStream.fill ( '0' );
00053
00054
00055 outStream.width ( 2 );
00056 outStream << timestamp/3600000 << ":";
00057
00058
00059 outStream.width ( 2 );
00060 outStream << timestamp/60000 % 60 << ":";
00061
00062
00063 outStream.width ( 2 );
00064 outStream << timestamp/1000 % 60 << ":";
00065
00066
00067 outStream.width ( 3 );
00068 outStream << timestamp % 1000;
00069
00070 return outStream.str();
00071 }
00072
00073 std::string Clock::getTimeString()
00074 {
00075 return getTimeString( getTimestamp() );
00076 }
00077
00078 std::string Clock::getFilenameDateString()
00079 {
00080 stringstream dateStr;
00081 time_t timestamp = time( 0 );
00082 tm* now = localtime( ×tamp );
00083 int year = now->tm_year + 1900;
00084 int month = now->tm_mon + 1;
00085 int day = now->tm_mday;
00086 int hour = now->tm_hour;
00087 int min = now->tm_min;
00088 int sec = now->tm_sec;
00089
00090 dateStr << year << ".";
00091 dateStr.fill( '0' );
00092 dateStr.width( 2 );
00093 dateStr << month << ".";
00094 dateStr.width( 2 );
00095 dateStr << day << "_";
00096 dateStr.width( 2 );
00097 dateStr << hour << "-";
00098 dateStr.width( 2 );
00099 dateStr << min << "-";
00100 dateStr.width( 2 );
00101 dateStr << sec;
00102 return dateStr.str();
00103 }
00104
00105
00106 Clock::~Clock()
00107 {}
00108
00109 #ifdef __TEST__
00110
00111 #include <iostream>
00112
00113 int main ( int argc, char **argv )
00114 {
00115 std::cout << "UNITTEST" << std::endl;
00116
00117 return 0;
00118 }
00119 #endif