Created
February 14, 2012 05:00
-
-
Save benmacleod/1823672 to your computer and use it in GitHub Desktop.
Leap year agnostic day of year function for Postgres PL/SQL
This file contains 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
CREATE OR REPLACE FUNCTION yday(time_in TIMESTAMP) RETURNS INTEGER AS $$ | |
DECLARE | |
yr INTEGER; | |
mnth INTEGER; | |
yday INTEGER; | |
BEGIN | |
-- Ensure March 1st and after always have the same day of year across leap-years | |
-- by adding 1 to the day of year for months after Feb in non-leap years | |
yday := EXTRACT(doy FROM time_in); | |
yr := EXTRACT(year FROM time_in); | |
mnth := EXTRACT(month FROM time_in); | |
IF yr % 4 > 0 and mnth > 2 THEN | |
yday := yday + 1; | |
END IF; | |
RETURN yday; | |
END; | |
$$ LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function not includes adjustments introduced by Gregorian calendar.