diff --git a/Stopwatch.lua b/Stopwatch.lua index 01a226a..0a5339c 100644 --- a/Stopwatch.lua +++ b/Stopwatch.lua @@ -6,7 +6,15 @@ --You need some kind of table to hold the times -Stopwatch = {} +Stopwatch = { + timeResolution = { + microSeconds = 1; + milliSeconds = 2; + seconds = 3; + minutes = 4; + hours = 5; + }; +}; Stopwatch.__index = Stopwatch; --This is needed to create a Stopwatch and link the variables and functions @@ -88,6 +96,26 @@ function Stopwatch:getSeconds() return self:getTime(); end +--This function returns the duration the stopwatch has run in minutes +function Stopwatch:getMinutes() + local timeDifference = self:getTime(); --Get the time that has elapsed + --If you did not get an error convert the time to minutes + if(timeDifference >= 0) then + timerDifference = timeDifference / 60; + end + return timeDifference; +end + +--This function returns the duration the stopwatch has run in hours +function Stopwatch:getHours() + local timeDifference = self:getTime(); --Get the time that has elapsed + --If you did not get an error convert the time to minutes + if(timeDifference >= 0) then + timerDifference = timeDifference / 3600; + end + return timeDifference; +end + --This function returns the duration the stopwatch has run in minutes function Stopwatch:getMinutes() local timeDifference = self:getTime(); @@ -97,3 +125,51 @@ function Stopwatch:getMinutes() end return timeDifference; end + +function Stopwatch:getString() + local timeDifference = self:getTime(); --Get the time that has elapsed + local timeResolution = Stopwatch.timeResolution.seconds; + local timeString = ""; + + --Convert the time to a string with the format XXX.XXX + --The time is returned in seconds. Decide if that as ok or if the resolution needs to go up or down + --If there are more than 120 seconds change the time to minutes + if(timeDifference > 120) then + timeDifference = timeDifference / 60; + timeResolution = Stopwatch.timeResolution.minutes; + --If there are more than 120 minutes change the time to hours + if(timeDifference > 120) then + timeDifference = timeDifference / 60; + timeResolution = Stopwatch.timeResolution.hours; + end + + --If there is less than 1 second change the time to milliseconds + elseif(timeDifference < 1) then + timeDifference = timeDifference * 1000; + timeResolution = Stopwatch.timeResolution.milliSeconds; + --If there are is less than 1 millisecond change the time to microseconds + if(timeDifference < 1) then + timeDifference = timeDifference * 1000; + timeResolution = Stopwatch.timeResolution.microSeconds; + end + end + --If the number of seconds is between 1 and 120 just use seconds + + --Create the string + if(timeResolution == Stopwatch.timeResolution.microSeconds) then + timeString = string.format("%.0f microseconds", timeDifference); + elseif(timeResolution == Stopwatch.timeResolution.milliSeconds) then + timeString = string.format("%.3f milliseconds", timeDifference); + elseif(timeResolution == Stopwatch.timeResolution.seconds) then + timeString = string.format("%.3f seconds", timeDifference); + elseif(timeResolution == Stopwatch.timeResolution.minutes) then + timeString = string.format("%.3f minutes", timeDifference); + elseif(timeResoltuion == Stopwatch.timeResolution.hours) then + timeString = string.format("%.3f hours", timeDifference); + else + timeString = "ERROR!"; + end + + --Return the string + return timeString; +end \ No newline at end of file