Created
June 13, 2012 17:10
-
-
Save jacobd/2925314 to your computer and use it in GitHub Desktop.
Stored MySQL function to return a signed Unix timestamp compatible with pre Unix Epoch dates
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
DELIMITER | | |
CREATE FUNCTION SIGNED_UNIX_TIMESTAMP (d DATETIME) | |
RETURNS BIGINT | |
DETERMINISTIC | |
BEGIN | |
DECLARE tz VARCHAR(100); | |
DECLARE ts BIGINT; | |
SET tz = @@time_zone; | |
SET time_zone = '+00:00'; | |
SELECT DATEDIFF(d, FROM_UNIXTIME(0)) * 86400 + | |
TIME_TO_SEC( | |
TIMEDIFF( | |
d, | |
DATE_ADD(MAKEDATE(YEAR(d), DAYOFYEAR(d)), INTERVAL 0 HOUR) | |
) | |
) INTO ts; | |
SET time_zone = tz; | |
return ts; | |
END| | |
DELIMITER ; | |
-- SELECT UNIX_TIMESTAMP('1900-01-02 03:45:00'); | |
-- will return 0 | |
-- SELECT SIGNED_UNIX_TIMESTAMP('1900-01-02 03:45:00'); | |
-- will return -2208888900 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment