local w={}w.ToTime=function(w)local z=math.floor(w/10080)local c=math.floor((w-z*10080)/1440)local o=math.floor(((w-c*1440)-z*10080)/60)local l=math.floor(((w-o*60)-c*1440)-z*10080)local a=""if z>0 then a=a..(z.."\119 ")end if c>0 then a=a..(c.."\100 ")end if o>0 then a=a..(o.."\104 ")end if l>0 then a=a..(l.."\109")end return a end w.FromTime=function(w)local z=0 local c=0 local o=0 local l=0 for w in string.gmatch(w,"\040\037\100\043\041\119")do z=tonumber(w)end for w in string.gmatch(w,"\040\037\100\043\041\100")do c=tonumber(w)end for w in string.gmatch(w,"\040\037\100\043\041\104")do o=tonumber(w)end for w in string.gmatch(w,"\040\037\100\043\041\109")do l=tonumber(w)end return((z*10080+c*1440)+o*60)+l end setmetatable(w,{__call=function(w,z)return w.ToTime(z)end})return w
local mod = {}

mod.ToTime = function(minutes)
	local weeks = math.floor(minutes / 10080)
	local days = math.floor((minutes - (weeks * 10080)) / 1440)
	local hours = math.floor((minutes - (days * 1440) - (weeks * 10080)) / 60)
	local mins = math.floor(minutes - (hours * 60) - (days * 1440) - (weeks * 10080))

	local str = ""
	if weeks > 0 then
		str = str .. weeks .. "w "
	end
	if days > 0 then
		str = str .. days .. "d "
	end
	if hours > 0 then
		str = str .. hours .. "h "
	end
	if mins > 0 then
		str = str .. mins .. "m"
	end

	return str
end

mod.FromTime = function(str)
	local weeks = 0
	local days = 0
	local hours = 0
	local mins = 0

	for w in string.gmatch(str, "(%d+)w") do
		weeks = tonumber(w)
	end
	for d in string.gmatch(str, "(%d+)d") do
		days = tonumber(d)
	end
	for h in string.gmatch(str, "(%d+)h") do
		hours = tonumber(h)
	end
	for m in string.gmatch(str, "(%d+)m") do
		mins = tonumber(m)
	end

	return (weeks * 10080) + (days * 1440) + (hours * 60) + mins
end

setmetatable(mod, {
	-- For the old API, which didn't use .ToTime but just called the module as env.FormatTime(123)
	-- We need to keep this for backwards compatibility since some modules probably still use it
	__call = function(self, minutes)
		return self.ToTime(minutes)
	end
})

return mod