Anonymous

Module:Shared: Difference between revisions

From Melvor Idle
Added shortened timestring version
(Added timeString function)
(Added shortened timestring version)
Line 297: Line 297:
end
end


function p.timeString(timeInSeconds)
function p.timeString(timeInSeconds, shorten)
   local remain = timeInSeconds
   local remain = timeInSeconds
   local days, hours, minutes = 0, 0, 0
   local days, hours, minutes = 0, 0, 0
  local isShort = shorten
    
    
   local pieces = {}
   local pieces = {}
Line 306: Line 307:
     days = math.floor(remain / 86400)
     days = math.floor(remain / 86400)
     remain = remain - days * 86400
     remain = remain - days * 86400
     if days > 1 then
     if isShort then
      table.insert(pieces, days..'d')
    elseif days > 1 then
       table.insert(pieces, days..' days')
       table.insert(pieces, days..' days')
     else
     else
Line 315: Line 318:
     hours = math.floor(remain / 3600)
     hours = math.floor(remain / 3600)
     remain = remain - hours * 3600
     remain = remain - hours * 3600
     if hours > 1 then
     if isShort then
      table.insert(pieces, hours..'h')
    elseif hours > 1 then
       table.insert(pieces, hours..' hours')
       table.insert(pieces, hours..' hours')
     else
     else
Line 324: Line 329:
     minutes = math.floor(remain / 60)
     minutes = math.floor(remain / 60)
     remain = remain - minutes * 60
     remain = remain - minutes * 60
     if minutes > 1 then
     if isShort then
      table.insert(pieces, minutes..'m')
    elseif minutes > 1 then
       table.insert(pieces, minutes..' minutes')
       table.insert(pieces, minutes..' minutes')
     else
     else
Line 331: Line 338:
   end
   end
   if remain > 0 then
   if remain > 0 then
     if remain > 1 then
     if isShort then
      table.insert(pieces, remain..'s')
    elseif remain > 1 then
       table.insert(pieces, remain..' seconds')
       table.insert(pieces, remain..' seconds')
     else
     else