Created
September 17, 2014 14:26
-
-
Save mlebkowski/a67ba0a901a5fe919f4c to your computer and use it in GitHub Desktop.
combined ids
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
<?php | |
function compositeId($calendarId, \DateTime $start) { | |
static $startOfTime = 1410963347; | |
$time = ($start->getTimestamp() - $startOfTime) / 60 / 5; | |
if ($time & 0xFFFF0000) { | |
throw new RuntimeException(sprintf('We have ran out of time: %s!', $start->format('Y-m-d H:i:s'))); | |
} | |
if ($calendarId & 0xFFFF0000) { | |
throw new RuntimeException(sprintf('We are too successful (calendarId: %d)', $calendarId)); | |
} | |
return $time + ($calendar << 16); | |
} | |
function reverseCompositeId($compositeId) { | |
static $startOfTime = 1410963347; | |
return [ | |
$compositeId >> 16, | |
DateTime::createFromFormat('U', ($compositeId & 0xFFFF) * 60 * 5 + $startOfTime), | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment