Created
May 22, 2018 14:55
-
-
Save chrisvaughn/43d66b930b794920d8aa5e080ef5834f 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
CREATE OR REPLACE FUNCTION current_streak_for_month(x integer, as_of date) RETURNS INTEGER AS | |
$$ | |
DECLARE | |
shifted_checkin bigint; | |
rightmost_zero bigint; | |
BEGIN | |
shifted_checkin := x >> 31 - day_of_month(as_of); | |
rightmost_zero := ~shifted_checkin & (shifted_checkin + 1); | |
RETURN log(2, rightmost_zero)::INTEGER; | |
END | |
$$ LANGUAGE 'plpgsql'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment