forked from sschiau/Particle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Particle.php
84 lines (72 loc) · 1.92 KB
/
Particle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* Copyright 2014 Silviu Schiau.
*
* This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/**
* @about PHP implementation of Twitter Snowflake ID Generator (64 bit ID)
* @author Silviu Schiau <pr@silviu.co>
* @package Schiau
* @created 1364036124000 (UNIX Time)
* @modified 1418803239960 (UNIX Time)
* @version 1.0.1
* @license Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Thanks to Twitter for Snowflake.
*/
namespace Schiau\Utilities;
abstract class Particle
{
const EPOCH = 1418801787000;
public static function generateParticle($machine_id)
{
/*
* Time - 41 bits
*/
$time = floor(microtime(true) * 1000);
/*
* Substract custom epoch from current time
*/
$time -= self::EPOCH;
/*
* Create a base and add time to it
*/
$base = decbin(pow(2,40) - 1 + $time);
/*
* Configured machine id - 10 bits - up to 512 machines
*/
$machineid = decbin(pow(2,9) - 1 + $machine_id);
/*
* sequence number - 12 bits - up to 2048 random numbers per machine
*/
$random = mt_rand(1, pow(2,11)-1);
$random = decbin(pow(2,11)-1 + $random);
/*
* Pack
*/
$base = $base.$machineid.$random;
/*
* Return unique time id no
*/
return bindec($base);
}
public static function timeFromParticle($particle)
{
/*
* Return time
*/
return bindec(substr(decbin($particle),0,41)) - pow(2,40) + 1 + self::EPOCH;
}
}
?>