Advertisement
Robear9992

timer

Jul 4th, 2022 (edited)
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.78 KB | None | 0 0
  1. --cc:Tweaked: Timer
  2. --pastebin get mJKJ9hYq timer.lua
  3.  
  4.  
  5.  
  6. local timer = {}
  7. local started = nil
  8. local stopped = nil
  9.  
  10. function timer.start()
  11.     stopped = nil
  12.     started = os.clock()
  13. end
  14.  
  15. function timer.stop()
  16.     stopped = os.clock()
  17. end
  18.  
  19. function timer.elapsed()
  20.     if stopped == nil then
  21.         return os.clock() - started
  22.     else
  23.         return stopped - started
  24.     end
  25. end
  26.  
  27. function timer.format(seconds)
  28.     if seconds == nil then seconds = timer.elapsed() end
  29.    
  30.     local hours = math.floor(seconds / 3600)
  31.     seconds = seconds % 3600
  32.     local minutes = math.floor(seconds / 60)
  33.     seconds = seconds % 60
  34.    
  35.     local stime = math.floor(seconds) .. "s"
  36.     if(minutes > 0) then stime = minutes .. "m " .. stime end
  37.     if(hours > 0) then stime = hours .. "h " .. stime end
  38.     return stime
  39. end
  40.  
  41. return timer
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement