Module:Date: Difference between revisions
support date difference codes 'dhms' and 'hms'
(_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) |
(support date difference codes 'dhms' and 'hms') |
||
Line 1,102: | Line 1,102: | ||
-- Return true if dates identify same date/time where, for example, | -- Return true if dates identify same date/time where, for example, | ||
-- Date(-4712, 1, 1, 'Julian') == Date(-4713, 11, 24, 'Gregorian') is true. | -- Date(-4712, 1, 1, 'Julian') == Date(-4713, 11, 24, 'Gregorian') is true. | ||
-- This is only | -- This is called only if lhs and rhs have the same type and the same metamethod. | ||
if lhs.partial or rhs.partial then | if lhs.partial or rhs.partial then | ||
-- One date is partial; the other is a partial or a full date. | -- One date is partial; the other is a partial or a full date. | ||
Line 1,114: | Line 1,114: | ||
-- Return true if lhs < rhs, for example, | -- Return true if lhs < rhs, for example, | ||
-- Date('1 Jan 2016') < Date('06:00 1 Jan 2016') is true. | -- Date('1 Jan 2016') < Date('06:00 1 Jan 2016') is true. | ||
-- This is only | -- This is called only if lhs and rhs have the same type and the same metamethod. | ||
if lhs.partial or rhs.partial then | if lhs.partial or rhs.partial then | ||
-- One date is partial; the other is a partial or a full date. | -- One date is partial; the other is a partial or a full date. | ||
Line 1,366: | Line 1,366: | ||
return floor(days/7 + offset) | return floor(days/7 + offset) | ||
end | end | ||
local H, M = diff.hours, diff.minutes | local H, M, S = diff.hours, diff.minutes, diff.seconds | ||
if code == 'dh' or code == 'dhm' or code == 'h' or code == 'hm' then | if code == 'dh' or code == 'dhm' or code == 'dhms' or code == 'h' or code == 'hm' or code == 'hms' 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,375: | Line 1,375: | ||
inc_hour = true | inc_hour = true | ||
end | end | ||
elseif | elseif code == 'dhm' or code == 'hm' then | ||
if S >= 30 then | |||
M = M + 1 | |||
if M >= 60 then | |||
M = 0 | |||
inc_hour = true | |||
end | |||
end | end | ||
else | |||
-- Nothing needed because S is an integer. | |||
end | end | ||
if inc_hour then | if inc_hour then | ||
Line 1,390: | Line 1,394: | ||
end | end | ||
end | end | ||
if code == 'dh' | if code == 'dh' or code == 'dhm' or code == 'dhms' then | ||
if code == 'dh' then | |||
return days, H | |||
elseif code == 'dhm' then | |||
return days, H, M | |||
return | else | ||
return days, H, M, S | |||
end | end | ||
end | |||
local hours = days * 24 + H | |||
if code == 'h' then | |||
return hours | |||
elseif code == 'hm' then | |||
return hours, M | return hours, M | ||
end | end | ||
return | return hours, M, S | ||
end | end | ||
if wantround then | if wantround then | ||
Line 1,408: | Line 1,418: | ||
end | end | ||
elseif code == 'ymdhm' or code == 'ymwdhm' then | elseif code == 'ymdhm' or code == 'ymwdhm' then | ||
if | if S >= 30 then | ||
M = M + 1 | M = M + 1 | ||
if M >= 60 then | if M >= 60 then |