Module:Date: Difference between revisions

    (date:list() now accepts a count; DateDiff methods age and duration; cleaner member names; remember if time entered with an am/pm option for default output)
    (use error(...) to report caller's errors)
    Line 378: Line 378:
    -- Return a formatted string representing the given date.
    -- Return a formatted string representing the given date.
    if not is_date(date) then
    if not is_date(date) then
    return 'Need a date (use "date:text()" with a colon).'
    error('date:text: need a date (use "date:text()" with a colon)', 2)
    end
    end
    if type(fmt) ~= 'string' then
    if type(fmt) ~= 'string' then
    Line 409: Line 409:
    f = '%-d %B %-Y %{era}'
    f = '%-d %B %-Y %{era}'
    else
    else
    return '(invalid format)'
    error('date:text: invalid format', 2)
    end
    end
    t:add(f)
    t:add(f)
    Line 485: Line 485:
    -- Return a list of formatted strings from a list of dates.
    -- Return a list of formatted strings from a list of dates.
    if not type(list) == 'table' then
    if not type(list) == 'table' then
    return 'Need "list:text()" with a colon.'
    error('date:list:text: need "list:text()" with a colon', 2)
    end
    end
    local result = { join = _list_join }
    local result = { join = _list_join }
    Line 509: Line 509:
    local list = { text = _list_text }
    local list = { text = _list_text }
    if not is_date(date) then
    if not is_date(date) then
    return 'Need a date (use "date:list()" with a colon).'
    error('date:list: need a date (use "date:list()" with a colon)', 2)
    end
    end
    local count, offset, operation
    local count, offset, operation
    Line 1,053: Line 1,053:
    local mt = {
    local mt = {
    __index = newdate,
    __index = newdate,
    __newindex = function(t, k, v) error('Date.' .. tostring(k) .. ' is read-only', 2) end,
    __newindex = function(t, k, v) error('date.' .. tostring(k) .. ' is read-only', 2) end,
    __add = mt_date_add,
    __add = mt_date_add,
    __sub = mt_date_sub,
    __sub = mt_date_sub,
    Line 1,069: Line 1,069:
    if not is_diff(diff) then
    if not is_diff(diff) then
    local f = options == 'duration' and 'duration' or 'age'
    local f = options == 'duration' and 'duration' or 'age'
    return 'Need a date difference (use "diff:' .. f .. '()" with a colon).'
    error(f .. ': need a date difference (use "diff:' .. f .. '()" with a colon)', 2)
    end
    end
    local extra_day = options == 'duration' and 1 or 0
    local extra_day = options == 'duration' and 1 or 0