Module:Sandbox/DixonD/DateTemplates

local DateTime = require("Module:Sandbox/DixonD/Datetime"); local Math = require("Module:Math"); local Error = require('Module:Error')

local z = {}

-- utility functions local function getArgs(frame) local args = frame.args; -- if there are any arguments, then assume that we've got proper ones for k,v in pairs(args) do        return args end -- otherwise, return args from the parent local parent = frame:getParent; return parent.args; end

local function isError(response) return type(response) == 'string' and string.find(response, 'error'); end

--template implementations

-- Template:JULIANDAY function z.julianDay(frame) local args = getArgs(frame); return DateTime.julianDay(Math._cleanNumber(args[1]),       Math._cleanNumber(args[2]), Math._cleanNumber(args[3]),        Math._cleanNumber(args[4]), Math._cleanNumber(args[5]),        Math._cleanNumber(args[6]), 'g'); end

-- Template:JULIANDAY.YEAR function z.yearOfJulianDay(frame) local args = getArgs(frame); return DateTime.yearOfJulianDay(Math._cleanNumber(args[1]), 'g'); end

-- Template:JULIANDAY.MONTH function z.monthOfJulianDay(frame) local args = getArgs(frame); return DateTime.monthOfJulianDay(Math._cleanNumber(args[1]), 'g'); end

-- Template:JULIANDAY.DAY function z.dayOfJulianDay(frame) local args = getArgs(frame); return DateTime.dayOfJulianDay(Math._cleanNumber(args[1]), 'g'); end

-- Template:JULIANDAY.HOUR function z.hourOfJulianDay(frame) local args = getArgs(frame); return DateTime.hourOfJulianDay(Math._cleanNumber(args[1]), 'g'); end

-- Template:JULIANDAY.MINUTE function z.minuteOfJulianDay(frame) local args = getArgs(frame); return DateTime.minuteOfJulianDay(Math._cleanNumber(args[1]), 'g'); end

-- Template:JULIANDAY.SECOND function z.secondOfJulianDay(frame) local args = getArgs(frame); return DateTime.secondOfJulianDay(Math._cleanNumber(args[1]), 'g'); end

-- Template:JULIANDAY.JULIAN function z.julianDayJulian(frame) local args = getArgs(frame); return DateTime.julianDay(Math._cleanNumber(args[1]),       Math._cleanNumber(args[2]), Math._cleanNumber(args[3]),        Math._cleanNumber(args[4]), Math._cleanNumber(args[5]),        Math._cleanNumber(args[6]), 'j'); end

-- Template:Gregorian serial date function z.gregorianSerialDate(frame) local args = getArgs(frame); local currentDate = os.date('!*t'); local year = args.year == nil and currentDate.year or Math._cleanNumber(args.year); if year == nil then return Error.error{"Parameter year has wrong format!"}; end; local month = args.month == nil and currentDate.month or Math._cleanNumber(args.month); if month == nil then return Error.error{"Parameter month has wrong format!"}; end; local day = args.day == nil and currentDate.day or Math._cleanNumber(args.day); if day == nil then return Error.error{"Parameter day has wrong format!"}; end; local julianDay = DateTime.julianDay(year, month, day, nil, nil, nil, 'g'); if isError(julianDay) then return julianDay; end; return julianDay - 1721425; end

return z