local W,k=math.floor,math.ceil local function w(W)return W%4==0 and(W%25~=0 or W%16==0)end local function m(k)return(W(k/4)-W(k/100))+W(k/400)end local function z(W)return 365*W+m(W)end local function B(W)return 365*W+m(W-1)end local function O(W,k)for w=1,#W,1 do if k-W[w]<=0 then return w,k end k=k-W[w]end end local function S(m)local S=m or tick()local o=k((S+1)/86400)+z(1970)local f=400*W(o/z(400))local L=100*W((o%z(400))/z(100))local N=4*W(((o%z(400))%z(100))/z(4))local x,a x,o=O({366;365,365,365},o-B((N+L)+f))x=x-1 local E=((x+N)+L)+f a,o=O({31;w(E)and 29 or 28;31,30;31,30,31;31,30;31;30;31},o)return E,a,o end local function o(k)local w=W((k/3600)%24)local m=W((k/60)%60)local z=W(k%60)return w,m,z end return{Date=S;Time=o}
--[[
	-- Time functions
	-- @author Narrev
	
	This module can be called like so:
	
	timeModule = require(331894616) 	
	
	timeModule.Date() 			--> returns (year, month, days) @ timestamp tick()
	timeModule.Date(seconds) 	--> returns (year, month, days) @ seconds after epoch time
	
	timeModule.Time()			--> returns (hours, minutes, seconds) @ timestamp tick()
	timeModule.Time(seconds)	--> returns (hours, minutes, seconds) @ seconds after epoch time
	
--]]

local floor, ceil = math.floor, math.ceil

local function isLeapYear(year)
	return year % 4 == 0 and (year % 25 ~= 0 or year % 16 == 0)
end

local function GetLeaps(year)
	return floor(year / 4) - floor(year / 100) + floor(year / 400)
end

local function CountDays(year)
	return 365 * year + GetLeaps(year)
end

local function CountDays2(year)
	return 365 * year + GetLeaps(year - 1)
end

local function overflow(array, seed)
	for i = 1, #array do
		if seed - array[i] <= 0 then
			return i, seed
		end
		seed = seed - array[i]
	end
end

local function GetYMDFromSeconds(seconds)
	local seconds = seconds or tick()
	local days = ceil((seconds + 1) / 86400) + CountDays(1970)

	local _400Years = 400 * floor(days / CountDays(400))
	local _100Years = 100 * floor(days % CountDays(400) / CountDays(100))
	local _4Years = 4 * floor(days % CountDays(400) % CountDays(100) / CountDays(4))

	local _1Years, month
	_1Years, days = overflow({ 366, 365, 365, 365 }, days - CountDays2(_4Years + _100Years + _400Years))
	_1Years = _1Years - 1

	local year = _1Years + _4Years + _100Years + _400Years
	month, days = overflow({ 31, isLeapYear(year) and 29 or 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, days)

	return year, month, days
end

local function GetTimeFromSeconds(seconds)
	local hours = floor(seconds / 3600 % 24)
	local minutes = floor(seconds / 60 % 60)
	local seconds = floor(seconds % 60)

	return hours, minutes, seconds
end

return { Date = GetYMDFromSeconds, Time = GetTimeFromSeconds }
