rockbandcheeseman

Time

Jun 1st, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. function secondsToTime(seconds, places)
  2.  
  3.     local years = math.floor(seconds / (60 * 60 * 24 * 365))
  4.     seconds = seconds % (60 * 60 * 24 * 365)
  5.     local weeks = math.floor(seconds / (60 * 60 * 24 * 7))
  6.     seconds = seconds % (60 * 60 * 24 * 7)
  7.     local days = math.floor(seconds / (60 * 60 * 24))
  8.     seconds = seconds % (60 * 60 * 24)
  9.     local hours = math.floor(seconds / (60 * 60))
  10.     seconds = seconds % (60 * 60)
  11.     local minutes = math.floor(seconds / 60)
  12.     seconds = seconds % 60
  13.    
  14.     if places == 6 then
  15.         return string.format("%02d:%02d:%02d:%02d:%02d:%02d", years, weeks, days, hours, minutes, seconds)
  16.     elseif places == 5 then
  17.         return string.format("%02d:%02d:%02d:%02d:%02d", weeks, days, hours, minutes, seconds)
  18.     elseif not places or places == 4 then
  19.         return string.format("%02d:%02d:%02d:%02d", days, hours, minutes, seconds)
  20.     elseif places == 3 then
  21.         return string.format("%02d:%02d:%02d", hours, minutes, seconds)
  22.     elseif places == 2 then
  23.         return string.format("%02d:%02d", minutes, seconds)
  24.     elseif places == 1 then
  25.         return string.format("%02", seconds)
  26.     end
  27. end
  28.  
  29. function toSeconds(time)
  30.  
  31.     local seconds = 0
  32.     local split = string.split(time, ":")
  33.     if #split == 1 then
  34.         seconds = tonumber(split[1])
  35.     elseif #split == 2 then
  36.         seconds = tonumber(split[1]) * 60
  37.         seconds = seconds + tonumber(split[2])
  38.     elseif #split == 3 then
  39.         seconds = tonumber(split[1]) * 60 * 60
  40.         seconds = seconds + tonumber(split[2]) * 60
  41.         seconds = seconds + tonumber(split[3])
  42.     elseif #split == 4 then
  43.         seconds = tonumber(split[1]) * 60 * 60 * 24
  44.         seconds = seconds + tonumber(split[2]) * 60 * 60
  45.         seconds = seconds + tonumber(split[3]) * 60
  46.         seconds = seconds + tonumber(split[4])
  47.     elseif #split == 5 then
  48.         seconds = tonumber(split[1]) * 60 * 60 * 24 * 7
  49.         seconds = seconds + tonumber(split[2]) * 60 * 60 * 24
  50.         seconds = seconds + tonumber(split[3]) * 60 * 60
  51.         seconds = seconds + tonumber(split[4]) * 60
  52.         seconds = seconds + tonumber(split[5])
  53.     elseif #split == 6 then
  54.         seconds = tonumber(split[1]) * 60 * 60 * 24 * 365
  55.         seconds = seconds + tonumber(split[2]) * 60 * 60 * 24 * 7
  56.         seconds = seconds + tonumber(split[3]) * 60 * 60 * 24
  57.         seconds = seconds + tonumber(split[4]) * 60 * 60
  58.         seconds = seconds + tonumber(split[5]) * 60
  59.         seconds = seconds + tonumber(split[6])
  60.     end
  61.    
  62.     return seconds
  63. end
Advertisement
Add Comment
Please, Sign In to add comment