Module:Date

From ArchivesWiki

Documentation for this module may be created at Module:Date/doc

local p = {}

p.format = function ( frame )
    if frame.args == nil then
        return 'No args provided'
    end
    local args = frame.args
    local lang = mw.language.getContentLanguage()

    -- Precision.
    if args.precision == nil or args.precision == '' then
        args.precision = 'day'
    end

    -- Timestamp for display.
    if args.timestamp == nil or args.timestamp == '' then
        return ''
    end
    -- What is going on here? Why does 'U' not work on its own? Anyway, stupid workaround
    -- is to split off the year and month after converting.
    local timestamp = string.sub( lang:formatDate( 'YmU', args.timestamp ), 7 )
    local displayTimestamp = timestamp
    if args.timezone ~= nil and args.timezone ~= '' then
        displayTimestamp = displayTimestamp + ( tonumber( args.timezone ) * 60 * 60 )
    end

    -- Format.
    local format = 'Y-m-d H:i:s'
    if args.precision == 'exact' then
        format = '[[Y]] F j D, g:iA'
    elseif args.precision == 'day' then
        format = '[[Y]] F j D'
    elseif args.precision == 'month' then
        format = 'F [[Y]]'
    elseif args.precision == 'year' then
        format = '[[Y]]'
    elseif args.precision == 'circa' then
        format = '\\c. [[Y]]'
    end

    -- Old-format date parameter tracking.
    local cats = ''
    if tonumber( args.precision ) ~= nil then
        cats = '[[Category:Dates with numerical precision]]'
    end

    -- HTML output.
    local timeEl = mw.html.create( 'time' )
    timeEl:attr( 'datetime', lang:formatDate( 'c', '@' .. timestamp ) )
    timeEl:wikitext( lang:formatDate( format, '@' .. displayTimestamp ) )
    return tostring( timeEl ) .. cats
end

return p
-- =p.format({args={timestamp='2019-07-23 12:34:56'}})