Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "Timestamp.hpp"
00009
00010 #include <sys/time.h>
00011 #include <time.h>
00012 #include <unistd.h>
00013
00014 #include <cstdio>
00015
00016 #include <iostream>
00017 using namespace std;
00018
00019 namespace lssr{
00020
00021 Timestamp::Timestamp()
00022 {
00023 resetTimer();
00024 }
00025
00026
00027 unsigned long Timestamp::getCurrentTimeInMs() const
00028 {
00029 static struct timeval tv;
00030 static unsigned long milliseconds;
00031 gettimeofday(&tv, NULL);
00032 milliseconds = tv.tv_sec * 1000 + tv.tv_usec / 1000;
00033 return milliseconds;
00034 }
00035
00036 unsigned long Timestamp::getElapsedTimeInMs() const
00037 {
00038 return getCurrentTimeInMs() - m_startTime;
00039 }
00040
00041 double Timestamp::getCurrentTimeinS() const
00042 {
00043 return (double)getCurrentTimeInMs() / 1000.0;
00044 }
00045
00046 double Timestamp::getElapsedTimeInS() const
00047 {
00048 return (double)getElapsedTimeInMs() / 1000.0;
00049 }
00050
00051 void Timestamp::resetTimer()
00052 {
00053 m_startTime = getCurrentTimeInMs();
00054 }
00055
00056 string Timestamp::getElapsedTime() const
00057 {
00058 unsigned long time = getElapsedTimeInMs();
00059 unsigned long hours = time / 3600000;
00060
00061 unsigned long el_hrs_ms = hours * 3600000;
00062
00063 unsigned long mins;
00064 if(hours != 0)
00065 {
00066 mins = time / 60000;
00067 }
00068 else
00069 {
00070 mins = (time - el_hrs_ms) / 60000;
00071 }
00072
00073 unsigned long secs;
00074 if(mins != 0)
00075 {
00076 secs = time / 1000;
00077 }
00078 else
00079 {
00080 secs = (time - el_hrs_ms - secs * 60000) / 1000;
00081 }
00082
00083 unsigned long msecs = time % 1000;
00084
00085 char times[1024];
00086 sprintf(times, "[%02lu:%02lu:%02lu %04lu] ", hours, mins, secs, msecs);
00087
00088 return string(times);
00089 }
00090
00091 }
00092
00093
00094