mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Reworked get(Time) function to for easier upkeep
This commit is contained in:
@@ -34,6 +34,38 @@ private:
|
||||
std::chrono::high_resolution_clock::time_point endTime; //The time the stop function was called
|
||||
bool hasStarted; //A flag to show that start() has been called
|
||||
bool hasStopped; //A flag to show that stop() has been called
|
||||
enum TIME_RESOLUTION {HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND, DEFAULT};
|
||||
//Return the duration in the default time period for the high_resolution_clock
|
||||
double getTime(TIME_RESOLUTION timeResolution){
|
||||
double timePassed = 0; //Holds the amount of time that has passed
|
||||
//If the timer hasn't been stopped then record the time right now. This will simulate looping at the stopwatch while it is still running
|
||||
//I put this at the beginning to get the timestamp at close to the calling of the function as possible
|
||||
if(!hasStopped){
|
||||
endTime = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
//If the timer hasn't been started return a -1 as an error
|
||||
if(!hasStarted){
|
||||
return -1;
|
||||
}
|
||||
//Decide what resolution to make the duration
|
||||
switch(timeResolution){
|
||||
case HOUR: std::chrono::duration<double, std::ratio<3600LL>> dur = (endTime - startTime);
|
||||
timePassed = dur.count(); break;
|
||||
case MINUTE: std::chrono::duration<double, std::ratio<60LL>> dur = (endTime - startTime);
|
||||
timePassed = dur.count(); break;
|
||||
case SECOND: std::chrono::duration<double> dur = (endTime - startTime);
|
||||
timePassed = dur.count(); break;
|
||||
case MILLISECOND: std::chrono::duration<double, std::milli> dur = (endTime - startTime);
|
||||
timePassed = dur.count(); break;
|
||||
case MICROSECOND: std::chrono::duration<double, std::micro> dur = (endTime - startTime);
|
||||
timePassed = dur.count(); break;
|
||||
case NANOSECOND: std::chrono::duration<double, std::nano> dur = (endTime - startTime);
|
||||
timePassed = dur.count(); break;
|
||||
case DEFAULT: std::chrono::high_resolution_clock::duration dur = (endTime - startTime);
|
||||
timePassed = dur.count(); break;
|
||||
}
|
||||
return timePassed;
|
||||
}
|
||||
public:
|
||||
Stopwatch(){
|
||||
//Make sure the flags are set to false to show nothing has been called yet
|
||||
@@ -54,66 +86,38 @@ public:
|
||||
void stop(){
|
||||
//Put this first to ensure the time recorded is as close to the call time as possible
|
||||
endTime = std::chrono::high_resolution_clock::now();
|
||||
hasStopped = true; //Show that the stop function has been called
|
||||
}
|
||||
//Return the duration in the default time period for the high_resolution_clock
|
||||
double getTime(){
|
||||
//If the stopwatch has started and stopped return the difference in the 2 times as the duration
|
||||
if(hasStarted && hasStopped){
|
||||
return (endTime - startTime).count();
|
||||
}
|
||||
//Otherwise return -1 as an error message
|
||||
else{
|
||||
return -1;
|
||||
//Make sure the stopwatch has started before you say it has stopped
|
||||
if(hasStarted){
|
||||
hasStopped = true; //Show that the stop function has been called
|
||||
}
|
||||
}
|
||||
//Return the duration in nanoseconds
|
||||
double getNano(){
|
||||
//If the stopwatch has started and stopped return the difference in the 2 times in nanoseconds
|
||||
if(hasStarted && hasStopped){
|
||||
std::chrono::duration<double, std::nano> dur = endTime - startTime;
|
||||
return dur.count();
|
||||
}
|
||||
//Otherwise return -1 as an error message
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
return getTime(NANOSECOND);
|
||||
}
|
||||
//Return the duration in microseconds
|
||||
double getMicro(){
|
||||
//If the stopwatch has started and stopped return the difference in the 2 times in microseconds
|
||||
if(hasStarted && hasStopped){
|
||||
std::chrono::duration<double, std::micro> dur = endTime - startTime;
|
||||
return dur.count();
|
||||
}
|
||||
//Otherwise return -1 as an error message
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
return getTime(MICROSECOND);
|
||||
}
|
||||
//Return the duration in milliseconds
|
||||
double getMilli(){
|
||||
//If the stopwatch has started and stopped return the difference in the 2 times in milliseconds
|
||||
if(hasStarted && hasStopped){
|
||||
std::chrono::duration<double, std::milli> dur = endTime - startTime;
|
||||
return dur.count();
|
||||
}
|
||||
//Otherwise return -1 as an error message
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
return getTime(MILLISECOND);
|
||||
}
|
||||
//Return the duration in seconds
|
||||
double getSeconds(){
|
||||
//If the stopwatch has started and stopped return the difference in the 2 times in seconds
|
||||
if(hasStarted && hasStopped){
|
||||
std::chrono::duration<double> dur = endTime - startTime;
|
||||
return dur.count();
|
||||
}
|
||||
//Otherwise return -1 as an error message
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
return getTime(SECOND);
|
||||
}
|
||||
//Return the duration in minutes
|
||||
double getMinutes(){
|
||||
return getTime(MINUTE);
|
||||
}
|
||||
//Return the duration in hours
|
||||
double getHours(){
|
||||
return getTime(HOUR);
|
||||
}
|
||||
//Return the duration in the default resolution of high_resolution_clock
|
||||
double getTime(){
|
||||
return getTime(DEFAULT);
|
||||
}
|
||||
//This function resets all the variables so that it can be run again
|
||||
void reset(){
|
||||
|
||||
Reference in New Issue
Block a user