Jump to content

Module:Date: Difference between revisions

211 bytes added ,  4 years ago
update from sandbox: implement show=M (minutes) and show=s (seconds); better method to fill a partial date
(fix bug that can occur in {{age}} if attempt to make an invalid yyyy-02-29 date if need to fill month/day from other date)
(update from sandbox: implement show=M (minutes) and show=s (seconds); better method to fill a partial date)
Line 907: Line 907:
-- Return filled date1, date2 (two full dates).
-- Return filled date1, date2 (two full dates).
local function filled(a, b)
local function filled(a, b)
-- Return date a filled, if necessary, with month and/or day from date b.
-- The filled day is truncated to fit the number of days in the month.
local fillmonth, fillday
local fillmonth, fillday
if not a.month then
if not a.month then
Line 915: Line 917:
end
end
if fillmonth or fillday then  -- need to create a new date
if fillmonth or fillday then  -- need to create a new date
if (fillmonth or a.month) == 2 and (fillday or a.day) == 29 then
a = Date(a, {
-- Avoid invalid date, for example with {{age|2013|29 Feb 2016}} or {{age|Feb 2013|29 Jan 2015}}.
month = fillmonth,
if not is_leap_year(a.year, a.calendar) then
day = math.min(fillday or a.day, days_in_month(a.year, fillmonth or a.month, a.calendar))
fillday = 28
})
end
end
a = Date(a, { month = fillmonth, day = fillday })
end
end
return a
return a
Line 1,402: Line 1,401:
end
end
local H, M, S = diff.hours, diff.minutes, diff.seconds
local H, M, S = diff.hours, diff.minutes, diff.seconds
if code == 'dh' or code == 'dhm' or code == 'dhms' or code == 'h' or code == 'hm' or code == 'hms' then
if code == 'dh' or code == 'dhm' or code == 'dhms' or code == 'h' or code == 'hm' or code == 'hms' or code == 'M' or code == 's' then
local days = floor(diff.age_days + extra_days)
local days = floor(diff.age_days + extra_days)
local inc_hour
local inc_hour
Line 1,417: Line 1,416:
inc_hour = true
inc_hour = true
end
end
end
elseif code == 'M' then
if S >= 30 then
M = M + 1
end
end
else
else
Line 1,443: Line 1,446:
elseif code == 'hm' then
elseif code == 'hm' then
return hours, M
return hours, M
elseif code == 'M' or code == 's' then
M = hours * 60 + M
if code == 'M' then
return M
end
return M * 60 + S
end
end
return hours, M, S
return hours, M, S
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.