Skip to content

Instantly share code, notes, and snippets.

@ianthrive
Last active October 29, 2024 15:08
Show Gist options
  • Save ianthrive/fbd985c155259905868b to your computer and use it in GitHub Desktop.
Save ianthrive/fbd985c155259905868b to your computer and use it in GitHub Desktop.
PostgreSQL functions to determine if a date is a leap year. Two different functions to demonstrate two different ways.
CREATE OR REPLACE FUNCTION is_leap_year(year integer)
RETURNS BOOLEAN AS $$
SELECT ($1 % 4 = 0) AND (($1 % 100 <> 0) or ($1 % 400 = 0))
$$ LANGUAGE sql IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION is_leap_year(date date)
RETURNS BOOLEAN AS $$
SELECT DATE_PART('month', DATE_TRUNC('year', $1)+'1 months 28 days'::INTERVAL) = 2;
$$ LANGUAGE sql IMMUTABLE STRICT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment