Latest revision |
Your text |
Line 4: |
Line 4: |
| local origArgs | | local origArgs |
| local root | | local root |
|
| |
|
| |
| 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
| |
|
| |
|
|
| |
|
Line 281: |
Line 274: |
| return t, dout | | return t, dout |
| end | | end |
| | |
| | |
| | local function preprocessSingleArg(argName) |
| | -- If the argument exists and isn't blank, add it to the argument table. |
| | -- Blank arguments are treated as nil to match the behaviour of ParserFunctions. |
| | if origArgs[argName] and origArgs[argName] ~= '' then |
| | args[argName] = origArgs[argName] |
| | end |
| | end |
| | |
| | -- |
| | -- Public API |
| | -- |
| | | |
| local function parseSurfaceDate(s,fmt)
| | dateparse = {} |
| | function p.parseSurfaceDate(frame) |
| | |
| | -- If called via #invoke, use the args passed into the invoking template. |
| | -- Otherwise, for testing purposes, assume args are being passed directly in. |
| | if frame == mw.getCurrentFrame() then |
| | origArgs = frame:getParent().args |
| | else |
| | origArgs = frame |
| | end |
| | |
| | preprocessSingleArg('datetime') |
| | preprocessSingleArg('format') |
| | |
| | s = args['datetime'] |
| | fmt = args['format'] |
|
| |
|
| if type(s) ~= 'string' or s == '' or s == 'NULL' then | | if type(s) ~= 'string' or s == '' or s == 'NULL' then |
Line 304: |
Line 325: |
| error('"'..s..'" does not match:\n'.. | | error('"'..s..'" does not match:\n'.. |
| table.concat(all_errors, '\n'), 2) | | table.concat(all_errors, '\n'), 2) |
| end
| |
|
| |
| local function formatSurfaceTime(surfaceTime,format)
| |
|
| |
|
| |
| format = format or "yyyy XX mmmm dd HH:MM:SS"
| |
|
| |
| result = format
| |
|
| |
| if (surfaceTime['year'] < 0) then
| |
| era = "BC"
| |
| sign = ""
| |
| else
| |
| era = "AD"
| |
| sign = "+"
| |
| end
| |
|
| |
| if (surfaceTime['hour'] > 11) then
| |
| hour12 = surfaceTime['hour'] - 12
| |
| ampm = "PM"
| |
| elseif (surfaceTime['hour'] == 0) then
| |
| hour12 = 12
| |
| ampm = "AM"
| |
| else
| |
| hour12 = surfaceTime['hour']
| |
| ampm = "AM"
| |
| end
| |
|
| |
| dateStr = string.format("%s%04d-%02d-%02dT00:00:00Z",sign,surfaceTime['year'],surfaceTime['month'],surfaceTime['day'])
| |
|
| |
| wday = Date(dateStr):text("%{dayname}")
| |
|
| |
| result = string.gsub(result,"%a+", {
| |
| ["yyyy"] = string.format("%04d",tostring(math.abs(surfaceTime['year']))),
| |
| ["yy"] = string.format("%02d",tostring(math.abs(surfaceTime['year']))),
| |
| ["y"] = tostring(math.abs(surfaceTime['year'])),
| |
| ["YYYY"] = string.format("%04d",tostring(surfaceTime['year'])),
| |
| ["YY"] = string.format("%02d",tostring(surfaceTime['year'])),
| |
| ["Y"] = tostring(surfaceTime['year']),
| |
| ["mmmm"] = months[surfaceTime['month']],
| |
| ["mmm"] = string.sub(months[surfaceTime['month']],1,3),
| |
| ["mm"] = string.format("%02d",tostring(surfaceTime['month'])),
| |
| ["m"] = tostring(surfaceTime['month']),
| |
| ["dddd"] = wday,
| |
| ["ddd"] = string.sub(wday,1,3),
| |
| ["dd"] = string.format("%02d",tostring(surfaceTime['day'])),
| |
| ["d"] = tostring(surfaceTime['day']),
| |
| ["HH"] = string.format("%02d",tostring(surfaceTime['hour'])),
| |
| ["hh"] = string.format("%02d",tostring(hour12)),
| |
| ["H"] = tostring(surfaceTime['hour']),
| |
| ["h"] = tostring(hour12),
| |
| ["MM"] = string.format("%02d",tostring(surfaceTime['min'])),
| |
| ["M"] = tostring(surfaceTime['min']),
| |
| ["SS"] = string.format("%02d",tostring(surfaceTime['sec'])),
| |
| ["S"] = tostring(surfaceTime['hour']),
| |
| ["tt"] = ampm,
| |
| ["t"] = string.sub(ampm,1,1),
| |
| ["XX"] = era
| |
| })
| |
|
| |
| return result
| |
|
| |
| end
| |
|
| |
|
| |
| local function formatSurfaceTime_old(surfaceTime,format)
| |
|
| |
| format = format or '%-Y-%m-%d %{era} %H:%M:%S'
| |
|
| |
| dateStr = string.format("%04d-%02d-%02d",surfaceTime['year'],surfaceTime['month'],surfaceTime['day'],surfaceTime['hour'],surfaceTime['min'],surfaceTime['sec'])
| |
|
| |
| if surfaceTime['year'] > 0 then dateStr = "+"..dateStr end
| |
|
| |
| return Date(dateStr):text(format)
| |
|
| |
| end
| |
|
| |
| -- Public API
| |
|
| |
| function p.displaySurfaceTime(surface)
| |
| return displaySurfaceTime(surface)
| |
| end
| |
|
| |
| function p.parseSurfaceDateStr(s,fmt)
| |
| d, dout = parseSurfaceDate(s,fmt)
| |
| return dout
| |
| end
| |
|
| |
| function p.formatSurfaceTime(surfaceTime,format)
| |
| return formatSurfaceTime(surfaceTime,format)
| |
| end | | end |
|
| |
|
| return p | | return p |