mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Moved all string functions to a single function
A couple of bugs relating to time resolution squished
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
//MyClasses/Stopwatch.hpp
|
//MyClasses/Stopwatch.hpp
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 10-30-2018
|
// Created: 10-30-18
|
||||||
//Modified: 02-07-2019
|
//Modified: 07-09-20
|
||||||
//This file defines a class that can be used as a simple timer for programs
|
//This file defines a class that can be used as a simple timer for programs
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2019 Matthew Ellison
|
Copyright (C) 2020 Matthew Ellison
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Lesser General Public License as published by
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -147,44 +147,7 @@ public:
|
|||||||
//Returns a string with the time at best resolution
|
//Returns a string with the time at best resolution
|
||||||
std::string getStr(){
|
std::string getStr(){
|
||||||
//Setup the variables
|
//Setup the variables
|
||||||
double tempTime = getTime(NANOSECOND); //Holds the time that we are manipulating
|
return getStr(getTime(NANOSECOND)); //Holds the time that we are manipulating
|
||||||
std::stringstream timeStr;
|
|
||||||
|
|
||||||
//Decide what time resolution would be best. Looking for the format of XXX.XXX
|
|
||||||
int timeRes = NANOSECOND;
|
|
||||||
for(timeRes = MICROSECOND;(timeRes < DEFAULT) && (tempTime >= 1000);++timeRes){
|
|
||||||
tempTime = getTime(static_cast<TIME_RESOLUTION>(timeRes));
|
|
||||||
}
|
|
||||||
--timeRes; //Take this variable back down to the right place. It has to go one too far to trigger the loop stop
|
|
||||||
|
|
||||||
//Check if the resolution is seconds and if there are more than 120 seconds
|
|
||||||
if((timeRes == SECOND) && (tempTime >= 120)){
|
|
||||||
++timeRes;
|
|
||||||
tempTime = getTime(static_cast<TIME_RESOLUTION>(timeRes));
|
|
||||||
}
|
|
||||||
//Check if the resolution is minutes and if there are more than 120 minutes
|
|
||||||
else if((timeRes == MINUTE) && (tempTime >= 120)){
|
|
||||||
++timeRes;
|
|
||||||
tempTime = getTime(static_cast<TIME_RESOLUTION>(timeRes));
|
|
||||||
}
|
|
||||||
|
|
||||||
//Put the number in the string
|
|
||||||
timeStr << std::fixed << std::setprecision(3) << tempTime << ' ';
|
|
||||||
|
|
||||||
//From the timeRes variable decide what word should go on the end of the string
|
|
||||||
switch(timeRes){
|
|
||||||
case HOUR: timeStr << "hours"; break;
|
|
||||||
case MINUTE: timeStr << "minutes"; break;
|
|
||||||
case SECOND: timeStr << "seconds"; break;
|
|
||||||
case MILLISECOND: timeStr << "milliseconds"; break;
|
|
||||||
case MICROSECOND: timeStr << "microseconds"; break;
|
|
||||||
case NANOSECOND: timeStr << "nanoseconds"; break;
|
|
||||||
case DEFAULT: timeStr << "time"; break;
|
|
||||||
default: throw invalidTimeResolution(); //This should never be hit with this code, but it's good to have all the bases covered
|
|
||||||
}
|
|
||||||
|
|
||||||
//Return the string
|
|
||||||
return timeStr.str();
|
|
||||||
}
|
}
|
||||||
//This function resets all the variables so that it can be run again
|
//This function resets all the variables so that it can be run again
|
||||||
void reset(){
|
void reset(){
|
||||||
@@ -200,10 +163,9 @@ public:
|
|||||||
|
|
||||||
//Decide what time resolution would be best. Looking for the format of XXX.XXX
|
//Decide what time resolution would be best. Looking for the format of XXX.XXX
|
||||||
int timeRes = NANOSECOND;
|
int timeRes = NANOSECOND;
|
||||||
for(timeRes = MICROSECOND;(timeRes < DEFAULT) && (tempTime >= 1000);++timeRes){
|
for(timeRes = NANOSECOND;(timeRes < SECOND) && (tempTime >= 1000);++timeRes){
|
||||||
tempTime /= 1000;
|
tempTime /= 1000;
|
||||||
}
|
}
|
||||||
--timeRes; //Take this variable back down to the right place. It has to go one too far to trigger the loop stop
|
|
||||||
|
|
||||||
//Check if the resolution is seconds and if there are more than 120 seconds
|
//Check if the resolution is seconds and if there are more than 120 seconds
|
||||||
if((timeRes == SECOND) && (tempTime >= 120)){
|
if((timeRes == SECOND) && (tempTime >= 120)){
|
||||||
@@ -211,7 +173,7 @@ public:
|
|||||||
tempTime /= 60;
|
tempTime /= 60;
|
||||||
}
|
}
|
||||||
//Check if the resolution is minutes and if there are more than 120 minutes
|
//Check if the resolution is minutes and if there are more than 120 minutes
|
||||||
else if((timeRes == MINUTE) && (tempTime >= 120)){
|
if((timeRes == MINUTE) && (tempTime >= 120)){
|
||||||
++timeRes;
|
++timeRes;
|
||||||
tempTime /= 60;
|
tempTime /= 60;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user