Module:TimeUtil

local lang = mw.getLanguage('en')

local p = {}

function p.strToTime(str) local year, month, day, hour, minute, second = string.match(str, "(%d%d%d%d)-(%d%d)-(%d%d) (%d%d):(%d%d):(%d%d)") return { year = year, month = month, day = day, hour = hour, min = minute, sec = second } end

function p.strToDate(str) local year, month, day = string.match(str, "(%d%d%d%d)-(%d%d)-(%d%d)") return { year = year, month = month, day = day } end

function p.isInFuture(str) return os.difftime(os.time(p.strToTime(str)),os.time) > 0 end

function p.dateTimeInLocal(time, datetime) if not datetime then datetime = 'Date' end local tbl = mw.html.create('span'):addClass(datetime .. 'InLocal'):wikitext(lang:formatDate('Y,n,d,H,i', time)) return tostring(tbl) end

-- input is a string function p.countdown(time, args) if not args then args = {} end local i = args.i or '' local data_end = args.data_end or 'remove' local tbl = mw.html.create tbl:tag('span') :attr('data-end', data_end) :attr('data-options','no-leading-zeros') :attr('data-toggle','.post-countdown' .. i)		:addClass('countdown') :css('display','none') :tag('span') :addClass('countdowndate') :wikitext(lang:formatDate('j F Y H:i:s', time) .. ' +0000') tbl:tag('span') :addClass('post-countdown' .. i)		:css('display','none') :wikitext(args.default or '-') return tostring(tbl) end

-- input is a string function p.unix(str) return lang:formatDate('U', str) end

function p.age(bday) if type(bday) == 'string' then bday = p.strToDate(bday) end for k, v in pairs(bday) do		bday[k] = tonumber(v) end local now = os.date('*t') local bdaypast = now.month > bday.month or now.month == bday.month and now.day == bday.day return now.year - bday.year - 1 + (bdaypast and 1 or 0) end return p