Module:DurationConverter

local HOURS_PER_DAY = 24 local MINUTES_PER_HOUR = 60 local SECONDS_PER_MINUTE = 60

local SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE local SECONDS_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE

-- Converts seconds into the coin cost in-game local function secondsToCoins( seconds ) if seconds <= 300 then return 0 elseif seconds <= 570 then return 1 else return 1 + math.ceil( (seconds - 570) / 180 ) end end

-- Converts the format [days]d [hours]h [minutes]m [seconds]s, similar to what is displayed in-game, into seconds. local function durationToSeconds( duration ) duration = tostring(duration)

return tonumber(duration:match("(%d+)d") or 0) * SECONDS_PER_DAY + tonumber(duration:match("(%d+)h") or 0) * SECONDS_PER_HOUR + tonumber(duration:match("(%d+)m") or 0) * SECONDS_PER_MINUTE + tonumber(duration:match("(%d+)s") or 0) end

-- Converts the format [days]d [hours]h [minutes]m [seconds]s, similar to what is displayed in-game, into the coin cost in-game local function durationToCoins( duration ) return secondsToCoins( durationToSeconds(duration) ) end

-- Formats a timespan as it appears in-game. local function formatInGameTime( num_seconds ) -- Convert into a number if it is a string num_seconds = tonumber(num_seconds) if (num_seconds >= SECONDS_PER_DAY) then return string.format("%s %s %s", os.date("%dd", num_seconds):gsub("0*(%d+)", "%1"), os.date("%Hh", num_seconds):gsub("0*(%d+)", "%1"), os.date("%Mm %Ss", num_seconds)) elseif (num_seconds >= SECONDS_PER_HOUR) then return string.format("%s %s", os.date("%Hh", num_seconds):gsub("0*(%d+)", "%1"), os.date("%Mm %Ss", num_seconds)) elseif (num_seconds >= SECONDS_PER_MINUTE) then return string.format("%s %s", os.date("%Mm", num_seconds):gsub("0*(%d+)", "%1"), os.date("%Ss", num_seconds)) else return (os.date("%Ss", num_seconds):gsub("0*(%d+)", "%1")) end end

-- Module call from Template:DurationConverter local function durationToSeconds_parserFunction( frame ) local args = require("Module:Args").getCleanArgs return durationToSeconds(args[1]) end

return { secondsToDuration = secondsToDuration, secondsToCoins = secondsToCoins, durationToSeconds = durationToSeconds, durationToCoins = durationToCoins, formatInGameTime = formatInGameTime, durationToSeconds_parserFunction = durationToSeconds_parserFunction, }