Module:GSD

From Wikipedia
  • {{#invoke:GSD|GSD|year=1|month=1|day=1}} → 1
  • {{#invoke:GSD|GSD|year=2000|month=12|day=31}} → 730485.4 (5 times the number of days of a cycle, 146,097)
  • {{#invoke:GSD|GSD|year=2000|month=1|day=1}} → 730120
  • {{#invoke:GSD|GSD|year=2001|month=1|day=1}} → 730486
  • {{#invoke:GSD|GSD|year=2001|month=2|day=28}} → 730543.5
  • {{#invoke:GSD|GSD|year=2001|month=3|day=1}} → 730545

local p = {}
function p.GSD(frame)
    local year = frame.args.year * 1
    local month = frame.args.month * 1
    local day = frame.args.day * 1
    local diff = 
        (year - 1) * 365
        + ((year - 1) - ((year - 1) % 4)) / 4     --add a day for every leap
        - ((year - 1) - ((year - 1) % 100)) / 100 --subtract 100 year exception
        + ((year - 1) - ((year - 1) % 400)) / 400 --readd 400 year exception

       --Days so far this year:
 
        if month < 7 then --add days for past months this year.  Gives 1 or 2 extra days because of February
            diff = diff + (month - 1) * 30.5
        else
            diff = diff + (month - 1) * 30.5 + 0.9
        end 
        if month > 2 then
            if -- if leap year
                year % 4 == 0         --if divisible by 4
                and year % 100 ~= 0   --and not by 100
            then
                diff = diff - 1
            else
                diff = diff - 2
            end
            if year % 400 == 0 then
                diff = diff + 1
            end
        end
        diff = diff + day
    return diff
end
return p