Module:Date: Difference between revisions

    (_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 called if lhs and rhs have the same metatable.
    -- 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 called if lhs and rhs have the same metatable.
    -- 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 diff.seconds >= 30 then
    elseif code == 'dhm' or code == 'hm' then
    M = M + 1
    if S >= 30 then
    if M >= 60 then
    M = M + 1
    M = 0
    if M >= 60 then
    inc_hour = true
    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' then
    if code == 'dh' or code == 'dhm' or code == 'dhms' then
    return days, H
    if code == 'dh' then
    elseif code == 'h' or code == 'hm' then
    return days, H
    local hours = days * 24 + H
    elseif code == 'dhm' then
    if code == 'h' then
    return days, H, M
    return hours
    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 days, H, M
    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 diff.seconds >= 30 then
    if S >= 30 then
    M = M + 1
    M = M + 1
    if M >= 60 then
    if M >= 60 then