-- https://gist.github.com/DimuDesigns/6a27fbe27b1b400b6de1f85d23e02548
DELIMITER $$
CREATE FUNCTION `getUnixTimestampInMilliseconds`()
RETURNS bigint(20)

BEGIN
  DECLARE milliseconds BIGINT DEFAULT 0;

  SELECT CONV(
	  CONCAT(
		  SUBSTRING(uid,16,3),
		  SUBSTRING(uid,10,4),
		  SUBSTRING(uid,1,8)),
	  16,10)
	  DIV 10000
	  - (141427 * 24 * 60 * 60 * 1000) INTO milliseconds
  FROM (SELECT UUID() uid) AS alias;

  RETURN milliseconds;
END;
$$
DELIMITER ;
