Module:Date: Difference between revisions

    (clean up and some aliases)
    (major refactor with fixes; force Date to be read-only (error on write); list of dates in a month on a particular day of week)
    Line 4: Line 4:
    local MINUS = '−'  -- Unicode U+2212 MINUS SIGN
    local MINUS = '−'  -- Unicode U+2212 MINUS SIGN


    local Date, DateDiff, datemt -- forward declarations
    local Date, DateDiff, diffmt -- forward declarations
    local uniq = { 'unique identifier' }


    local function is_date(t)
    local function is_date(t)
    return type(t) == 'table' and getmetatable(t) == datemt
    -- The system used to make a date read-only means there is no unique
    -- metatable that is conveniently accessible to check.
    return type(t) == 'table' and t._id == uniq
    end
     
    local function is_diff(t)
    return type(t) == 'table' and getmetatable(t) == diffmt
    end
     
    local function _list_join(list, sep)
    return table.concat(list, sep)
    end
    end


    Line 18: Line 29:
    self[self.n] = item
    self[self.n] = item
    end,
    end,
    join = function (self, sep)
    join = _list_join,
    return table.concat(self, sep)
    end,
    }
    }
    end
    end
    Line 93: Line 102:
    local floor = math.floor
    local floor = math.floor
    local calname = date.calname
    local calname = date.calname
    local jd = date.jd
    local limits  -- min/max limits for date ranges −9999-01-01 to 9999-12-31
    local limits  -- min/max limits for date ranges −9999-01-01 to 9999-12-31
    if calname == 'Julian' then
    if calname == 'Julian' then
    Line 100: Line 108:
    limits = { -1930999.5, 5373484.49999 }
    limits = { -1930999.5, 5373484.49999 }
    else
    else
    limits = { 1, 0 }  -- impossible
    return
    end
    end
    if not (limits[1] <= jd and jd <= limits[2]) then
    local jd = date.jd
    if not (type(jd) == 'number' and limits[1] <= jd and jd <= limits[2]) then
    return
    return
    end
    end
    Line 150: Line 159:
    return
    return
    end
    end
    local y = numbers.y or numbers[1]
    local y = numbers.year  or date.year
    local m = numbers.m or numbers[2]
    local m = numbers.month  or date.month
    local d = numbers.d or numbers[3]
    local d = numbers.day    or date.day
    local H = numbers.H or numbers[4]
    local H = numbers.hour
    local M = numbers.M or numbers[5] or 0
    local M = numbers.minute or date.minute or 0
    local S = numbers.S or numbers[6] or 0
    local S = numbers.second or date.second or 0
    if not (y and m and d) then
    if not (y and m and d) or not
    return
    (-9999 <= y and y <= 9999 and
    end
    1 <= m and m <= 12 and
    if not (-9999 <= y and y <= 9999 and 1 <= m and m <= 12 and
    1 <= d and d <= days_in_month(y, m, date.calname)) then
    1 <= d and d <= days_in_month(y, m, date.calname)) then
    return
    return
    end
    end
    Line 166: Line 174:
    date.hastime = true
    date.hastime = true
    else
    else
    H = 0
    H = date.hour or 0
    end
    end
    if not (0 <= H and H <= 23 and
    if not (0 <= H and H <= 23 and
    Line 206: Line 214:
    -- Example: 'am:AM era:BC'
    -- Example: 'am:AM era:BC'
    for item in options1:gmatch('%S+') do
    for item in options1:gmatch('%S+') do
    local lhs, rhs = item:match('^(%w+):(.+)$')
    local lhs, rhs = item:match('^(%w+)[:=](.+)$')
    if lhs then
    if lhs then
    result[lhs] = rhs
    result[lhs] = rhs
    Line 339: Line 347:
    :gsub('%%%%', PERCENT)
    :gsub('%%%%', PERCENT)
    :gsub('(%s*)%%{(%w+)}', replace_property)
    :gsub('(%s*)%%{(%w+)}', replace_property)
    :gsub('(%s*)%%(-?)(%a)', replace_code)
    :gsub('(%s*)%%(%-?)(%a)', replace_code)
    :gsub(PERCENT, '%%')
    :gsub(PERCENT, '%%')
    )
    )
    Line 345: Line 353:


    local function _date_text(date, fmt, options)
    local function _date_text(date, fmt, options)
    -- Return formatted string from 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).'
    return 'Need a date (use "date:text()" with a colon).'
    Line 410: Line 418:
    { 'Dec', 'December' },
    { 'Dec', 'December' },
    }
    }
    local function name_to_number(text, translate)
    if type(text) == 'string' then
    return translate[text:lower()]
    end
    end
    local function day_number(text)
    return name_to_number(text, {
    sun = 0, sunday = 0,
    mon = 1, monday = 1,
    tue = 2, tuesday = 2,
    wed = 3, wednesday = 3,
    thu = 4, thursday = 4,
    fri = 5, friday = 5,
    sat = 6, saturday = 6,
    })
    end


    local function month_number(text)
    local function month_number(text)
    if type(text) == 'string' then
    return name_to_number(text, {
    local month_names = {
    jan = 1, january = 1,
    jan = 1, january = 1,
    feb = 2, february = 2,
    feb = 2, february = 2,
    mar = 3, march = 3,
    mar = 3, march = 3,
    apr = 4, april = 4,
    apr = 4, april = 4,
    may = 5,
    may = 5,
    jun = 6, june = 6,
    jun = 6, june = 6,
    jul = 7, july = 7,
    jul = 7, july = 7,
    aug = 8, august = 8,
    aug = 8, august = 8,
    sep = 9, september = 9,
    sep = 9, september = 9,
    oct = 10, october = 10,
    oct = 10, october = 10,
    nov = 11, november = 11,
    nov = 11, november = 11,
    dec = 12, december = 12,
    dec = 12, december = 12
    })
    }
    end
    return month_names[text:lower()]
     
    local function _list_text(list, fmt)
    -- Return a list of formatted strings from a list of dates.
    if not type(list) == 'table' then
    return 'Need "list:text()" with a colon.'
    end
    local result = { join = _list_join }
    for i, date in ipairs(list) do
    result[i] = date:text(fmt)
    end
    return result
    end
     
    local function _make_list(date, spec)
    -- Return a possibly empty numbered table of dates meeting the specification.
    -- The spec should be a string like "Tue >=" meaning that the list will
    -- hold dates for all Tuesdays on or after date, and in date's month.
    if not is_date(date) then
    return 'Need a date (use "date:list()" with a colon).'
    end
    local want_dow, op
    local ops = {
    ['>='] = { before = false, include = true  },
    ['>']  = { before = false, include = false },
    ['<='] = { before = true , include = true  },
    ['<']  = { before = true , include = false },
    }
    if spec then
    if type(spec) ~= 'string' then
    return {}
    end
    for item in spec:gmatch('%S+') do
    if ops[item] then
    if op then
    return {}
    end
    op = ops[item]
    else
    local dow = day_number(item)
    if dow then
    if want_dow then
    -- LATER Could handle more than one day, but probably not needed.
    return {}
    end
    want_dow = dow
    else
    return {}
    end
    end
    end
    end
    local offset = want_dow and want_dow - date.dow or 0
    op = op or ops['>=']
    local first, last
    if op.before then
    if offset >= 0 and not (op.include and offset == 0) then
    offset = offset - 7
    end
    last = date.day + offset
    first = last % 7
    if first == 0 then
    first = 7
    end
    else
    if offset < 0 or (not op.include and offset == 0) then
    offset = offset + 7
    end
    first = date.day + offset
    last = date.monthdays
    end
    local list = { text = _list_text }
    local count = math.floor((last - first)/7) + 1
    for i = 1, count do
    list[i] = Date(date, {day = first})
    first = first + 7
    end
    end
    return list
    end
    end


    -- A table to get the current date/time (UTC), but only if needed.
    -- A table to get the current date/time (UTC), but only if needed.
    local current = setmetatable({}, {
    -- A local test can set the global variable to produce fixed results.
    __index = function (self, key)
    local current = setmetatable(
    local d = os.date('!*t')
    set_current_for_test or {}, {
    self.year = d.year
    __index = function (self, key)
    self.month = d.month
    local d = os.date('!*t')
    self.day = d.day
    self.year = d.year
    self.hour = d.hour
    self.month = d.month
    self.minute = d.min
    self.day = d.day
    self.second = d.sec
    self.hour = d.hour
    return rawget(self, key)
    self.minute = d.min
    end
    self.second = d.sec
    })
    return rawget(self, key)
    end })


    local function extract_date(text)
    local function extract_date(text)
    Line 458: Line 560:
    local date, options = {}, {}
    local date, options = {}, {}
    local function extract_ymd(item)
    local function extract_ymd(item)
    local ystr, mstr, dstr = item:match('^(%d%d%d%d)-(%w+)-(%d%d?)$')
    local ystr, mstr, dstr = item:match('^(%d%d%d%d)%-(%w+)%-(%d%d?)$')
    if ystr then
    if ystr then
    local m
    local m
    Line 467: Line 569:
    end
    end
    if m then
    if m then
    date.y = tonumber(ystr)
    date.year = tonumber(ystr)
    date.m = m
    date.month = m
    date.d = tonumber(dstr)
    date.day = tonumber(dstr)
    return true
    return true
    end
    end
    Line 478: Line 580:
    local m = month_number(item)
    local m = month_number(item)
    if m then
    if m then
    date.m = m
    date.month = m
    return true
    return true
    end
    end
    Line 484: Line 586:
    local function extract_time(item)
    local function extract_time(item)
    local h, m, s = item:match('^(%d%d?):(%d%d)(:?%d*)$')
    local h, m, s = item:match('^(%d%d?):(%d%d)(:?%d*)$')
    if date.H or not h then
    if date.hour or not h then
    return
    return
    end
    end
    Line 493: Line 595:
    end
    end
    end
    end
    date.H = tonumber(h)
    date.hour = tonumber(h)
    date.M = tonumber(m)
    date.minute = tonumber(m)
    date.S = tonumber(s)  -- nil if empty string
    date.second = tonumber(s)  -- nil if empty string
    return true
    return true
    end
    end
    Line 511: Line 613:
    local index_time
    local index_time
    local function set_ampm(item)
    local function set_ampm(item)
    local H = date.H
    local H = date.hour
    if H and not options.am and index_time + 1 == item_count then
    if H and not options.am and index_time + 1 == item_count then
    options.am = ampm_options[item]
    options.am = ampm_options[item]
    Line 519: Line 621:
    end
    end
    if H == 12 then
    if H == 12 then
    date.H = 0
    date.hour = 0
    end
    end
    else
    else
    Line 526: Line 628:
    end
    end
    if H <= 11 then
    if H <= 11 then
    date.H = H + 12
    date.hour = H + 12
    end
    end
    end
    end
    Line 549: Line 651:
    end
    end
    index_time = item_count
    index_time = item_count
    elseif date.d and date.m then
    elseif date.day and date.month then
    if date.y then
    if date.year then
    return  -- should be nothing more so item is invalid
    return  -- should be nothing more so item is invalid
    end
    end
    Line 556: Line 658:
    return
    return
    end
    end
    date.y = tonumber(item)
    date.year = tonumber(item)
    elseif date.d then
    elseif date.day then
    if not extract_month(item) then
    if not extract_month(item) then
    return
    return
    end
    end
    elseif date.m then
    elseif date.month then
    if not item:match('^(%d%d?)$') then
    if not item:match('^(%d%d?)$') then
    return
    return
    end
    end
    date.d = tonumber(item)
    date.day = tonumber(item)
    elseif not extract_ymd(item) then
    elseif not extract_ymd(item) then
    if item:match('^(%d%d?)$') then
    if item:match('^(%d%d?)$') then
    date.d = tonumber(item)
    date.day = tonumber(item)
    elseif not extract_month(item) then
    elseif not extract_month(item) then
    return
    return
    Line 574: Line 676:
    end
    end
    end
    end
    if not date.y or date.y == 0 then
    if not date.year or date.year == 0 then
    return
    return
    end
    end
    local era = era_text[options.era]
    local era = era_text[options.era]
    if era and era.isbc then
    if era and era.isbc then
    date.y = 1 - date.y
    date.year = 1 - date.year
    end
    end
    return date, options
    return date, options
    Line 587: Line 689:
    -- Return a new date from calculating (lhs + rhs) or (lhs - rhs),
    -- Return a new date from calculating (lhs + rhs) or (lhs - rhs),
    -- or return nothing if invalid.
    -- or return nothing if invalid.
    -- The result is nil if the calculated date exceeds allowable limits.
    -- Caller ensures that lhs is a date; its properties are copied for the new date.
    -- Caller ensures that lhs is a date; its properties are copied for the new date.
    local function is_prefix(text, word, minlen)
    local function is_prefix(text, word, minlen)
    Line 593: Line 696:
    end
    end
    local function do_days(n)
    local function do_days(n)
    if is_sub then
    local forcetime, jd
    n = -n
    if math.floor(n) == n then
    jd = lhs.jd
    else
    forcetime = not lhs.hastime
    jd = lhs.jdz
    end
    end
    return Date(lhs, 'juliandate', lhs.jd + n)
    jd = jd + (is_sub and -n or n)
    if forcetime then
    jd = tostring(jd)
    if not jd:find('.', 1, true) then
    jd = jd .. '.0'
    end
    end
    return Date(lhs, 'juliandate', jd)
    end
    end
    if type(rhs) == 'number' then
    if type(rhs) == 'number' then
    -- Add days, including fractional days.
    -- Add/subtract days, including fractional days.
    return do_days(rhs)
    return do_days(rhs)
    end
    end
    Line 648: Line 762:
    end
    end


    -- Metatable for some operations on dates.
    -- Metatable for a date's calculated fields.
    datemt = {  -- for forward declaration above
    local datemt = {
    __add = function (lhs, rhs)
    if not is_date(lhs) then
    lhs, rhs = rhs, lhs  -- put date on left (it must be a date for this to have been called)
    end
    return date_add_sub(lhs, rhs)
    end,
    __sub = function (lhs, rhs)
    if is_date(lhs) then
    if is_date(rhs) then
    return DateDiff(lhs, rhs)
    end
    return date_add_sub(lhs, rhs, true)
    end
    end,
    __concat = function (lhs, rhs)
    return tostring(lhs) .. tostring(rhs)
    end,
    __tostring = function (self)
    return self:text()
    end,
    __eq = function (lhs, rhs)
    -- Return true if dates identify same date/time where, for example,
    -- Date(-4712, 1, 1, 'Julian') == Date(-4713, 11, 24, 'Gregorian') is true.
    -- This is only called if lhs and rhs have the same metatable.
    return lhs.jdz == rhs.jdz
    end,
    __lt = function (lhs, rhs)
    -- Return true if lhs < rhs, for example,
    -- Date('1 Jan 2016') < Date('06:00 1 Jan 2016') is true.
    -- This is only called if lhs and rhs have the same metatable.
    return lhs.jdz < rhs.jdz
    end,
    __index = function (self, key)
    __index = function (self, key)
    local value
    local value
    Line 689: Line 771:
    value = day_info[self.dow][2]
    value = day_info[self.dow][2]
    elseif key == 'dow' then
    elseif key == 'dow' then
    value = (self.jd + 1) % 7  -- day-of-week 0=Sun to 6=Sat
    value = (self.jdnoon + 1) % 7  -- day-of-week 0=Sun to 6=Sat
    elseif key == 'dayofweek' then
    elseif key == 'dayofweek' then
    value = self.dow
    value = self.dow
    elseif key == 'dowiso' then
    elseif key == 'dowiso' then
    value = (self.jd % 7) + 1  -- ISO day-of-week 1=Mon to 7=Sun
    value = (self.jdnoon % 7) + 1  -- ISO day-of-week 1=Mon to 7=Sun
    elseif key == 'dayofweekiso' then
    elseif key == 'dayofweekiso' then
    value = self.dowiso
    value = self.dowiso
    elseif key == 'doy' then
    elseif key == 'doy' then
    local first = Date(self.year, 1, 1, self.calname).jd
    local first = Date(self.year, 1, 1, self.calname).jdnoon
    value = self.jd - first + 1  -- day-of-year 1 to 366
    value = self.jdnoon - first + 1  -- day-of-year 1 to 366
    elseif key == 'dayofyear' then
    elseif key == 'dayofyear' then
    value = self.doy
    value = self.doy
    Line 706: Line 788:
    elseif key == 'gsd' then
    elseif key == 'gsd' then
    -- GSD = 1 from 00:00:00 to 23:59:59 on 1 January 1 AD Gregorian calendar,
    -- GSD = 1 from 00:00:00 to 23:59:59 on 1 January 1 AD Gregorian calendar,
    -- which is JDN = 1721426, and is from jd 1721425.5 to 1721426.49999.
    -- which is from jd 1721425.5 to 1721426.49999.
    value = math.floor(self.jd - 1721424.5)
    value = math.floor(self.jd - 1721424.5)
    elseif key == 'juliandate' or key == 'jd' or key == 'jdz' then
    elseif key == 'juliandate' or key == 'jd' or key == 'jdz' then
    Line 714: Line 796:
    rawset(self, 'jdz', jdz)
    rawset(self, 'jdz', jdz)
    return key == 'jdz' and jdz or jd
    return key == 'jdz' and jdz or jd
    elseif key == 'jdnoon' then
    -- Julian date at noon (an integer) on the calendar day when jd occurs.
    value = math.floor(self.jd + 0.5)
    elseif key == 'isleapyear' then
    elseif key == 'isleapyear' then
    value = is_leap_year(self.year, self.calname)
    value = is_leap_year(self.year, self.calname)
    Line 729: Line 814:
    end,
    end,
    }
    }
    -- Date operators.
    local function mt_date_add(lhs, rhs)
    if not is_date(lhs) then
    lhs, rhs = rhs, lhs  -- put date on left (it must be a date for this to have been called)
    end
    return date_add_sub(lhs, rhs)
    end
    local function mt_date_sub(lhs, rhs)
    if is_date(lhs) then
    if is_date(rhs) then
    return DateDiff(lhs, rhs)
    end
    return date_add_sub(lhs, rhs, true)
    end
    end
    local function mt_date_concat(lhs, rhs)
    return tostring(lhs) .. tostring(rhs)
    end
    local function mt_date_tostring(self)
    return self:text()
    end
    local function mt_date_eq(lhs, rhs)
    -- Return true if dates identify same date/time where, for example,
    -- Date(-4712, 1, 1, 'Julian') == Date(-4713, 11, 24, 'Gregorian') is true.
    -- This is only called if lhs and rhs have the same metatable.
    return lhs.jdz == rhs.jdz
    end
    local function mt_date_lt(lhs, rhs)
    -- Return true if lhs < rhs, for example,
    -- Date('1 Jan 2016') < Date('06:00 1 Jan 2016') is true.
    -- This is only called if lhs and rhs have the same metatable.
    return lhs.jdz < rhs.jdz
    end


    --[[ Examples of syntax to construct a date:
    --[[ Examples of syntax to construct a date:
    Line 740: Line 864:
    Date('04:30:59 1 April 1995', 'julian')
    Date('04:30:59 1 April 1995', 'julian')
    Date(date)                          copy of an existing date
    Date(date)                          copy of an existing date
    LATER: Following is not yet implemented:
    Date(date, t)                      same, updated with y,m,d,H,M,S fields from table t
    Date('currentdate', H, M, S)       current date with given time
    Date(t)                       date with y,m,d,H,M,S fields from table t
    ]]
    ]]
    function Date(...)  -- for forward declaration above
    function Date(...)  -- for forward declaration above
    Line 747: Line 871:
    -- (proleptic Gregorian calendar or proleptic Julian calendar), or
    -- (proleptic Gregorian calendar or proleptic Julian calendar), or
    -- return nothing if date is invalid.
    -- return nothing if date is invalid.
    local is_copy
    local calendars = { julian = 'Julian', gregorian = 'Gregorian' }
    local calendars = { julian = 'Julian', gregorian = 'Gregorian' }
    local result = {
    local newdate = {
    _id = uniq,
    calname = 'Gregorian',  -- default is Gregorian calendar
    calname = 'Gregorian',  -- default is Gregorian calendar
    hastime = false,  -- true if input sets a time
    hastime = false,  -- true if input sets a time
    Line 756: Line 880:
    second = 0,
    second = 0,
    options = make_option_table(),
    options = make_option_table(),
    list = _make_list,
    text = _date_text,
    text = _date_text,
    }
    }
    local argtype, datetext
    local argtype, datetext, is_copy, jd_number, tnums
    local numbers = collection()
    local numindex = 0
    local numfields = { 'year', 'month', 'day', 'hour', 'minute', 'second' }
    local numbers = {}
    for _, v in ipairs({...}) do
    for _, v in ipairs({...}) do
    v = strip_to_nil(v)
    v = strip_to_nil(v)
    Line 766: Line 893:
    -- Ignore empty arguments after stripping so modules can directly pass template parameters.
    -- Ignore empty arguments after stripping so modules can directly pass template parameters.
    elseif calendars[vlower] then
    elseif calendars[vlower] then
    result.calname = calendars[vlower]
    newdate.calname = calendars[vlower]
    elseif is_date(v) then
    elseif is_date(v) then
    -- Copy existing date (items can be overridden by other arguments).
    -- Copy existing date (items can be overridden by other arguments).
    if is_copy then
    if is_copy or tnums then
    return
    return
    end
    end
    is_copy = true
    is_copy = true
    result.calname = v.calname
    newdate.calname = v.calname
    result.hastime = v.hastime
    newdate.hastime = v.hastime
    result.options = v.options
    newdate.options = v.options
    result.year = v.year
    newdate.year = v.year
    result.month = v.month
    newdate.month = v.month
    result.day = v.day
    newdate.day = v.day
    result.hour = v.hour
    newdate.hour = v.hour
    result.minute = v.minute
    newdate.minute = v.minute
    result.second = v.second
    newdate.second = v.second
    elseif type(v) == 'table' then
    if tnums then
    return
    end
    tnums = {}
    local tfields = { year=1, month=1, day=1, hour=2, minute=2, second=2 }
    for tk, tv in pairs(v) do
    if tfields[tk] then
    tnums[tk] = tonumber(tv)
    end
    if tfields[tk] == 2 then
    newdate.hastime = true
    end
    end
    else
    else
    local num = tonumber(v)
    local num = tonumber(v)
    if not num and argtype == 'setdate' and numbers.n == 1 then
    if not num and argtype == 'setdate' and numindex == 1 then
    num = month_number(v)
    num = month_number(v)
    end
    end
    Line 791: Line 932:
    argtype = 'setdate'
    argtype = 'setdate'
    end
    end
    numbers:add(num)
    if argtype == 'setdate' and numindex < 6 then
    if argtype == 'juliandate' then
    numindex = numindex + 1
    numbers[numfields[numindex]] = num
    elseif argtype == 'juliandate' and not jd_number then
    jd_number = num
    if type(v) == 'string' then
    if type(v) == 'string' then
    if v:find('.', 1, true) then
    if v:find('.', 1, true) then
    result.hastime = true
    newdate.hastime = true
    end
    end
    elseif num ~= math.floor(num) then
    elseif num ~= math.floor(num) then
    -- The given value was a number. The time will be used
    -- The given value was a number. The time will be used
    -- if the fractional part is nonzero.
    -- if the fractional part is nonzero.
    result.hastime = true
    newdate.hastime = true
    end
    end
    else
    return
    end
    end
    elseif argtype then
    elseif argtype then
    Line 818: Line 964:
    end
    end
    if argtype == 'datetext' then
    if argtype == 'datetext' then
    if not (numbers.n == 0 and
    if tnums or not set_date_from_numbers(newdate, extract_date(datetext)) then
    set_date_from_numbers(result,
    extract_date(datetext))) then
    return
    return
    end
    end
    elseif argtype == 'juliandate' then
    elseif argtype == 'juliandate' then
    result.jd = numbers[1]
    newdate.jd = jd_number
    if not (numbers.n == 1 and set_date_from_jd(result)) then
    if not set_date_from_jd(newdate) then
    return
    return
    end
    end
    elseif argtype == 'currentdate' or argtype == 'currentdatetime' then
    elseif argtype == 'currentdate' or argtype == 'currentdatetime' then
    result.year = current.year
    newdate.year = current.year
    result.month = current.month
    newdate.month = current.month
    result.day = current.day
    newdate.day = current.day
    if argtype == 'currentdatetime' then
    if argtype == 'currentdatetime' then
    result.hour = current.hour
    newdate.hour = current.hour
    result.minute = current.minute
    newdate.minute = current.minute
    result.second = current.second
    newdate.second = current.second
    result.hastime = true
    newdate.hastime = true
    end
    end
    result.calname = 'Gregorian'  -- ignore any given calendar name
    newdate.calname = 'Gregorian'  -- ignore any given calendar name
    elseif argtype == 'setdate' then
    elseif argtype == 'setdate' then
    if not set_date_from_numbers(result, numbers) then
    if tnums or not set_date_from_numbers(newdate, numbers) then
    return
    return
    end
    end
    elseif not is_copy then
    elseif not (is_copy or tnums) then
    return
    return
    end
    end
    return setmetatable(result, datemt)
    if tnums then
    newdate.jd = nil  -- force recalculation in case jd was set before changes from tnums
    if not set_date_from_numbers(newdate, tnums) then
    return
    end
    end
    setmetatable(newdate, datemt)
    local readonly = {}
    local mt = {
    __index = newdate,
    __newindex = function(t, k, v) error('Date.' .. tostring(k) .. ' is read-only', 2) end,
    __add = mt_date_add,
    __sub = mt_date_sub,
    __concat = mt_date_concat,
    __tostring = mt_date_tostring,
    __eq = mt_date_eq,
    __lt = mt_date_lt,
    }
    return setmetatable(readonly, mt)
    end
    end
    local function _age_ym(diff)
    -- Return text specifying date difference in years, months.
    local sign = diff.isnegative and MINUS or ''
    local mtext = number_name(diff.months, 'month')
    local result
    if diff.years > 0 then
    local ytext = number_name(diff.years, 'year')
    if diff.months == 0 then
    result = ytext
    else
    result = ytext .. ',&nbsp;' .. mtext
    end
    else
    if diff.months == 0 then
    sign = ''
    end
    result = mtext
    end
    return sign .. result
    end
    -- Metatable for some operations on date differences.
    diffmt = {  -- for forward declaration above
    __concat = function (lhs, rhs)
    return tostring(lhs) .. tostring(rhs)
    end,
    __tostring = function (self)
    return tostring(self.daystotal)
    end,
    __index = function (self, key)
    local value
    if key == 'age_ym' then
    value = _age_ym(self)
    elseif key == 'daystotal' then
    value = self.date1.jdz - self.date2.jdz
    end
    if value ~= nil then
    rawset(self, key, value)
    return value
    end
    end,
    }


    function DateDiff(date1, date2)  -- for forward declaration above
    function DateDiff(date1, date2)  -- for forward declaration above
    -- Return a table with the difference between the two dates (date1 - date2).
    -- Return a table with the difference between the two dates (date1 - date2).
    -- The difference is negative if date2 is more recent than date1.
    -- The difference is negative if date1 is older than date2.
    -- Return nothing if invalid.
    -- Return nothing if invalid.
    if not (is_date(date1) and is_date(date2) and date1.calname == date2.calname) then
    if not (is_date(date1) and is_date(date2) and date1.calname == date2.calname) then
    return
    return
    end
    end
    local isnegative
    local isnegative = false
    if date1 < date2 then
    if date1 < date2 then
    isnegative = true
    isnegative = true
    Line 873: Line 1,078:
    years = years - 1
    years = years - 1
    end
    end
    return {
    return setmetatable({
    date1 = date1,
    date2 = date2,
    years = years,
    years = years,
    months = months,
    months = months,
    days = days,
    days = days,
    isnegative = isnegative,
    isnegative = isnegative,
    age_ym = function (self)
    }, diffmt)
    -- Return text specifying difference in years, months.
    local sign = self.isnegative and MINUS or ''
    local mtext = number_name(self.months, 'month')
    local result
    if self.years > 0 then
    local ytext = number_name(self.years, 'year')
    if self.months == 0 then
    result = ytext
    else
    result = ytext .. ',&nbsp;' .. mtext
    end
    else
    if self.months == 0 then
    sign = ''
    end
    result = mtext
    end
    return sign .. result
    end,
    }
    end
    end