Module:Date: Difference between revisions

206 bytes added ,  8 years ago
_diff_age returns nil for an unsupported show code rather than assuming a default which may cause client to show incorrect result; new codes: h, hm where days not wanted
(replace kludge using global set_current_for_test so Module:No globals can be used by a module requiring Module:Date)
(_diff_age returns nil for an unsupported show code rather than assuming a default which may cause client to show incorrect result; new codes: h, hm where days not wanted)
Line 1,309: Line 1,309:
local function _diff_age(diff, code, options)
local function _diff_age(diff, code, options)
-- Return a tuple of integer values from diff as specified by code, except that
-- Return a tuple of integer values from diff as specified by code, except that
-- each integer may be a list of two integers for a diff with a partial date.
-- each integer may be a list of two integers for a diff with a partial date, or
-- return nil if the code is not supported.
-- If want round, the least significant unit is rounded to nearest whole unit.
-- If want round, the least significant unit is rounded to nearest whole unit.
-- For a duration, an extra day is added.
-- For a duration, an extra day is added.
Line 1,347: Line 1,348:
return choose(diff.partial.years), choose(diff.partial.months)
return choose(diff.partial.years), choose(diff.partial.months)
end
end
-- Default: assume code == 'y'; ignore invalid codes.
if code == 'y' then
return choose(diff.partial.years)
return choose(diff.partial.years)
end
return nil
end
end
local extra_days = wantduration and 1 or 0
local extra_days = wantduration and 1 or 0
Line 1,364: Line 1,367:
end
end
local H, M = diff.hours, diff.minutes
local H, M = diff.hours, diff.minutes
if code == 'dh' or code == 'dhm' then
if code == 'dh' or code == 'dhm' or code == 'h' or code == 'hm' then
local days = floor(diff.age_days + extra_days)
local days = floor(diff.age_days + extra_days)
local inc_hour
local inc_hour
if wantround then
if wantround then
if code == 'dh' then
if code == 'dh' or code == 'h' then
if M >= 30 then
if M >= 30 then
inc_hour = true
inc_hour = true
Line 1,389: Line 1,392:
if code == 'dh' then
if code == 'dh' then
return days, H
return days, H
elseif code == 'h' or code == 'hm' then
local hours = days * 24 + H
if code == 'h' then
return hours
end
return hours, M
end
end
return days, H, M
return days, H, M
Line 1,472: Line 1,481:
return y, m, floor(d/7), d % 7, H, M
return y, m, floor(d/7), d % 7, H, M
end
end
-- Default: assume code == 'y'; ignore invalid codes.
if code == 'y' then
if wantround and m >= 6 then
if wantround and m >= 6 then
y = y + 1
y = y + 1
end
return y
end
end
return y
return nil
end
end


Anonymous user