Editing Module:D'ni Time
From Guild of Archivists
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
-- THIS IS OBVIOUSLY NOT DONE! DO NOT TOUCH! -Vee | -- THIS IS OBVIOUSLY NOT DONE! DO NOT TOUCH! -Vee | ||
local p = {} | local p = {} | ||
Line 197: | Line 191: | ||
return os.date("*t",surfaceTimestamp) | return os.date("*t",surfaceTimestamp) | ||
end | end | ||
-- ==Parse Functions== | |||
local function parseSurfaceDateStr(dateStr) | |||
-- Given a surface data string, accepts formats that #time accepts | |||
-- Returns a surfaceDate (table) | |||
lang = mw.getContentLanguage() | |||
surfaceTimeStamp = lang:formatDate("U",dateStr) | |||
surfaceTime = os.date("!*t",surfaceTimeStamp) | |||
surfaceTime['isdst'] = nil | |||
return surfaceTime | |||
end | |||
-- ==Display Functions== | |||
local function getVaileeName(vailee, useDniFont) | |||
-- Return name of vailee given number (zero-indexed) | |||
-- If useDniFont is true, return it in Dnifont text | |||
useDniFont = useDniFont or false | |||
vaileetee = {"Leefo","Leebro","Leesahn","Leetar","Leevot","Leevofo","Leevobro","Leevosahn","Leevotar","Leenovoo"} | |||
vaileetee_dnifont = {"lEfo","lEbro","lEsan","lEtar","lEvot","lEvofo","lEvobro","lEvosan","lEvotar","lEnovU"} | |||
if(useDniFont) then | |||
return vaileetee_dnifont[vailee+1] | |||
else | |||
return vaileetee[vailee+1] | |||
end | |||
end | |||
local function displayDniTime(dniTime) | |||
-- Return dniTime (table) as string of table | |||
return '{'..'hahr = '..dniTime['hahr']..', vailee = '..dniTime['vailee']..', vaileeName = '..getVaileeName(dniTime['vailee'])..', yahr = '..dniTime['yahr']..', gahrtahvo = '..dniTime['gahrtahvo']..', tahvo = '..dniTime['tahvo']..', gorahn = '..dniTime['gorahn']..', prorahn = '..dniTime['prorahn']..'}' | |||
end | |||
local function displaySurfaceTime(surface) | |||
-- Return surfaceTime (table) as string of table | |||
return '{'..'year = '..surface['year']..', month = '..surface['month']..', day = '..surface['day']..', hour = '..surface['hour']..', min = '..surface['min']..', sec = '..surface['sec']..', isdst = '..tostring(surface['isdst'])..'}' | |||
end | |||
local function formatSurfaceTime(surfaceTime,format) | |||
format = format or 'c' | |||
lang = mw.getContentLanguage() | |||
return lang:formatDate(format,tostring(surfaceTime['year'])..'/'..tostring(surfaceTime['month'])..'/'..tostring(surfaceTime['day'])..' '..tostring(surfaceTime['hour'])..':'..tostring(surfaceTime['min'])..':'..tostring(surfaceTime['sec'])) | |||
end | |||
local function formatDniTime(dniTime,format) | |||
-- Given a DniTime (table), returns formatted string. | |||
-- format is a string with patterns for parts of the date, like standard dates | |||
-- Patterns in capitals padded with zeroes | |||
-- HHHH, hhhh = hahr (4-digits, Uppercase includes +/-) | |||
-- hh = last two digits of the hahr | |||
-- XX = 'era' (BE or AE) | |||
-- VVVV = vailee by name | |||
-- VV,vv = vailee (1 indexed) | |||
-- YY, yy = yahr | |||
-- GG, gg = gahrtahvo | |||
-- TT, tt = tahvo | |||
-- RR, rr = gorahnn | |||
-- PP, pp = prorahn | |||
format = format or "VVVV yy HHHH gg:tt:rr:pp" | |||
result = format | |||
if (dniTime['hahr'] < 0) then era = "BE" else era = "AE" end | |||
result = string.gsub(result,"HHHH", string.format("%04d",tostring(dniTime['hahr']))) | |||
result = string.gsub(result,"hhhh", math.abs(dniTime['hahr'])) | |||
result = string.gsub(result,"hh", string.sub(dniTime['hahr'],-2)) | |||
result = string.gsub(result,"YY", string.format("%02d",tostring(dniTime['yahr']))) | |||
result = string.gsub(result,"yy", dniTime['yahr']) | |||
result = string.gsub(result,"VVVV", getVaileeName(dniTime['vailee'])) | |||
result = string.gsub(result,"VV", string.format("%02d",tostring(dniTime['vailee']+1))) | |||
result = string.gsub(result,"vv", dniTime['vailee'] + 1) | |||
result = string.gsub(result,"GG", string.format("%02d",tostring(dniTime['gahrtahvo']))) | |||
result = string.gsub(result,"gg", dniTime['gahrtahvo']) | |||
result = string.gsub(result,"TT", string.format("%02d",tostring(dniTime['tahvo']))) | |||
result = string.gsub(result,"tt", dniTime['tahvo']) | |||
result = string.gsub(result,"RR", string.format("%02d",tostring(dniTime['gorahn']))) | |||
result = string.gsub(result,"rr", dniTime['gorahn']) | |||
result = string.gsub(result,"PP", string.format("%02d",tostring(dniTime['prorahn']))) | |||
result = string.gsub(result,"pp", dniTime['prorahn']) | |||
result = string.gsub(result,"XX", era) | |||
return result | |||
end | |||
-- ============================================================================= | -- ============================================================================= | ||
Line 205: | Line 297: | ||
if origArgs[argName] and origArgs[argName] ~= '' then | if origArgs[argName] and origArgs[argName] ~= '' then | ||
args[argName] = origArgs[argName] | args[argName] = origArgs[argName] | ||
end | |||
end | |||
local function preprocessArgs(prefixTable, step) | |||
-- Assign the parameters with the given prefixes to the args table, in order, in batches | |||
-- of the step size specified. This is to prevent references etc. from appearing in the | |||
-- wrong order. The prefixTable should be an array containing tables, each of which has | |||
-- two possible fields, a "prefix" string and a "depend" table. The function always parses | |||
-- parameters containing the "prefix" string, but only parses parameters in the "depend" | |||
-- table if the prefix parameter is present and non-blank. | |||
if type(prefixTable) ~= 'table' then | |||
error("Non-table value detected for the prefix table", 2) | |||
end | |||
if type(step) ~= 'number' then | |||
error("Invalid step value detected", 2) | |||
end | |||
-- Get arguments without a number suffix, and check for bad input. | |||
for i,v in ipairs(prefixTable) do | |||
if type(v) ~= 'table' or type(v.prefix) ~= "string" or (v.depend and type(v.depend) ~= 'table') then | |||
error('Invalid input detected to preprocessArgs prefix table', 2) | |||
end | |||
preprocessSingleArg(v.prefix) | |||
-- Only parse the depend parameter if the prefix parameter is present and not blank. | |||
if args[v.prefix] and v.depend then | |||
for j, dependValue in ipairs(v.depend) do | |||
if type(dependValue) ~= 'string' then | |||
error('Invalid "depend" parameter value detected in preprocessArgs') | |||
end | |||
preprocessSingleArg(dependValue) | |||
end | |||
end | |||
end | |||
-- Get arguments with number suffixes. | |||
local a = 1 -- Counter variable. | |||
local moreArgumentsExist = true | |||
while moreArgumentsExist == true do | |||
moreArgumentsExist = false | |||
for i = a, a + step - 1 do | |||
for j,v in ipairs(prefixTable) do | |||
local prefixArgName = v.prefix .. tostring(i) | |||
if origArgs[prefixArgName] then | |||
moreArgumentsExist = true -- Do another loop if any arguments are found, even blank ones. | |||
preprocessSingleArg(prefixArgName) | |||
end | |||
-- Process the depend table if the prefix argument is present and not blank, or | |||
-- we are processing "prefix1" and "prefix" is present and not blank, and | |||
-- if the depend table is present. | |||
if v.depend and (args[prefixArgName] or (i == 1 and args[v.prefix])) then | |||
for j,dependValue in ipairs(v.depend) do | |||
local dependArgName = dependValue .. tostring(i) | |||
preprocessSingleArg(dependArgName) | |||
end | |||
end | |||
end | |||
end | |||
a = a + step | |||
end | end | ||
end | end | ||
Line 220: | Line 370: | ||
preprocessSingleArg('datetime') | preprocessSingleArg('datetime') | ||
if args['datetime'] then | if args['datetime'] then | ||
output = displayDniTime(surface2DniTime(makeSurfaceTimeStamp(parseSurfaceDateStr(args['datetime'])))) | |||
else | else | ||
preprocessSingleArg('year') | preprocessSingleArg('year') | ||
Line 235: | Line 381: | ||
-- Parse the data parameters | -- Parse the data parameters | ||
surface = {year = args['year'], month = args['month'] | surface = {year = args['year'], month = args['month'], day = args['day'], hour = args['hour'], min = args['minute'], sec = args['second']} | ||
-- return displaySurfaceTime(surface) | |||
output = formatDniTime(surface2DniTime(makeSurfaceTimeStamp(surface))) | |||
end | end | ||
return output | return output | ||
Line 252: | Line 398: | ||
end | end | ||
preprocessSingleArg(' | preprocessSingleArg('hahr') | ||
preprocessSingleArg(' | preprocessSingleArg('vailee') | ||
preprocessSingleArg(' | preprocessSingleArg('yahr') | ||
preprocessSingleArg('gahrtahvo') | |||
preprocessSingleArg('tahvo') | |||
preprocessSingleArg('gorahn') | |||
preprocessSingleArg('prorahn') | |||
-- Parse the data parameters | |||
dni = {hahr = tonumber(args['hahr']), vailee = tonumber(args['vailee']), yahr = tonumber(args['yahr']), gahrtahvo = tonumber(args['gahrtahvo']), tahvo = tonumber(args['tahvo']), gorahn = tonumber(args['gorahn']), prorahn = tonumber(args['prorahn'])} | |||
-- | -- return displaySurfaceTime(surface) | ||
return formatSurfaceTime(dni2SurfaceTime(dni)) | |||
end | end | ||
Line 291: | Line 427: | ||
lang = mw.getContentLanguage() | lang = mw.getContentLanguage() | ||
timeStr = "1991-04-21 16:54:00 +0000" | |||
output = | output = timeStr .. "<br/>"..lang:formatDate("U",timeStr) | ||
output = output .. "<br/>"..displaySurfaceTime(parseSurfaceDateStr(timeStr)) | |||
output = output .. "<br/>"..displaySurfaceTime({year = 1991, month = 4, day = 21, hour = 16, min = 54, sec = 0}) | |||
output = output .. "<br/>"..makeSurfaceTimeStamp(parseSurfaceDateStr(timeStr)) | |||
output = output .. "<br/>".. | output = output .. "<br/>"..makeSurfaceTimeStamp({year = 1991, month = 4, day = 21, hour = 16, min = 54, sec = 0}) | ||
output = output .. "<br/>".. | output = output .. "<br/>"..displayDniTime(surface2DniTime(makeSurfaceTimeStamp(parseSurfaceDateStr(timeStr)))) | ||
output = output .. "<br/>".. | output = output .. "<br/>"..displayDniTime(surface2DniTime(makeSurfaceTimeStamp({year = 1991, month = 4, day = 21, hour = 16, min = 54, sec = 0}))) | ||