SecondsToDays

Two functions for formatting seconds (as returned by TIME_PLAYED_MSG or by GetQuestResetTime()) to a human-readable format.
== formatSeconds ==
 
Converts seconds to "s", "m:ss", "h:mm:ss", or "d:hh:mm:ss" format
 local function formatSeconds(seconds)
 local d, h, m, s; 
 -- Allowing fractional values, since string:format("%d") will coerce to integer
 d = seconds / 86400; -- 86400 seconds in a day
 h = (seconds % 86400) / 3600; -- seconds in the left-over day, divided by seconds in an hour
 m = (seconds % 86400 % 3600) / 60; -- seconds in the left-over hour, divided by seconds in a minute
 s = seconds % 86400 % 3600 % 60; -- seconds in the left-over minute
 -- Add or remove output formats you want
 if d >= 1 then return ("%d:%02d:%02d:%02d"):format(d,h,m,s); end;
 if h >= 1 then return ( "%d:%02d:%02d"):format( h,m,s); end;
 -- For better clarity, you could keep the minutes even if they're zero:
 -- return ( "%d:%02d"):format( m,s);
 if m >= 1 then return ( "%d:%02d"):format( m,s); end;
 return s;
 end
===Examples===
Example usage:
 /run print("Dailies rollover in "..formatSeconds(GetQuestResetTime()))
Example runs:
 /run print(formatSeconds(100))
 1:40
 /run print(formatSeconds(100000)) 
 1:03:46:40 
 /run print(formatSeconds(3606))
 1:00:06
Fractions are ignored:
 /run print(formatSeconds(3606.4))
 1:00:06
Negative numbers wind up being interpreted oddly, but understandably:
 /run print(formatSeconds(-3))
 23:59:57
 /run print(formatSeconds(-3606))
 22:59:54
===Notes===
Non-numeric values will generate a runtime error.
Runtime performance is unlikely to matter in code that formats output, but if you're doing this in a tight loop (maybe in logging code), this version will be preferable to the one below, since using the modulo operator (%) and the string:format decimal specifier (%d) is about an order of magnitude faster than using bit.mod() and math.floor(); similarly, string:format() is about the same times faster than repeated string concatenation (a..b..c etc). (Based on measurements against WoW 4.0.3)
You could probably speed this up a little by retaining the values of (seconds % 86400) and ((seconds % 86400) % 3600), at the cost of two more variables to read. I haven't profiled that.
As always, your best bet is to use code that you can read and maintain more easily.
== secondsToDays ==
 
Function for converting the TIME_PLAYED_MSG response (or any source of seconds) to a X days/X hours/X minutes/X seconds format.
 secondsToDays(inputSeconds)
=== Function Parameters ===
==== Arguments ====
:;inputSeconds : Number - the amount of seconds to convert
==== Returns ====
:;result : String - the equivalent combination, using the format: "# days/# hours/# minutes/# seconds"
=== Details ===
: Works with any amount of seconds
=== Code ===
 function secondsToDays(inputSeconds)
 fdays = math.floor(inputSeconds/86400)
 fhours = math.floor((bit.mod(inputSeconds,86400))/3600)
 fminutes = math.floor(bit.mod((bit.mod(inputSeconds,86400)),3600)/60)
 fseconds = math.floor(bit.mod(bit.mod((bit.mod(inputSeconds,86400)),3600),60))
 return fdays.." days/"..fhours.." hours/"..fminutes.." minutes/"..fseconds.." seconds"
 end
Category:User defined functions