Last active
May 24, 2023 15:40
-
-
Save SysProfile/0c8d4b68d2adb02415558405851e6005 to your computer and use it in GitHub Desktop.
MySQL/MariaDB SEC_TO_TIME limit: This is a version with some improvements, the original post is here: https://gist.github.com/NaWer/8333736
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
DROP FUNCTION IF EXISTS BIG_SEC_TO_TIME_WITH_DAY; | |
DELIMITER $$ | |
CREATE FUNCTION BIG_SEC_TO_TIME_WITH_DAY(SECS BIGINT, SHOWDAYS BOOLEAN) | |
RETURNS TEXT | |
READS SQL DATA | |
DETERMINISTIC | |
BEGIN | |
/* Basically you can send the seconds (beyond the limit of the SEC_TO_TIME() function) and you decide if it is expressed only in hours or days. */ | |
DECLARE DAYS TEXT; | |
DECLARE HOURS TEXT; | |
DECLARE MINUTES CHAR(5); | |
DECLARE SECONDS CHAR(5); | |
DECLARE RETURNFORMAT TEXT; | |
IF (SECS IS NULL) THEN RETURN NULL; END IF; | |
IF (SECS < 0) THEN RETURN "-"; END IF; | |
SET DAYS = FLOOR(SECS / 86400); | |
SET HOURS = FLOOR(SECS / 3600); | |
SET MINUTES = FLOOR((SECS - (HOURS * 3600)) / 60); | |
SET SECONDS = MOD(SECS, 60); | |
IF SHOWDAYS = TRUE THEN | |
IF HOURS >= 24 THEN SET HOURS = MOD(HOURS, 24); END IF; | |
SET RETURNFORMAT = CONCAT(LPAD(DAYS, 4, " "), " ", LPAD(HOURS, 2, "0"), ":", LPAD(MINUTES, 2, "0"), ":", LPAD(SECONDS, 2, "0")); | |
ELSE | |
SET HOURS = LPAD(HOURS, 2, "0"); | |
SET RETURNFORMAT = CONCAT(LPAD(HOURS, 3, " "), ":", LPAD(MINUTES, 2, "0"), ":", LPAD(SECONDS, 2, "0")); | |
END IF; | |
RETURN RETURNFORMAT; | |
END; | |
$$ | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment