Created
May 15, 2017 12:40
-
-
Save hroi/cd70390801ebc7f4223160bbc24d9345 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Computus | |
-- Easter in the Gregorian calendar | |
-- Copied from https://docs.rs/computus/1.0.0/src/computus/lib.rs.html | |
CREATE OR REPLACE FUNCTION computus_gregorian(_year INTEGER) | |
RETURNS DATE AS $$ | |
DECLARE | |
AA INTEGER; | |
BB INTEGER; | |
CC INTEGER; | |
DD INTEGER; | |
EE INTEGER; | |
FF INTEGER; | |
GG INTEGER; | |
HH INTEGER; | |
II INTEGER; | |
KK INTEGER; | |
LL INTEGER; | |
MM INTEGER; | |
MON INTEGER; | |
DAY INTEGER; | |
BEGIN | |
IF _year < 1583 OR _year > 9999 THEN | |
RAISE EXCEPTION 'Out of range'; | |
END IF; | |
AA := _year % 19; | |
BB := _year / 100; | |
CC := _year % 100; | |
DD := BB / 4; | |
EE := BB % 4; | |
FF := (BB + 8) / 25; | |
GG := (BB - FF + 1) / 3; | |
HH := (19 * AA + BB - DD - GG + 15) % 30; | |
II := CC / 4; | |
KK := CC % 4; | |
LL := (32 + 2 * EE + 2 * II - HH - KK) % 7; | |
MM := (AA + 11 * HH + 22 * LL) / 451; | |
MON := (HH + LL - 7 * MM + 114) / 31; | |
DAY := (HH + LL - 7 * MM + 114) % 31 + 1; | |
RETURN make_date(_year, MON, DAY); | |
END; | |
$$ LANGUAGE plpgsql IMMUTABLE STRICT; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment